From 34386973f6d93dba5c15f990535e4898056d026b Mon Sep 17 00:00:00 2001 From: Aaron Mueller Date: Sat, 28 Sep 2013 14:15:22 +0200 Subject: [PATCH] Rearrange some stuff and start implementing the image processing --- resources/public/css/screen.css | 3 +- src/ldview/tasks/images.clj | 29 +++++++++++++++++ src/ldview/tasks/{ldscrape.clj => scrape.clj} | 32 ++++++++++++++----- src/ldview/util.clj | 11 +++---- 4 files changed, 59 insertions(+), 16 deletions(-) create mode 100644 src/ldview/tasks/images.clj rename src/ldview/tasks/{ldscrape.clj => scrape.clj} (61%) diff --git a/resources/public/css/screen.css b/resources/public/css/screen.css index 4059e62..94b1153 100644 --- a/resources/public/css/screen.css +++ b/resources/public/css/screen.css @@ -7,8 +7,7 @@ #container { margin: auto; text-align: center; - border-left: 2px solid gray; - border-right: 2px solid gray; + padding: 0 20px; } div.entry { diff --git a/src/ldview/tasks/images.clj b/src/ldview/tasks/images.clj new file mode 100644 index 0000000..b28ba8b --- /dev/null +++ b/src/ldview/tasks/images.clj @@ -0,0 +1,29 @@ +(ns ldview.tasks.images + (:require [image-resizer.core :refer :all] + [image-resizer.util :as utils] + [image-resizer.format :refer :all] + [image-resizer.crop :refer :all] + [clojure.java.io :as io]) + (:use [ldview.util])) + +(defn image-name [folder entry-id number] + (str *image-base-path* "/" folder "/" entry-id "_" number ".png")) + +(defn to-square [file new-size] + (let [[width height] (dimensions (utils/buffered-image file))] + (if (< width height) + (let [resized-img (resize-to-width file new-size) + [w h] (dimensions resized-img) + crop-margin (quot (- h new-size) 2)] + (crop-from resized-img 0 crop-margin new-size new-size)) + (let [resized-img (resize-to-height file new-size) + [w h] (dimensions resized-img) + crop-margin (quot (- w new-size) 2)] + (crop-from resized-img crop-margin 0 new-size new-size))))) + +(defn sourceimage->thumb [image-path new-image-path] + (as-file (to-square (io/file image-path) 200) new-image-path)) + +(defn sourceimage->fullscreen [image-path new-image-path] + (as-file (resize (io/file image-path) 800 600) new-image-path)) + diff --git a/src/ldview/tasks/ldscrape.clj b/src/ldview/tasks/scrape.clj similarity index 61% rename from src/ldview/tasks/ldscrape.clj rename to src/ldview/tasks/scrape.clj index daa9e78..317cbdd 100644 --- a/src/ldview/tasks/ldscrape.clj +++ b/src/ldview/tasks/scrape.clj @@ -1,10 +1,12 @@ -(ns ldview.ldscrape - (:require [net.cgrand.enlive-html :as html]) - (:use [clojure.string :only (split)])) - -(def ^:dynamic *base-url* "http://www.ludumdare.com/compo/") -(def ^:dynamic *competition* 27) +(ns ldview.tasks.scrape + (:require [net.cgrand.enlive-html :as html] + [clj-http.client :as http] + [clojure.java.io :as io] + (:use [clojure.string :only (split)] + [ldview.util])) +; Some helper functions to construct proper urls. If the Ludum Date Website +; changes some URL stuff, this is the place to crank. (defn url-action [action] (str *base-url* "/ludum-dare-" *competition* "/?action=" action)) @@ -14,10 +16,19 @@ (defn url-entry [entry-id] (str (url-action "preview") "&uid=" entry-id)) - +; Helpers to fetch an html resource or save an url from the web into a +; local file. This thow functions will be needed to pull the content from +; the website. (defn fetch-url [url] (html/html-resource (java.net.URL. url))) +(defn save-image-from-url [url target-path new-name] + (with-open [bodystream (:body (http/get url {:as :stream}))] + (io/copy bodystream (io/file (str target-path new-name))))) + + +; The actual scraping process. We crawl through the entire content results +; and fetch the relevant information from the DOM. (defn number-of-pages [] (let [p (second (html/select (fetch-url (url-action "preview")) [:div#compo2 :> :p]))] (read-string (html/text (last (butlast (html/select [p] [:a]))))))) @@ -46,5 +57,10 @@ :links links :images images})) +; Do the job +; TODO: Make it multithreaded and resumable. (defn fetch-page [page] - (map entry-details (entries-on-page 1))) + (map entry-details (entries-on-page page))) + +(defn fetch-all [] + (map fetch-page (number-of-pages))) diff --git a/src/ldview/util.clj b/src/ldview/util.clj index 93875ff..518cdff 100644 --- a/src/ldview/util.clj +++ b/src/ldview/util.clj @@ -2,9 +2,8 @@ (:require [noir.io :as io] [markdown.core :as md])) -(defn md->html - "reads a markdown file from public/md and returns an HTML string" - [filename] - (->> - (io/slurp-resource filename) - (md/md-to-html-string))) +; Global stuff that does not change over time and needed everywhere +(def ^:dynamic *base-url* "http://www.ludumdare.com/compo/") +(def ^:dynamic *image-base-path* "content/images") + +(def ^:dynamic *competition* 27)