aboutsummaryrefslogtreecommitdiff
path: root/cljssss-g.clj
blob: 04977a9615fdbd7c6ed7079670c86c76e89b902e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
(ns cljssss-g
  (require [clojure.xml :as xml]
           [clojure.contrib.sql :as sql]
           compojure)
  (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")

(def templates (new StringTemplateGroup ""))

(defservlet cljssss-g
  (GET "/"
    (.toString
     (doto (.getInstanceOf templates "index")
       (.setAttributes {"title" "Subscriptions",
                        "mainParagraph" "Hi there!"}))))
  (ANY "*"
    (page-not-found)))

(def db-connection-data {:classname   "org.sqlite.JDBC"
                         :subprotocol "sqlite"
                         :subname     "cljssss-g.sqlite3"
                         :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-dbt
    (sql/create-table :user
                      [:id "integer" "PRIMARY KEY"]
                      [:name "text"]
                      [:email "text"]
                      [:password "text"]))

  (with-dbt
    (sql/create-table :feed
                      [:id "integer" "PRIMARY KEY"]
                      [:uri "text"]
                      [:language "text"]
                      [:iri "text"]
                      [:icon "blob"]
                      [:link "text"]
                      [:logo "text"]
                      [:rights "text"]
                      [:title "text"]
                      [:subtitle "text"]
                      [:updated "date"]))

  (with-dbt
    (sql/create-table :entry
                      [:id "integer" "PRIMARY KEY"]
                      [:uri "text"]  ;?
                      [:language "text"]
                      [:content "blob"]
                      [:content_type "text"]
                      [:iri "text"]
                      [:link "text"]
                      [:published "date"]
                      [:rights "text"]
                      [:source "integer"] ;:feed
                      [:title "text"]
                      [:summary "blob"]
                      [:summary_type "text"]
                      [:updated "date"]))

  (with-dbt
    (sql/create-table :feed_entry_link
                      [:feed "integer"]
                      [:entry "integer"]))

  (with-dbt
    (sql/create-table :user_feed_link   ;subscription
                      [:user "integer"]
                      [:feed "integer"]
                      [:title "text"]))

  (with-dbt
    (sql/create-table :user_entry_link
                      [:user "integer"]
                      [:entry "integer"]
                      [:read "boolean"]
                      [:marked "boolean"]
                      [:hidden "boolean"])))