[back/refactor] fetch and build post

This commit is contained in:
SouthFox 2023-09-01 15:39:24 +08:00
parent 86e08d5478
commit 547ce09b45
2 changed files with 56 additions and 54 deletions

View file

@ -11,18 +11,15 @@
(generate-string content)) (generate-string content))
(defn clean-html (defn clean-html
[docs hugo] [docs replace-str]
(let [replace-str (if hugo (-> (.select docs "style[data-emotion-css~=^[a-z0-9]*$]")
"/hp/" (.remove))
"#/item/")] (doseq [a (.select docs "a")]
(-> (.select docs "style[data-emotion-css~=^[a-z0-9]*$]") (.attr a "href"
(.remove)) (-> (.attr a "href")
(doseq [a (.select docs "a")] (str/replace "https://link.zhihu.com/?target=" "")
(.attr a "href" (str/replace "https://zhuanlan.zhihu.com/p/" replace-str)
(-> (.attr a "href") (url-decode)))))
(str/replace "https://link.zhihu.com/?target=" "")
(str/replace "https://zhuanlan.zhihu.com/p/" replace-str)
(url-decode))))))
(defn clean-images (defn clean-images
[docs] [docs]
@ -53,38 +50,39 @@
(apply str (mapv build-catalog-item catalog)))) (apply str (mapv build-catalog-item catalog))))
(defn process-hu-post (defn process-hu-post
[page hugo] [page {:keys [content-type replace-str wrap-fn]}]
(let [post-content (.getElementsByClass page "Post-RichTextContainer") (let [post-content (.getElementsByClass page "Post-RichTextContainer")
title (.getElementsByClass page "Post-Title") title (.getElementsByClass page "Post-Title")
post-time (.getElementsByClass page "ContentItem-time")] post-time (.getElementsByClass page "ContentItem-time")]
(clean-html post-content hugo) (clean-html post-content replace-str)
(clean-images post-content) (clean-images post-content)
(render-linkcard post-content) (render-linkcard post-content)
{:status 200 {:status 200
:body {:content (.toString post-content) :headers content-type
:title (.text title) :body (wrap-fn
:time (first (str/split (.text post-time) #"・")) {:content (.toString post-content)
:catalog (build-catalog post-content)}})) :title (.text title)
:time (first (str/split (.text post-time) #"・"))
:catalog (build-catalog post-content)})}))
(defn fetch-hu-post (defn fetch-hu-post
[post-id & {:keys [hugo]}] [post-id params]
(let [post-url (str/join ["https://zhuanlan.zhihu.com/p/" post-id]) (let [post-url (str/join ["https://zhuanlan.zhihu.com/p/" post-id])
page (-> (client/get post-url) :body Jsoup/parse) page (-> (client/get post-url) :body Jsoup/parse)
docs (.getElementsByClass page "Post-RichTextContainer")] docs (.getElementsByClass page "Post-RichTextContainer")]
(if (empty? docs) (if (empty? docs)
{:status 404 {:status 404
:body {:content "Not Found" :headers (:content-type params)
:title "Not Found"}} :body {:content "Not Found"
(process-hu-post page hugo)))) :title "Not Found"}}
(process-hu-post page params))))
(defn build-api-hu-post (defn build-api-hu-post
[request] [request]
(let [post-id (-> request :path-params :id) (let [post-id (-> request :path-params :id)]
content (fetch-hu-post post-id)] (fetch-hu-post post-id
{:status (:status content) {:content-type {"Content-Type" "application/json; charset=utf-8"}
:headers {"Content-Type" "application/json; charset=utf-8"} :replace-str "#/item/"
:body (wrap-json (:body content))})) :wrap-fn wrap-json})))

View file

@ -3,29 +3,33 @@
[hiccup.page :as page])) [hiccup.page :as page]))
(def hugo-post-template
#(page/html5
{:escape-strings? false}
[:html
[:head
[:meta {:name "viewport" :content "width=device-width, initial-scale=1.0"}]
[:meta {:name "referrer" :content "no-referrer"}]
(page/include-css "/css/app.css")]
[:body
[:div {:id "app"}
[:div {:class "container p-2 mx-auto"}
[:div {:class "flex flex-row flex-wrap py-4"}
[:div {:class "w-full sm:w-1/3 md:w-1/4 px-2"}
[:div {:class "sticky top-0 p-4 bg-slate-300 rounded-xl w-full"}
[:div (:catalog %)]]]
[:div {:class "w-full sm:w-2/3 md:w-3/4 pt-1 px-2"}
[:h1 (:title %)]
[:time (:time %)]
[:hr]
[:div
(:content %)]]]]]]]))
(defn build-hugo-post (defn build-hugo-post
[request] [request]
(let [post-id (-> request :path-params :id) (let [post-id (-> request :path-params :id)]
result (handlers/fetch-hu-post post-id {:hugo true}) (handlers/fetch-hu-post
content (:body result)] post-id
{:status (:status result) {:content-type {"Content-Type" "text/html; charset=utf-8"}
:headers {"Content-Type" "text/html; charset=utf-8"} :replace-str "/hp/"
:body (page/html5 {:escape-strings? false} :wrap-fn hugo-post-template})))
[:html
[:head
[:meta {:name "viewport" :content "width=device-width, initial-scale=1.0"}]
[:meta {:name "referrer" :content "no-referrer"}]
(page/include-css "/css/app.css")]
[:body
[:div {:id "app"}
[:div {:class "container p-2 mx-auto"}
[:div {:class "flex flex-row flex-wrap py-4"}
[:div {:class "w-full sm:w-1/3 md:w-1/4 px-2"}
[:div {:class "sticky top-0 p-4 bg-slate-300 rounded-xl w-full"}
[:div (:catalog content)]]]
[:div {:class "w-full sm:w-2/3 md:w-3/4 pt-1 px-2"}
[:h1 (:title content)]
[:time (:time content)]
[:hr]
[:div
(:content content)]]]]]]])}))