diff --git a/src/ldview/models/competition.clj b/src/ldview/models/competition.clj index 3b42f77..a08614b 100644 --- a/src/ldview/models/competition.clj +++ b/src/ldview/models/competition.clj @@ -14,4 +14,6 @@ (where {:number number}))))) (defn create! [number motto] - (insert competitions (values {:number number :motto motto}))) + (if-not (exists? number) + (insert competitions (values {:number number + :motto motto})))) diff --git a/src/ldview/tasks/runner.clj b/src/ldview/tasks/runner.clj index f5c059a..8a9072f 100644 --- a/src/ldview/tasks/runner.clj +++ b/src/ldview/tasks/runner.clj @@ -6,14 +6,18 @@ [ldview.models.competition :as competition] [ldview.models.schema :as schema])) -(defn cleanup [] +;; TODO: This is ugly and not the right way to do. This should be cleaned up +;; so the functions does not have side effects +(def competition (atom 0)) + +(defn cleanup! [] (if (fs/exists? images/base-path) (fs/delete-dir images/base-path)) (fs/mkdirs (str images/base-path "/thumbs/")) (fs/mkdirs (str images/base-path "/fullscreen/")) (fs/mkdirs (str images/base-path "/raw/"))) -(defn save-entry [new-entry] +(defn save-entry! [new-entry] (entry/create! new-entry) (if (:images new-entry) (map (fn [image-url] @@ -32,9 +36,8 @@ (if-not (entry/exists? ld-uid) (save-entry (scrape/entry-details ld-uid))))))) -; TODO: Watch out, the competition is set in the scrape .... refactor this ... -; TODO: Only create the competition if not allready exists -(defn load-competition [id title] - (competition/create! id title) +(defn load-competition [id] + (swap! competition id) + (competition/create! id (scrape/theme)) (fetch-all-content)) diff --git a/src/ldview/tasks/scrape.clj b/src/ldview/tasks/scrape.clj index 451a72e..5c7da4c 100644 --- a/src/ldview/tasks/scrape.clj +++ b/src/ldview/tasks/scrape.clj @@ -2,12 +2,8 @@ (:require [net.cgrand.enlive-html :as html] [clj-http.client :as http] [clojure.java.io :as io]) - (:use [clojure.string :only (split)])) - -; Some helper functions to construct proper urls. If the Ludum Date Website -; changes some URL stuff, this is the place to crank. -; TODO: make this dynamic and replace all places in the code -(def competition 27) + (:use [clojure.string :only (split)] + [ldview.tasks.runner :only (@competition)])) (defn url-action [action] (str "http://www.ludumdare.com/compo/ludum-dare-" competition "/?action=" action)) @@ -40,7 +36,7 @@ links (map #(:href %1) (map #(:attrs (first (html/select [%1] [:a]))) tds))] (map #(last (split %1 #"=")) links))) -(defn competition-theme [competition] +(defn theme [] (let [p (html/select (fetch-url (url-action "preview")) [:div#content :> :div.post :> :div.entry :> :p html/first-child :a])] (first (:content (first p)))))