[back/refactor] fetch and build post
This commit is contained in:
parent
86e08d5478
commit
547ce09b45
2 changed files with 56 additions and 54 deletions
|
@ -11,10 +11,7 @@
|
||||||
(generate-string content))
|
(generate-string content))
|
||||||
|
|
||||||
(defn clean-html
|
(defn clean-html
|
||||||
[docs hugo]
|
[docs replace-str]
|
||||||
(let [replace-str (if hugo
|
|
||||||
"/hp/"
|
|
||||||
"#/item/")]
|
|
||||||
(-> (.select docs "style[data-emotion-css~=^[a-z0-9]*$]")
|
(-> (.select docs "style[data-emotion-css~=^[a-z0-9]*$]")
|
||||||
(.remove))
|
(.remove))
|
||||||
(doseq [a (.select docs "a")]
|
(doseq [a (.select docs "a")]
|
||||||
|
@ -22,7 +19,7 @@
|
||||||
(-> (.attr a "href")
|
(-> (.attr a "href")
|
||||||
(str/replace "https://link.zhihu.com/?target=" "")
|
(str/replace "https://link.zhihu.com/?target=" "")
|
||||||
(str/replace "https://zhuanlan.zhihu.com/p/" replace-str)
|
(str/replace "https://zhuanlan.zhihu.com/p/" replace-str)
|
||||||
(url-decode))))))
|
(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
|
||||||
|
:body (wrap-fn
|
||||||
|
{:content (.toString post-content)
|
||||||
:title (.text title)
|
:title (.text title)
|
||||||
:time (first (str/split (.text post-time) #"・"))
|
:time (first (str/split (.text post-time) #"・"))
|
||||||
:catalog (build-catalog post-content)}}))
|
: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
|
||||||
|
:headers (:content-type params)
|
||||||
:body {:content "Not Found"
|
:body {:content "Not Found"
|
||||||
:title "Not Found"}}
|
:title "Not Found"}}
|
||||||
(process-hu-post page hugo))))
|
(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})))
|
||||||
|
|
|
@ -3,14 +3,9 @@
|
||||||
[hiccup.page :as page]))
|
[hiccup.page :as page]))
|
||||||
|
|
||||||
|
|
||||||
(defn build-hugo-post
|
(def hugo-post-template
|
||||||
[request]
|
#(page/html5
|
||||||
(let [post-id (-> request :path-params :id)
|
{:escape-strings? false}
|
||||||
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
|
[:html
|
||||||
[:head
|
[:head
|
||||||
[:meta {:name "viewport" :content "width=device-width, initial-scale=1.0"}]
|
[:meta {:name "viewport" :content "width=device-width, initial-scale=1.0"}]
|
||||||
|
@ -22,10 +17,19 @@
|
||||||
[:div {:class "flex flex-row flex-wrap py-4"}
|
[: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 "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 {:class "sticky top-0 p-4 bg-slate-300 rounded-xl w-full"}
|
||||||
[:div (:catalog content)]]]
|
[:div (:catalog %)]]]
|
||||||
[:div {:class "w-full sm:w-2/3 md:w-3/4 pt-1 px-2"}
|
[:div {:class "w-full sm:w-2/3 md:w-3/4 pt-1 px-2"}
|
||||||
[:h1 (:title content)]
|
[:h1 (:title %)]
|
||||||
[:time (:time content)]
|
[:time (:time %)]
|
||||||
[:hr]
|
[:hr]
|
||||||
[:div
|
[:div
|
||||||
(:content content)]]]]]]])}))
|
(:content %)]]]]]]]))
|
||||||
|
|
||||||
|
(defn build-hugo-post
|
||||||
|
[request]
|
||||||
|
(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})))
|
||||||
|
|
Loading…
Reference in a new issue