From de29f05c904ef4cba83d38433af2f37600c9b226 Mon Sep 17 00:00:00 2001 From: rdiedrich Date: Tue, 23 Jun 2020 16:12:46 +0200 Subject: [PATCH] bookmarks --- src/clojsa/handler.clj | 22 +++++++++++++++++++--- src/clojsa/saclient.clj | 20 ++++++++++++++++++++ src/clojsa/saparser.clj | 25 +++++++++++++++++++++++++ src/clojsa/views.clj | 28 +++++++++++++++++++++------- 4 files changed, 85 insertions(+), 10 deletions(-) diff --git a/src/clojsa/handler.clj b/src/clojsa/handler.clj index 2e897b4..81fd306 100644 --- a/src/clojsa/handler.clj +++ b/src/clojsa/handler.clj @@ -15,23 +15,39 @@ tresp (client/thread-response session turl)] (parser/thread-map id page tresp))) -(defroutes app-routes +(defn get-bookmarks [session page] + (let [burl (client/bookmarks-url page) + bresp (client/bookmarks-response session burl)] + (parser/bookmarks-map page bresp))) +(defroutes app-routes (GET "/" request (views/index-page request)) (GET "/login" [] (views/login-page)) + (POST "/login" [username password :as {session :session}] (let [resp-cookies (client/login-response username password session)] - {:headers {"Content-Type" "text/plain; charset=utf-8"} - :body (with-out-str (clojure.pprint/pprint resp-cookies)) + {:status 302 + :headers {"Location" "/bookmarks"} :session (assoc session :cookies resp-cookies)})) + (GET "/bookmarks" [page :<< as-int + :as {session :session}] + (let [beems (get-bookmarks session page)] + (views/bookmarks-page beems))) + + (GET "/bookmarks" [] + {:status 302 :headers {"Location" "/bookmarks?page=1"}}) + (GET "/thread/:id" [id :<< as-int page :<< as-int :as {session :session}] (let [thread (get-thread session id page)] (views/thread-page thread))) + (GET "/thread/:id" [id] + {:status 302 :headers {"Location" (str "/thread/" id "?page=1")}}) + (route/not-found "Not Found")) (def app diff --git a/src/clojsa/saclient.clj b/src/clojsa/saclient.clj index a89482e..46a4ca9 100644 --- a/src/clojsa/saclient.clj +++ b/src/clojsa/saclient.clj @@ -14,6 +14,15 @@ :pagenumber page}] {:href base-url :params query}))) +(defn bookmarks-url + ([] + (bookmarks-url 1)) + ([page] + (let [base-url (str url "bookmarkthreads.php") + query {:pagenumber page}] + {:href base-url :params query}))) + + (defn login-response [username password session] (with-open [client (http/create-client)] (let [login-url (str url "account.php") @@ -27,6 +36,17 @@ http/await http/cookies)))) +(defn bookmarks-response [session url] + (with-open [client (http/create-client)] + (let [{:keys [href params]} url + req-cookies (:cookies session) + resp (http/GET client href :query params :cookies req-cookies) + status (http/status resp) + headers (http/headers resp)] + (-> resp + http/await + http/string)))) + (defn thread-response [session url] (with-open [client (http/create-client)] (let [{:keys [href params]} url diff --git a/src/clojsa/saparser.clj b/src/clojsa/saparser.clj index 091f67b..f1f3f80 100644 --- a/src/clojsa/saparser.clj +++ b/src/clojsa/saparser.clj @@ -62,3 +62,28 @@ :when (not= "Adbot" (-> (s/select (s/child (s/class :author)) ui) first :content first))] {:ui (parse-ui ui) :pd (parse-pd pd) :pb (parse-pb pb)})})) + +(defn parse-bookmarks [htree] + (s/select (s/descendant + (s/id :forum) + (s/tag :tbody) + (s/tag :tr)) + htree)) + +(defn parse-row [htree] + (let [link (-> (s/select (s/descendant (s/class :info) (s/tag :a)) htree) first) + thread-id (re-find #"\d+$" (:href (:attrs link))) + title (-> link :content first string/trim)] + {:id thread-id :title title})) + +(defn bookmarks-map [page doc] + (let [htree (hickory-doc doc) + title (parse-title htree) + page-count (parse-pagecount htree) + bookmarks-tree (parse-bookmarks htree)] + {:title title + :page page + :page-count page-count + :content + (for [row bookmarks-tree] + (parse-row row))})) diff --git a/src/clojsa/views.clj b/src/clojsa/views.clj index 906b183..bba9eed 100644 --- a/src/clojsa/views.clj +++ b/src/clojsa/views.clj @@ -57,28 +57,28 @@ [:div.control [:button.button.is-primary "Submit"]]]]])) -(defn paginate [id cur last] +(defn paginate [base cur last] [:nav.container.box.pagination {:hx-boot "false"} [:a.pagination-previous - {:href (format "/thread/%d?page=%d" id (dec cur))} "<"] + {:href (format "%s?page=%d" base (dec cur))} "<"] [:a.pagination-next - {:href (format "/thread/%s?page=%d" id (inc cur))} ">"] + {:href (format "%s?page=%d" base (inc cur))} ">"] [:ul.pagination-list [:li [:a.pagination-link - {:href (format "/thread/%d?page=%d" id 1)} (str 1)]] + {:href (format "%s?page=%d" base 1)} (str 1)]] [:li [:span.pagination-ellipsis "…"]] (for [i (range (- cur 2) (+ cur 3))] [:li [:a.pagination-link - {:href (format "/thread/%d?page=%d" id i) + {:href (format "%s?page=%d" base i) :class (when (= i cur) "is-current")} (str i)]]) [:li [:span.pagination-ellipsis "…"]] [:li [:a.pagination-link - {:href (format "/thread/%d?page=%d" id last)} (str last)]]]]) + {:href (format "%s?page=%d" base last)} (str last)]]]]) (defn thread-page [thread] (let [{:keys [id title page page-count content]} thread] @@ -100,4 +100,18 @@ [:span.postdate.level-item (:pd post)]]]]]])] [:section - (paginate id page page-count)]))) + (paginate (str "/thread/" id) page page-count)]))) + +(defn bookmarks-page [beems] + (let [{:keys [title page page-count content]} beems] + (main-template + {:title title} + [:div.container + [:h1.is-size-3.mb-4 title]] + [:section.bookmarks + [:nav.container.box.panel + (for [bm content] + [:a.panel-block {:href (str "/thread/" (:id bm))} + (:title bm)])]] + [:section + (paginate (str "/bookmarks") page page-count)])))