work branch
This commit is contained in:
parent
97a1125695
commit
05e84bab61
@ -5,7 +5,7 @@
|
||||
:dependencies [[org.clojure/clojure "1.10.0"]
|
||||
[compojure "1.6.1"]
|
||||
[ring/ring-defaults "0.3.2"]
|
||||
[http.async.client "1.3.1"]
|
||||
[clj-http "3.10.1"]
|
||||
[hickory "0.7.1"]]
|
||||
:plugins [[lein-ring "0.12.5"]]
|
||||
:ring {:handler clojsa.handler/app}
|
||||
|
@ -3,3 +3,9 @@ document.body.addEventListener('configRequest.htmx', (e) => {
|
||||
e.detail.parameters['__anti-forgery-token'] = __csrfToken;
|
||||
}
|
||||
});
|
||||
|
||||
document.querySelectorAll('.bbc-spoiler').forEach(elem => {
|
||||
elem.addEventListener('click', e => {
|
||||
e.target.classList.toggle('active');
|
||||
});
|
||||
});
|
||||
|
@ -1,7 +1,7 @@
|
||||
(ns clojsa.handler
|
||||
(:require [clojsa.views :as views]
|
||||
(:require [clojsa.saparser :as parser]
|
||||
[clojsa.saclient :as client]
|
||||
[clojsa.saparser :as parser]
|
||||
[clojsa.views :as views]
|
||||
[compojure.core :refer :all]
|
||||
[compojure.route :as route]
|
||||
[compojure.coercions :refer [as-int]]
|
||||
@ -9,23 +9,31 @@
|
||||
[ring.middleware.session :refer [wrap-session]]
|
||||
[ring.middleware.session.cookie :refer (cookie-store)]))
|
||||
|
||||
|
||||
(defn get-thread [session id page]
|
||||
(let [turl (client/thread-url id page)
|
||||
tresp (client/thread-response session turl)]
|
||||
(parser/thread-map id page tresp)))
|
||||
(def session-cookie-store (cookie-store {:key "b91c62d40a1fd146"}))
|
||||
|
||||
(defroutes app-routes
|
||||
(GET "/" request (views/index-page request))
|
||||
(GET "/login" {session :session}
|
||||
(let [login-cookies (client/do-login)]
|
||||
{:status 302
|
||||
:headers {"Location" "/thread/3814875?page=29"}
|
||||
:session (assoc session :cookies login-cookies)}))
|
||||
|
||||
(GET "/thread/:id" [id :<< as-int
|
||||
page :<< as-int
|
||||
:as {session :session}]
|
||||
(let [thread (get-thread session id page)]
|
||||
(views/thread-page thread)))
|
||||
(let [thread (assoc (parser/get-thread session id page) :page page)
|
||||
count (:count session 0)
|
||||
session (assoc session :count (inc count))]
|
||||
{:body (views/thread-page thread session)
|
||||
:headers {"Content-Type" "text/html; charset=utf-8"}
|
||||
:session session}))
|
||||
|
||||
(route/not-found "Not Found"))
|
||||
|
||||
(def app
|
||||
(-> app-routes
|
||||
(wrap-defaults site-defaults)
|
||||
(wrap-session {:cookie-attrs {:max-age 3600}
|
||||
:store (cookie-store {:key "12345678abcdefgh"})})))
|
||||
(wrap-session {:cookie-name "clojsa-session"
|
||||
:cookie-attrs {:max-age (* 7 24 3600)}
|
||||
:store session-cookie-store})))
|
||||
|
@ -1,10 +1,15 @@
|
||||
(ns clojsa.saclient
|
||||
(:require [clojure.string :as string]
|
||||
[http.async.client :as http]))
|
||||
|
||||
(:require [clj-http.client :as client]))
|
||||
|
||||
(def url "https://forums.somethingawful.com/")
|
||||
|
||||
(defn cookie-store [cookies]
|
||||
(let [cs (clj-http.cookies/cookie-store)
|
||||
v (vec cookies)]
|
||||
(for [c v]
|
||||
(println c))
|
||||
cs))
|
||||
|
||||
(defn thread-url
|
||||
([id]
|
||||
(thread-url id 1))
|
||||
@ -14,12 +19,20 @@
|
||||
:pagenumber page}]
|
||||
{:href base-url :params query})))
|
||||
|
||||
(defn do-login []
|
||||
(let [login-url (str url "account.php")
|
||||
cs (clj-http.cookies/cookie-store)]
|
||||
(client/post login-url {:form-params {:action "login"
|
||||
:username "xf86enodev"
|
||||
:password "triebhaftigke1t"}
|
||||
:cookie-store cs})
|
||||
(clj-http.cookies/get-cookies cs)))
|
||||
|
||||
(defn thread-response [session url]
|
||||
(with-open [client (http/create-client)]
|
||||
(let [{:keys [href params]} url
|
||||
resp (http/GET client href :query params)
|
||||
status (http/status resp)
|
||||
headers (http/headers resp)]
|
||||
(-> resp
|
||||
http/await
|
||||
http/string))))
|
||||
(let [cs (cookie-store (:cookies session))
|
||||
resp (client/get (:href url) {:query-params (:params url)
|
||||
:cookie-store cs
|
||||
:debug false})]
|
||||
(:body resp)))
|
||||
|
||||
;; (def witcher-thread (thread-response (thread-url 3720352 3)))
|
||||
|
@ -44,7 +44,7 @@
|
||||
(let [pb (-> pb :content)]
|
||||
(hickory-to-hiccup (hickory-div "postbody" pb))))
|
||||
|
||||
(defn thread-map [id page doc]
|
||||
(defn thread-map [id doc]
|
||||
(let [htree (hickory-doc doc)
|
||||
title (parse-title htree)
|
||||
page-count (parse-pagecount htree)
|
||||
@ -54,10 +54,12 @@
|
||||
postbody (select-td :postbody thread-tree)]
|
||||
{:title title
|
||||
:id id
|
||||
:page page
|
||||
:page-count page-count
|
||||
:content
|
||||
(for [[ui pd pb] (partition 3 (interleave userinfo postdate postbody))
|
||||
: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 get-thread [session id page]
|
||||
(thread-map id (thread-response session (thread-url id page))))
|
||||
|
@ -1,7 +1,7 @@
|
||||
(ns clojsa.views
|
||||
(:use [hiccup core page])
|
||||
(:require [clojure.string :as string]
|
||||
[clojure.pprint]
|
||||
(:require
|
||||
[clojure.string :as string]
|
||||
[clojure.java.io :as io]
|
||||
[cheshire.core :as json]))
|
||||
|
||||
@ -30,13 +30,14 @@
|
||||
(include-js "/js/main.js")]))
|
||||
|
||||
(defn index-page [req]
|
||||
(main-template {}
|
||||
[:div.container
|
||||
[:pre
|
||||
(clojure.pprint/pprint req)]]))
|
||||
(main-template
|
||||
{}
|
||||
[:div.container
|
||||
[:pre.output
|
||||
[:code (with-out-str (clojure.pprint/pprint req))]]]))
|
||||
|
||||
(defn paginate [id cur last]
|
||||
[:nav.container.pagination {:hx-boot "false"}
|
||||
[:nav.container.box.pagination {:hx-boot "false"}
|
||||
[:a.pagination-previous
|
||||
{:href (format "/thread/%d?page=%d" id (dec cur))} "<"]
|
||||
[:a.pagination-next
|
||||
@ -58,18 +59,26 @@
|
||||
[:a.pagination-link
|
||||
{:href (format "/thread/%d?page=%d" id last)} (str last)]]]])
|
||||
|
||||
(defn thread-page [thread]
|
||||
(defn thread-page [thread session]
|
||||
(let [{:keys [id title page page-count content]} thread]
|
||||
(main-template
|
||||
{:title title}
|
||||
[:div.container
|
||||
[:pre.output
|
||||
[:code (with-out-str (clojure.pprint/pprint session))]]
|
||||
[:h1.is-size-3.mb-4 title]]
|
||||
[:section.thread
|
||||
(for [post content]
|
||||
[:article.container.box.columns
|
||||
[:aside.userinfo.column
|
||||
(:ui post)]
|
||||
[:main.postbody.content.column.is-four-fifths
|
||||
(:pb post)]])]
|
||||
[:article.container.box
|
||||
[:div.tile.is-ancestor
|
||||
[:aside.userinfo.tile.is-3.is-parent
|
||||
(:ui post)]
|
||||
[:main.postbody.content.tile.is-9.is-parent.is-vertical
|
||||
[:div.tile.is-child
|
||||
(:pb post)]
|
||||
[:div.level.tile.is-child.is-12
|
||||
[:div.level-right
|
||||
[:span.postdate.level-item
|
||||
(:pd post)]]]]]])]
|
||||
[:section
|
||||
(paginate id page page-count)])))
|
||||
|
Loading…
Reference in New Issue
Block a user