From 43ffb71113f4225c6f1ef1b0dd86b8e64d4e4254 Mon Sep 17 00:00:00 2001 From: SouthFox Date: Thu, 31 Aug 2023 02:48:41 +0800 Subject: [PATCH] [perf] handle 404 --- src/main/backend/handlers.clj | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/main/backend/handlers.clj b/src/main/backend/handlers.clj index 16497c0..0684506 100644 --- a/src/main/backend/handlers.clj +++ b/src/main/backend/handlers.clj @@ -62,18 +62,31 @@ (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") - docs (.getElementsByClass page "Post-RichTextContainer")] + post-time (.getElementsByClass page "ContentItem-time")] + (clean-html docs hugo) (clean-images docs) (render-linkcard docs) - (let [content {:content (.toString docs) - :title (.text title) - :time (first (str/split (.text post-time) #"・")) - :catalog (.toString (build-catalog docs))}] - (if hugo - content - {:status 200 - :headers {"Content-Type" "application/json; charset=utf-8"} - :body (wrap-json content)})))) + + (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)})) + + (let [content {:content (.toString docs) + :title (.text title) + :time (first (str/split (.text post-time) #"・")) + :catalog (.toString (build-catalog docs))}] + (if hugo + content + {:status 200 + :headers {"Content-Type" "application/json; charset=utf-8"} + :body (wrap-json content)})))))