From 179a03867f28b092527d84f6d9cd62ef04fd2fac Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Thu, 19 Feb 2009 21:23:27 +0100 Subject: Add function fetch-feed. --- INSTALL | 4 ++- cljssss-g.clj | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 93 insertions(+), 13 deletions(-) diff --git a/INSTALL b/INSTALL index 930c6d6..566f5f6 100644 --- a/INSTALL +++ b/INSTALL @@ -3,7 +3,7 @@ #title CljSSSS^g Quick Start Guide #desc The CljSSSS^g installation guide -; Time-stamp: <2009-02-19 11:57:16 mulk> +; Time-stamp: <2009-02-19 17:35:37 mulk> ; ; C-c i t insert-time-stamp ; C-c C-t muse-project-publish-this-file @@ -30,6 +30,8 @@ The following dependencies need to be on your class path: - StringTemplate along with ANTLR + - ROME along with JDOM + - SQLiteJDBC diff --git a/cljssss-g.clj b/cljssss-g.clj index 074e6ab..04977a9 100644 --- a/cljssss-g.clj +++ b/cljssss-g.clj @@ -2,7 +2,10 @@ (require [clojure.xml :as xml] [clojure.contrib.sql :as sql] compojure) - (import (org.antlr.stringtemplate StringTemplateGroup)) + (import (org.antlr.stringtemplate StringTemplateGroup) + (com.sun.syndication.io SyndFeedInput XmlReader) + (com.sun.syndication.feed.synd SyndFeed SyndEntry) + (java.net URL)) (use compojure)) (Class/forName "org.sqlite.JDBC") @@ -18,28 +21,102 @@ (ANY "*" (page-not-found))) -(defmacro with-db [& body] - `(sql/with-connection {:classname "org.sqlite.JDBC" +(def db-connection-data {:classname "org.sqlite.JDBC" :subprotocol "sqlite" :subname "cljssss-g.sqlite3" - :create true} + :create true}) + +(defmacro with-db [& body] + `(sql/with-connection db-connection-data + ~@body)) + +(defmacro with-dbt [& body] + `(with-db (sql/transaction ~@body))) +(defn fetch-feed [id] + (with-db + (sql/with-query-results [{uri :uri}] + ["SELECT uri FROM feed WHERE id = ?" id] + (let [feed #^SyndFeed (.build (new SyndFeedInput) + (new XmlReader (new URL uri)))] + (sql/transaction + (sql/update-or-insert-values :feed + ["id = ?" id] + {:id id + :uri uri + :language (.getLanguage feed) + :iri (.getURI feed) + :link (.getLink feed) + :rights (.trim (.getCopyright feed)) + :title (.trim (.getTitle feed)) + :subtitle (.trim (.getDescription feed)) + :updated (.getPublishedDate feed)}) + (doseq [entry #^SyndEntry (.getEntries feed)] + (sql/with-query-results [{potential-entry-id :id}] + ["SELECT id FROM entry WHERE iri = ?" (.getURI entry)] + (let [entry-id + (or potential-entry-id + (+ 1 + (sql/with-query-results max-id + ["SELECT MAX(id) FROM entry"] + (or max-id -1))))] + (sql/update-or-insert-values :entry + ["id = ?" entry-id] + {:id entry-id + :language (.getLanguage entry) + :iri (.getURI entry) + :link (.getLink entry) + :rights (.trim (.getCopyright entry)) + :title (.trim (.getTitle entry)) + :summary_type (if (.getDescription entry) + (.getType (first (.getDescription entry))) + nil) + :summary (if (.getDescription entry) + (.getValue (first (.getDescription entry))) + nil) + :content_type (if (.getContents entry) + (.getType (first (.getContents entry))) + nil) + :content (if (.getContents entry) + (.getValue (first (.getContents entry))) + nil) + :updated (.getUpdatedDate entry) + :published (.getPublishedDate entry)}) + (sql/update-or-insert-values :feed_entry_link + ["feed = ?, entry = ?" id entry-id] + {:feed id + :entry entry-id}))))))))) + + (run-server {:port 8080} "/*" cljssss-g) +;;;; Sample database content +(comment + (with-dbt + (sql/update-or-insert-values :feed + ["id = ?" 0] + {:id 0 + :uri "http://matthias.benkard.de/journal/feed/"}) + (sql/update-or-insert-values :feed + ["id = ?" 1] + {:id 1 + :uri "http://uxul.wordpress.com/feed/"}))) + + ;;;; Database schema (comment - (with-db + (with-dbt (sql/create-table :user [:id "integer" "PRIMARY KEY"] [:name "text"] [:email "text"] [:password "text"])) - (with-db + (with-dbt (sql/create-table :feed [:id "integer" "PRIMARY KEY"] [:uri "text"] @@ -53,10 +130,10 @@ [:subtitle "text"] [:updated "date"])) - (with-db + (with-dbt (sql/create-table :entry [:id "integer" "PRIMARY KEY"] - [:uri "text"] + [:uri "text"] ;? [:language "text"] [:content "blob"] [:content_type "text"] @@ -66,21 +143,22 @@ [:rights "text"] [:source "integer"] ;:feed [:title "text"] - [:summary "text"] + [:summary "blob"] + [:summary_type "text"] [:updated "date"])) - (with-db + (with-dbt (sql/create-table :feed_entry_link [:feed "integer"] [:entry "integer"])) - (with-db + (with-dbt (sql/create-table :user_feed_link ;subscription [:user "integer"] [:feed "integer"] [:title "text"])) - (with-db + (with-dbt (sql/create-table :user_entry_link [:user "integer"] [:entry "integer"] -- cgit v1.2.3