Rearrange some stuff and start implementing the image processing

This commit is contained in:
Aaron Mueller 2013-09-28 14:15:22 +02:00
parent 7ee90ee2ad
commit 34386973f6
4 changed files with 59 additions and 16 deletions

View file

@ -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 {

View file

@ -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))

View file

@ -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)))

View file

@ -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)