[back/feat] generate json

This commit is contained in:
SouthFox 2023-08-25 12:54:32 +08:00
parent d34b377bce
commit 96822903e9
3 changed files with 22 additions and 15 deletions

View file

@ -13,6 +13,7 @@
[ring/ring-jetty-adapter "1.11.0-alpha1"] [ring/ring-jetty-adapter "1.11.0-alpha1"]
[org.babashka/http-client "0.4.14"] [org.babashka/http-client "0.4.14"]
[org.jsoup/jsoup "1.16.1"] [org.jsoup/jsoup "1.16.1"]
[cheshire "5.11.0"]
[org.slf4j/slf4j-nop "2.0.7"] [org.slf4j/slf4j-nop "2.0.7"]
[reagent/reagent "1.2.0"] [reagent/reagent "1.2.0"]
@ -21,8 +22,7 @@
[binaryage/devtools "1.0.7"] [binaryage/devtools "1.0.7"]
[re-frame/re-frame "1.3.0"] [re-frame/re-frame "1.3.0"]
[day8.re-frame/tracing "0.6.2"] [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"] :source-paths ["src/main"]
:main ^:skip-aot backend.core :main ^:skip-aot backend.core
@ -30,7 +30,10 @@
:profiles :profiles
{:uberjar {:dependencies ^:replace [[org.clojure/clojure "1.11.1"] {:uberjar {:dependencies ^:replace [[org.clojure/clojure "1.11.1"]
[ring/ring-jetty-adapter "1.11.0-alpha1"] [ring/ring-jetty-adapter "1.11.0-alpha1"]
[metosin/reitit "0.7.0-alpha5"]
[org.slf4j/slf4j-nop "2.0.7"] [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}}) :aot :all}})

View file

@ -9,9 +9,7 @@
(ring/ring-handler (ring/ring-handler
(ring/router (ring/router
[["/hp/:id" {:parameters {:path {:id int?}} [["/hp/:id" {:parameters {:path {:id int?}}
:get {:handler handlers/fetch-hu-post}}] :get {:handler handlers/fetch-hu-post}}]])
])
(ring/create-default-handler (ring/create-default-handler
{:not-found (constantly {:status 404 :body "Not found"})}))) {:not-found (constantly {:status 404 :body "Not found"})})))
@ -21,8 +19,7 @@
(def server (run-jetty #'app {:port 3000 (def server (run-jetty #'app {:port 3000
:join? false})) :join? false}))
;; (.stop server)
(comment ;; (app {:request-method :get
(app {:request-method :get ;; :uri "/hp/431038004"
:uri "/hp/431038004" ;; })
}))

View file

@ -1,17 +1,24 @@
(ns backend.handlers (ns backend.handlers
(:require [clojure.string :as str] (:require [clojure.string :as str]
[babashka.http-client :as client]) [babashka.http-client :as client]
[cheshire.core :refer [generate-string]])
(:import [org.jsoup Jsoup])) (:import [org.jsoup Jsoup]))
(defn wrap-json
[content]
(generate-string {:content content}))
(defn fetch-hu-post [request] (defn fetch-hu-post [request]
(let [id (-> request :path-params :id) (let [id (-> request :path-params :id)
post-url (str/join ["https://zhuanlan.zhihu.com/p/" id])] post-url (str/join ["https://zhuanlan.zhihu.com/p/" id])]
{:status 200 {:status 200
:headers {"Content-Type" "text/html; charset=utf-8"} :headers {"Content-Type" "application/json; charset=utf-8"}
:body (-> (client/get post-url) :body (-> (client/get post-url)
:body :body
Jsoup/parse Jsoup/parse
(.getElementsByClass "Post-RichTextContainer") (.getElementsByClass "Post-RichTextContainer")
(.toString))})) (.toString)
(wrap-json))}))