[back/refactor] fetch and build hu post

This commit is contained in:
SouthFox 2023-09-01 13:57:37 +08:00
parent e8fab9e4c7
commit 86e08d5478
3 changed files with 39 additions and 35 deletions

View file

@ -10,10 +10,10 @@
(ring/ring-handler
(ring/router
[["/hp/:id" {:parameters {:path {:id int?}}
:get {:handler hugo/build-hu-post}}]
:get {:handler hugo/build-hugo-post}}]
["/api"
["/hp/:id" {:parameters {:path {:id int?}}
:get {:handler handlers/fetch-hu-post}}]]])
:get {:handler handlers/build-api-hu-post}}]]])
(ring/routes
(ring/create-resource-handler {:path "/"})
(ring/create-default-handler

View file

@ -52,37 +52,39 @@
(let [catalog (.select docs "h2, h3, h4, h5")]
(apply str (mapv build-catalog-item catalog))))
(defn fetch-hu-post
[request & {:keys [hugo]}]
(let [id (-> request :path-params :id)
post-url (str/join ["https://zhuanlan.zhihu.com/p/" id])
page (-> (client/get post-url) :body Jsoup/parse)
docs (.getElementsByClass page "Post-RichTextContainer")
title (.getElementsByClass page "Post-Title")
post-time (.getElementsByClass page "ContentItem-time")
catalog (build-catalog docs)]
(defn process-hu-post
[page hugo]
(let [post-content (.getElementsByClass page "Post-RichTextContainer")
title (.getElementsByClass page "Post-Title")
post-time (.getElementsByClass page "ContentItem-time")]
(clean-html docs hugo)
(clean-images docs)
(render-linkcard docs)
(clean-html post-content hugo)
(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)}}))
(defn fetch-hu-post
[post-id & {:keys [hugo]}]
(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)
(let [content {:content "Not Found"
:title "Not Found"
:time ""
:catalog ""}]
(if hugo
content
{:status 404
:headers {"Content-Type" "application/json; charset=utf-8"}
:body (wrap-json content)}))
{:status 404
:body {:content "Not Found"
:title "Not Found"}}
(process-hu-post page hugo))))
(let [content {:content (.toString docs)
:title (.text title)
:time (first (str/split (.text post-time) #"・"))
:catalog catalog}]
(if hugo
content
{:status 200
:headers {"Content-Type" "application/json; charset=utf-8"}
:body (wrap-json content)})))))
(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))}))

View file

@ -3,10 +3,12 @@
[hiccup.page :as page]))
(defn build-hu-post
(defn build-hugo-post
[request]
(let [content (handlers/fetch-hu-post request {:hugo true})]
{:status 200
(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