bookmarks

This commit is contained in:
rdiedrich 2020-06-23 16:12:46 +02:00
parent 1ea948dc27
commit de29f05c90
4 changed files with 85 additions and 10 deletions

View File

@ -15,23 +15,39 @@
tresp (client/thread-response session turl)] tresp (client/thread-response session turl)]
(parser/thread-map id page tresp))) (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 "/" request (views/index-page request))
(GET "/login" [] (views/login-page)) (GET "/login" [] (views/login-page))
(POST "/login" [username password :as {session :session}] (POST "/login" [username password :as {session :session}]
(let [resp-cookies (client/login-response username password session)] (let [resp-cookies (client/login-response username password session)]
{:headers {"Content-Type" "text/plain; charset=utf-8"} {:status 302
:body (with-out-str (clojure.pprint/pprint resp-cookies)) :headers {"Location" "/bookmarks"}
:session (assoc session :cookies resp-cookies)})) :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 (GET "/thread/:id" [id :<< as-int
page :<< as-int page :<< as-int
:as {session :session}] :as {session :session}]
(let [thread (get-thread session id page)] (let [thread (get-thread session id page)]
(views/thread-page thread))) (views/thread-page thread)))
(GET "/thread/:id" [id]
{:status 302 :headers {"Location" (str "/thread/" id "?page=1")}})
(route/not-found "Not Found")) (route/not-found "Not Found"))
(def app (def app

View File

@ -14,6 +14,15 @@
:pagenumber page}] :pagenumber page}]
{:href base-url :params query}))) {: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] (defn login-response [username password session]
(with-open [client (http/create-client)] (with-open [client (http/create-client)]
(let [login-url (str url "account.php") (let [login-url (str url "account.php")
@ -27,6 +36,17 @@
http/await http/await
http/cookies)))) 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] (defn thread-response [session url]
(with-open [client (http/create-client)] (with-open [client (http/create-client)]
(let [{:keys [href params]} url (let [{:keys [href params]} url

View File

@ -62,3 +62,28 @@
:when (not= "Adbot" (-> (s/select (s/child (s/class :author)) ui) :when (not= "Adbot" (-> (s/select (s/child (s/class :author)) ui)
first :content first))] first :content first))]
{:ui (parse-ui ui) :pd (parse-pd pd) :pb (parse-pb pb)})})) {: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))}))

View File

@ -57,28 +57,28 @@
[:div.control [:div.control
[:button.button.is-primary "Submit"]]]]])) [:button.button.is-primary "Submit"]]]]]))
(defn paginate [id cur last] (defn paginate [base cur last]
[:nav.container.box.pagination {:hx-boot "false"} [:nav.container.box.pagination {:hx-boot "false"}
[:a.pagination-previous [:a.pagination-previous
{:href (format "/thread/%d?page=%d" id (dec cur))} "<"] {:href (format "%s?page=%d" base (dec cur))} "<"]
[:a.pagination-next [:a.pagination-next
{:href (format "/thread/%s?page=%d" id (inc cur))} ">"] {:href (format "%s?page=%d" base (inc cur))} ">"]
[:ul.pagination-list [:ul.pagination-list
[:li [:li
[:a.pagination-link [:a.pagination-link
{:href (format "/thread/%d?page=%d" id 1)} (str 1)]] {:href (format "%s?page=%d" base 1)} (str 1)]]
[:li [:li
[:span.pagination-ellipsis "&hellip;"]] [:span.pagination-ellipsis "&hellip;"]]
(for [i (range (- cur 2) (+ cur 3))] (for [i (range (- cur 2) (+ cur 3))]
[:li [:li
[:a.pagination-link [: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)]]) :class (when (= i cur) "is-current")} (str i)]])
[:li [:li
[:span.pagination-ellipsis "&hellip;"]] [:span.pagination-ellipsis "&hellip;"]]
[:li [:li
[:a.pagination-link [: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] (defn thread-page [thread]
(let [{:keys [id title page page-count content]} thread] (let [{:keys [id title page page-count content]} thread]
@ -100,4 +100,18 @@
[:span.postdate.level-item [:span.postdate.level-item
(:pd post)]]]]]])] (:pd post)]]]]]])]
[:section [: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)])))