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

View file

@ -3,29 +3,33 @@
[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
[request]
(let [post-id (-> request :path-params :id)
result (handlers/fetch-hu-post post-id {:hugo true})
content (:body result)]
{:status (:status result)
:headers {"Content-Type" "text/html; charset=utf-8"}
:body (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 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)]]]]]]])}))
(let [post-id (-> request :path-params :id)]
(handlers/fetch-hu-post
post-id
{:content-type {"Content-Type" "text/html; charset=utf-8"}
:replace-str "/hp/"
:wrap-fn hugo-post-template})))