[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/ring-handler
(ring/router (ring/router
[["/hp/:id" {:parameters {:path {:id int?}} [["/hp/:id" {:parameters {:path {:id int?}}
:get {:handler hugo/build-hu-post}}] :get {:handler hugo/build-hugo-post}}]
["/api" ["/api"
["/hp/:id" {:parameters {:path {:id int?}} ["/hp/:id" {:parameters {:path {:id int?}}
:get {:handler handlers/fetch-hu-post}}]]]) :get {:handler handlers/build-api-hu-post}}]]])
(ring/routes (ring/routes
(ring/create-resource-handler {:path "/"}) (ring/create-resource-handler {:path "/"})
(ring/create-default-handler (ring/create-default-handler

View file

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

View file

@ -3,10 +3,12 @@
[hiccup.page :as page])) [hiccup.page :as page]))
(defn build-hu-post (defn build-hugo-post
[request] [request]
(let [content (handlers/fetch-hu-post request {:hugo true})] (let [post-id (-> request :path-params :id)
{:status 200 result (handlers/fetch-hu-post post-id {:hugo true})
content (:body result)]
{:status (:status result)
:headers {"Content-Type" "text/html; charset=utf-8"} :headers {"Content-Type" "text/html; charset=utf-8"}
:body (page/html5 {:escape-strings? false} :body (page/html5 {:escape-strings? false}
[:html [:html