Rearrange some stuff and start implementing the image processing
This commit is contained in:
parent
7ee90ee2ad
commit
34386973f6
4 changed files with 59 additions and 16 deletions
|
@ -7,8 +7,7 @@
|
||||||
#container {
|
#container {
|
||||||
margin: auto;
|
margin: auto;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
border-left: 2px solid gray;
|
padding: 0 20px;
|
||||||
border-right: 2px solid gray;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
div.entry {
|
div.entry {
|
||||||
|
|
29
src/ldview/tasks/images.clj
Normal file
29
src/ldview/tasks/images.clj
Normal 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))
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
(ns ldview.ldscrape
|
(ns ldview.tasks.scrape
|
||||||
(:require [net.cgrand.enlive-html :as html])
|
(:require [net.cgrand.enlive-html :as html]
|
||||||
(:use [clojure.string :only (split)]))
|
[clj-http.client :as http]
|
||||||
|
[clojure.java.io :as io]
|
||||||
(def ^:dynamic *base-url* "http://www.ludumdare.com/compo/")
|
(:use [clojure.string :only (split)]
|
||||||
(def ^:dynamic *competition* 27)
|
[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]
|
(defn url-action [action]
|
||||||
(str *base-url* "/ludum-dare-" *competition* "/?action=" action))
|
(str *base-url* "/ludum-dare-" *competition* "/?action=" action))
|
||||||
|
|
||||||
|
@ -14,10 +16,19 @@
|
||||||
(defn url-entry [entry-id]
|
(defn url-entry [entry-id]
|
||||||
(str (url-action "preview") "&uid=" 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]
|
(defn fetch-url [url]
|
||||||
(html/html-resource (java.net.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 []
|
(defn number-of-pages []
|
||||||
(let [p (second (html/select (fetch-url (url-action "preview")) [:div#compo2 :> :p]))]
|
(let [p (second (html/select (fetch-url (url-action "preview")) [:div#compo2 :> :p]))]
|
||||||
(read-string (html/text (last (butlast (html/select [p] [:a])))))))
|
(read-string (html/text (last (butlast (html/select [p] [:a])))))))
|
||||||
|
@ -46,5 +57,10 @@
|
||||||
:links links
|
:links links
|
||||||
:images images}))
|
:images images}))
|
||||||
|
|
||||||
|
; Do the job
|
||||||
|
; TODO: Make it multithreaded and resumable.
|
||||||
(defn fetch-page [page]
|
(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)))
|
|
@ -2,9 +2,8 @@
|
||||||
(:require [noir.io :as io]
|
(:require [noir.io :as io]
|
||||||
[markdown.core :as md]))
|
[markdown.core :as md]))
|
||||||
|
|
||||||
(defn md->html
|
; Global stuff that does not change over time and needed everywhere
|
||||||
"reads a markdown file from public/md and returns an HTML string"
|
(def ^:dynamic *base-url* "http://www.ludumdare.com/compo/")
|
||||||
[filename]
|
(def ^:dynamic *image-base-path* "content/images")
|
||||||
(->>
|
|
||||||
(io/slurp-resource filename)
|
(def ^:dynamic *competition* 27)
|
||||||
(md/md-to-html-string)))
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue