From 3d4fc79a1fa116fbf40770b1baa27082b9b32e1c Mon Sep 17 00:00:00 2001 From: Aaron Mueller Date: Mon, 30 Sep 2013 00:37:38 +0200 Subject: [PATCH] DAmn db stuff .. switch to sqlite and finish up the persistence stuff --- project.clj | 3 ++- src/ldview/handler.clj | 8 +++--- src/ldview/models/competition.clj | 8 ++++++ src/ldview/models/db.clj | 20 +------------- src/ldview/models/entity.clj | 25 ----------------- src/ldview/models/entry.clj | 32 ++++++++++++++++++++++ src/ldview/models/schema.clj | 35 +++++++++++------------- src/ldview/models/source.clj | 10 +++++++ src/ldview/routes/home.clj | 6 ++--- src/ldview/tasks/images.clj | 6 +++-- src/ldview/tasks/runner.clj | 36 ++++++++++++++----------- src/ldview/tasks/scrape.clj | 2 -- src/ldview/views/templates/entries.html | 1 + 13 files changed, 102 insertions(+), 90 deletions(-) delete mode 100644 src/ldview/models/entity.clj create mode 100644 src/ldview/models/entry.clj create mode 100644 src/ldview/models/source.clj diff --git a/project.clj b/project.clj index 79ccf88..ac3d35e 100644 --- a/project.clj +++ b/project.clj @@ -10,7 +10,8 @@ [com.taoensso/timbre "2.6.2"] [com.postspectacular/rotor "0.1.0"] [com.taoensso/tower "1.7.1"] - [com.h2database/h2 "1.3.172"] +; [com.h2database/h2 "1.3.172"] + [sqlitejdbc "0.5.6"] [korma "0.3.0-RC5"] [log4j "1.2.17" diff --git a/src/ldview/handler.clj b/src/ldview/handler.clj index 5157ccd..95a6f50 100644 --- a/src/ldview/handler.clj +++ b/src/ldview/handler.clj @@ -1,5 +1,5 @@ -(ns ldview.handler - (:require [compojure.core :refer [defroutes]] +(ns ldview.handler + (:require [compojure.core :refer [defroutes]] [ldview.routes.home :refer [home-routes]] [noir.util.middleware :as middleware] [compojure.route :as route] @@ -24,13 +24,13 @@ :async? false ; should be always false for rotor :max-message-per-msecs nil :fn rotor/append}) - + (timbre/set-config! [:shared-appender-config :rotor] {:path "ldview.log" :max-size (* 512 1024) :backlog 10}) (if-not (schema/initialized?) (schema/create-tables)) - + (timbre/info "ldview started successfully")) (defn destroy diff --git a/src/ldview/models/competition.clj b/src/ldview/models/competition.clj index 9ad9c51..507b68d 100644 --- a/src/ldview/models/competition.clj +++ b/src/ldview/models/competition.clj @@ -7,3 +7,11 @@ (first (select competitions (where {:number *competition*}) (limit 1)))) + +(defn exists? [number] + (not (empty? (select competitions + (fields :id) + (where {:number number}))))) + +(defn create! [number motto] + (insert competitions (values {:number number :motto motto}))) diff --git a/src/ldview/models/db.clj b/src/ldview/models/db.clj index 7803723..dbc5398 100644 --- a/src/ldview/models/db.clj +++ b/src/ldview/models/db.clj @@ -12,7 +12,7 @@ (belongs-to users) (has-many sources)) -(defentity contests +(defentity competitions (has-many entries)) (defentity users @@ -21,21 +21,3 @@ (defentity sources (belongs-to entities)) - - - -; (defn create-entry [entry] -; (insert entries -; (values entry))) -; -; (defn update-entry [id id_uid title description user_id contest_id] -; (update entries -; (set-fields {:first_name first-name -; :last_name last-name -; :email email}) -; (where {:id id}))) -; -; (defn get-user [id] -; (first (select users -; (where {:id id}) -; (limit 1)))) diff --git a/src/ldview/models/entity.clj b/src/ldview/models/entity.clj deleted file mode 100644 index fc3d492..0000000 --- a/src/ldview/models/entity.clj +++ /dev/null @@ -1,25 +0,0 @@ -(ns ldview.models.entity - (:use korma.core - [ldview.models.db]) - (:require [ldview.models.user :as user])) - -(defn all [] - (select entries)) - -(defn exists? [ld-uid] - (not (empty? (select entities - (fields :id) - (where {:ld_uid ld-uid}) - (limit 1))))) - -(defn create! [fields] - (let [user (user/unique-user (:user fields)) - competition-id (competition/latest)] - (if-not (exists? (:ld_uid fields)) - (insert entries (values { - :ld_uid (:ld_uid fields) - :title (:title fields) - :description (:description fields) - :user_id (:id user) - :competition_id (:id competition)}))))) - diff --git a/src/ldview/models/entry.clj b/src/ldview/models/entry.clj new file mode 100644 index 0000000..39667e3 --- /dev/null +++ b/src/ldview/models/entry.clj @@ -0,0 +1,32 @@ +(ns ldview.models.entry + (:use korma.core + [ldview.models.db]) + (:require [ldview.models.user :as user] + [ldview.models.competition :as competition] + [ldview.models.source :as source])) + +(defn all [] + (select entries)) + +(defn by-ld-uid [ld-uid] + (first (select entries + (where {:ld_uid ld-uid})))) + +(defn exists? [ld-uid] + (not (empty? (select entries + (fields :id) + (where {:ld_uid ld-uid}) + (limit 1))))) + +(defn create! [fields] + (let [username (user/unique-user (:author fields)) + competition-id (:id (competition/latest))] + (if-not (exists? (:ld_uid fields)) + (insert entries (values { + :ld_uid (:ld_uid fields) + :title (:title fields) + :description (:description fields) + :user_id (:id username) + :competition_id competition-id}))) + (for [link (:links fields)] + (source/create! (:id (by-ld-uid (:ld_uid fields))) (:url link) (:title link))))) diff --git a/src/ldview/models/schema.clj b/src/ldview/models/schema.clj index 3b3e0cc..62661eb 100644 --- a/src/ldview/models/schema.clj +++ b/src/ldview/models/schema.clj @@ -4,40 +4,36 @@ (def db-store "site.db") -(def db-spec {:classname "org.h2.Driver" - :subprotocol "h2" - :subname (str (io/resource-path) db-store) - :user "sa" - :password "" - :naming {:keys clojure.string/lower-case - :fields clojure.string/upper-case}}) +(def db-spec {:classname "org.sqlite.JDBC" + :subprotocol "sqlite" + :subname (str (io/resource-path) db-store)}) (defn initialized? [] - (.exists (new java.io.File (str (io/resource-path) db-store ".h2.db")))) + (.exists (new java.io.File (str (io/resource-path) db-store)))) (defn create-initial-tables [] (sql/with-connection db-spec (sql/create-table :entries - [:id "INT PRIMARY KEY AUTO_INCREMENT"] - [:ld_uid "INT"] + [:id "INTEGER PRIMARY KEY AUTOINCREMENT"] + [:ld_uid "INTEGER"] [:title "VARCHAR(255)"] [:description "TEXT"] - [:user_id "INT"] - [:competition_id "INT"]) + [:user_id "INTEGER"] + [:competition_id "INTEGER"]) (sql/create-table :users - [:id "INT PRIMARY KEY AUTO_INCREMENT"] + [:id "INTEGER PRIMARY KEY AUTOINCREMENT"] [:name "VARCHAR(255)"]) (sql/create-table :competitions - [:id "INT PRIMARY KEY AUTO_INCREMENT"] - [:number "INT"] - [:name "VARCHAR(255)"]) + [:id "INTEGER PRIMARY KEY AUTOINCREMENT"] + [:number "INTEGER"] + [:motto "VARCHAR(255)"]) (sql/create-table :sources - [:id "INT PRIMARY KEY AUTO_INCREMENT"] - [:entry_id "INT"] + [:id "INTEGER PRIMARY KEY AUTOINCREMENT"] + [:entry_id "INTEGER"] [:type "VARCHAR(100)"] [:uri "VARCHAR(255)"]) @@ -45,4 +41,5 @@ (sql/do-commands "CREATE INDEX entry_id_index ON sources (entry_id)"))) (defn create-tables [] - (create-initial-tables)) + (if-not (initialized?) + (create-initial-tables))) diff --git a/src/ldview/models/source.clj b/src/ldview/models/source.clj new file mode 100644 index 0000000..d6d5c80 --- /dev/null +++ b/src/ldview/models/source.clj @@ -0,0 +1,10 @@ +(ns ldview.models.source + (:use korma.core + [ldview.models.db])) + +(defn create! [entry_id uri description] + (if (empty? (select sources (where {:entry_id entry_id :type description}))) + (insert sources (values {:entry_id entry_id + :uri uri + :type description})))) + diff --git a/src/ldview/routes/home.clj b/src/ldview/routes/home.clj index 0f0ec1d..d86d7f3 100644 --- a/src/ldview/routes/home.clj +++ b/src/ldview/routes/home.clj @@ -1,11 +1,11 @@ (ns ldview.routes.home (:use compojure.core) (:require [ldview.views.layout :as layout] - [ldview.util :as util])) + [ldview.util :as util] + [ldview.models.entry :as entry])) (defn entries-page [] - (layout/render - "entries.html")) + (layout/render "entries.html" {:entries (entry/all)})) (defroutes home-routes (GET "/" [] (entries-page))) diff --git a/src/ldview/tasks/images.clj b/src/ldview/tasks/images.clj index b28ba8b..a61204d 100644 --- a/src/ldview/tasks/images.clj +++ b/src/ldview/tasks/images.clj @@ -22,8 +22,10 @@ (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)) + (with-redefs [image-resizer.fs/new-filename (fn [filepath dimensions] (str filepath))] + (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)) + (with-redefs [image-resizer.fs/new-filename (fn [filepath dimensions] (str filepath))] + (as-file (resize (io/file image-path) 800 600) new-image-path))) diff --git a/src/ldview/tasks/runner.clj b/src/ldview/tasks/runner.clj index ad065d4..871ba29 100644 --- a/src/ldview/tasks/runner.clj +++ b/src/ldview/tasks/runner.clj @@ -1,7 +1,10 @@ (ns ldview.tasks.runner (:require [ldview.tasks.scrape :as scrape] [ldview.tasks.images :as images] - [me.raynes.fs :as fs]) + [me.raynes.fs :as fs] + [ldview.models.entry :as entry] + [ldview.models.competition :as competition] + [ldview.models.schema :as schema]) (:use [ldview.util])) (defn cleanup [] @@ -11,27 +14,30 @@ (fs/mkdirs (str *image-base-path* "/fullscreen/")) (fs/mkdirs (str *image-base-path* "/raw/"))) -(defn save-entry [entry] - ; TODO: Save to Database - (if (:images entry) +(defn save-entry [new-entry] + (entry/create! new-entry) + (if (:images new-entry) (map (fn [image-url] - (let [id (:id entry) + (let [id (:ld_uid new-entry) number (last (first (re-seq #"shot([0-9]+)" image-url))) - raw-image-path (images/image-name "raw" number)] + raw-image-path (images/image-name "raw" id number)] (scrape/save-image-from-url image-url raw-image-path) - (images/sourceimage->fullscreen raw-image-path (images/image-name "fullscreen" number)) - (images/sourceimage->thumb raw-image-path (images/image-name "thumbs" number)))) - (:images entry)))) + (images/sourceimage->fullscreen raw-image-path (images/image-name "fullscreen" id number)) + (images/sourceimage->thumb raw-image-path (images/image-name "thumbs" id number)))) + (:images new-entry)))) -; TODO: Make it multithreaded (defn fetch-all-content [] - (let [pages 1] ;(scrape/number-of-pages)] - (map (fn [page] - (map save-entry (scrape/fetch-page 1))) - (range pages)))) + (let [pages (range 1)] ;(scrape/number-of-pages)] + (for [page pages] + (for [ld-uid (scrape/entries-on-page (inc page))] + (if-not (entry/exists? ld-uid) + (save-entry (scrape/entry-details ld-uid))))))) +(defn prepare [] + (cleanup) + (schema/create-tables) + (competition/create! 27 "10 Seconds")) (defn start [] - (cleanup) (fetch-all-content)) diff --git a/src/ldview/tasks/scrape.clj b/src/ldview/tasks/scrape.clj index 11b53c1..e3becc7 100644 --- a/src/ldview/tasks/scrape.clj +++ b/src/ldview/tasks/scrape.clj @@ -58,5 +58,3 @@ :links links :images images})) -(defn fetch-page [page] - (map entry-details (entries-on-page page))) diff --git a/src/ldview/views/templates/entries.html b/src/ldview/views/templates/entries.html index 931ac22..e6d5611 100644 --- a/src/ldview/views/templates/entries.html +++ b/src/ldview/views/templates/entries.html @@ -1,5 +1,6 @@ {% extends "ldview/views/templates/layout.html" %} {% block content %} +
entry-#
entry-#
entry-#