diff --git a/src/main/backend/core.clj b/src/main/backend/core.clj index 4c64e38..178c95d 100644 --- a/src/main/backend/core.clj +++ b/src/main/backend/core.clj @@ -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 diff --git a/src/main/backend/handlers.clj b/src/main/backend/handlers.clj index 5522cc6..14e5b58 100644 --- a/src/main/backend/handlers.clj +++ b/src/main/backend/handlers.clj @@ -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))})) diff --git a/src/main/backend/hugo/site.clj b/src/main/backend/hugo/site.clj index ff81a79..9851d79 100644 --- a/src/main/backend/hugo/site.clj +++ b/src/main/backend/hugo/site.clj @@ -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