From 96822903e998e8fa29419fbe6ae19e7a85441ba0 Mon Sep 17 00:00:00 2001 From: SouthFox Date: Fri, 25 Aug 2023 12:54:32 +0800 Subject: [PATCH] [back/feat] generate json --- project.clj | 9 ++++++--- src/main/backend/core.clj | 15 ++++++--------- src/main/backend/handlers.clj | 13 ++++++++++--- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/project.clj b/project.clj index b171277..b39aa1e 100644 --- a/project.clj +++ b/project.clj @@ -13,6 +13,7 @@ [ring/ring-jetty-adapter "1.11.0-alpha1"] [org.babashka/http-client "0.4.14"] [org.jsoup/jsoup "1.16.1"] + [cheshire "5.11.0"] [org.slf4j/slf4j-nop "2.0.7"] [reagent/reagent "1.2.0"] @@ -21,8 +22,7 @@ [binaryage/devtools "1.0.7"] [re-frame/re-frame "1.3.0"] [day8.re-frame/tracing "0.6.2"] - [day8.re-frame/re-frame-10x "1.6.0"] - ] + [day8.re-frame/re-frame-10x "1.6.0"]] :source-paths ["src/main"] :main ^:skip-aot backend.core @@ -30,7 +30,10 @@ :profiles {:uberjar {:dependencies ^:replace [[org.clojure/clojure "1.11.1"] [ring/ring-jetty-adapter "1.11.0-alpha1"] + [metosin/reitit "0.7.0-alpha5"] [org.slf4j/slf4j-nop "2.0.7"] - [org.babashka/http-client "0.4.14"]] + [org.babashka/http-client "0.4.14"] + [org.jsoup/jsoup "1.16.1"] + [cheshire "5.11.0"]] :aot :all}}) diff --git a/src/main/backend/core.clj b/src/main/backend/core.clj index 84a8e46..a322b21 100644 --- a/src/main/backend/core.clj +++ b/src/main/backend/core.clj @@ -9,9 +9,7 @@ (ring/ring-handler (ring/router [["/hp/:id" {:parameters {:path {:id int?}} - :get {:handler handlers/fetch-hu-post}}] - ]) - + :get {:handler handlers/fetch-hu-post}}]]) (ring/create-default-handler {:not-found (constantly {:status 404 :body "Not found"})}))) @@ -20,9 +18,8 @@ :join? false})) (def server (run-jetty #'app {:port 3000 - :join? false})) - -(comment - (app {:request-method :get - :uri "/hp/431038004" - })) + :join? false})) +;; (.stop server) +;; (app {:request-method :get +;; :uri "/hp/431038004" +;; }) diff --git a/src/main/backend/handlers.clj b/src/main/backend/handlers.clj index cb2b44a..37fbda2 100644 --- a/src/main/backend/handlers.clj +++ b/src/main/backend/handlers.clj @@ -1,17 +1,24 @@ (ns backend.handlers (:require [clojure.string :as str] - [babashka.http-client :as client]) + [babashka.http-client :as client] + [cheshire.core :refer [generate-string]]) (:import [org.jsoup Jsoup])) +(defn wrap-json + [content] + (generate-string {:content content})) + + (defn fetch-hu-post [request] (let [id (-> request :path-params :id) post-url (str/join ["https://zhuanlan.zhihu.com/p/" id])] {:status 200 - :headers {"Content-Type" "text/html; charset=utf-8"} + :headers {"Content-Type" "application/json; charset=utf-8"} :body (-> (client/get post-url) :body Jsoup/parse (.getElementsByClass "Post-RichTextContainer") - (.toString))})) + (.toString) + (wrap-json))}))