[back/feat] question paging

This commit is contained in:
SouthFox 2023-09-11 10:57:16 +08:00
parent 2e010c8223
commit 319b875e33

View file

@ -106,7 +106,6 @@
:updated_time (get-in m ["target" "updated_time"]) :updated_time (get-in m ["target" "updated_time"])
:comment_count (get-in m ["target" "comment_count"]) :comment_count (get-in m ["target" "comment_count"])
:voteup_count (get-in m ["target" "voteup_count"])}) :voteup_count (get-in m ["target" "voteup_count"])})
(into {} (into {}
{:author {:avatar_url (get-in m ["author" "avatarUrl"]) {:author {:avatar_url (get-in m ["author" "avatarUrl"])
:url_token (get-in m ["author" "urlToken"]) :url_token (get-in m ["author" "urlToken"])
@ -131,44 +130,87 @@
(defn process-hu-questiion (defn process-hu-questiion
[question-json {:keys [content-type wrap-fn]}] [question-json {:keys [content-type wrap-fn]}]
{:status 200 {:status 200
:headers content-type :headers content-type
:body (wrap-fn :body (wrap-fn question-json)})
(merge
{:question (build-question (val (first (get-in question-json ["initialState" "entities" "questions"]))))} (defn fetch-hu-answers
{:answers (mapv build-answer (vals (get-in question-json ["initialState" "entities" "answers"])))}))}) [question-id query]
(let [question-url (str/join ["https://www.zhihu.com/api/v4/questions/"
question-id
"/feeds?cursor=" (get query "cursor")
"&include=data%5B%2A%5D.is_normal%2Cadmin_closed_comment%2Cis_collapsed%2Cannotation_action%2Cannotation_detail%2Ccollapse_reason%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cattachment%2Cvoteup_count%2Ccomment_permission%2Ccreated_time%2Cupdated_time%2Creview_info%2Crelevant_info%2Cquestion%2Cexcerpt%2Cis_labeled%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%3Bdata%5B%2A%5D.mark_infos%5B%2A%5D.url%3Bdata%5B%2A%5D.author.follower_count%2Cvip_info%2Cbadge%5B%2A%5D.topics%3Bdata%5B%2A%5D.settings.table_of_content.enabled"
"&limit=5"
"&offset=0"
"&order=default"
"&platform=desktop"
"&session_id=" (get query "session_id")])
question-json (-> (client/get question-url) :body parse-string)]
question-json))
(defn build-paging-next
[question-id next]
(let [cursor (last (re-find #"cursor=(.*?)&" next))
session-id (last (re-find #"session_id=(.*?)(?:&|$)" next))]
(str/join ["/api/hq/"
question-id
"?cursor="
cursor
"&session_id="
session-id])))
(defn process-api-paging
[question-id docs]
(let [next (get docs "next")]
{:paging {"page" (get docs "page")
"is_end" (get docs "is_end")
"next" (build-paging-next question-id next)}}))
(defn process-json-paging
[question-id docs]
(let [answers (val (first (get docs "answers")))]
(if (< (count (get answers "ids")) 5)
{:paging {"page" nil
"is_end" true
"next" nil}}
(let [next_url (get answers "next")]
{:paging {"page" nil
"is_end" false
"next" (build-paging-next question-id next_url)}}))))
(defn fetch-hu-question (defn fetch-hu-question
[question-id params] [request params]
(let [question-url (str/join ["https://www.zhihu.com/question/" (let [question-id (-> request :path-params :id)
question-id]) query (-> request :query-params)
question-url (str/join ["https://www.zhihu.com/question/"
question-id])
question-page (-> (client/get question-url) :body Jsoup/parse) question-page (-> (client/get question-url) :body Jsoup/parse)
question-json (-> (.getElementById question-page "js-initialData") question-json (-> (.getElementById question-page "js-initialData")
.firstChild .firstChild
.toString .toString
parse-string) parse-string)
question-error? (= (get question-json "subAppName") "errorpage")] question-error? (= (get question-json "subAppName") "errorpage")]
(if question-error? (if question-error?
{:status 404 {:status 404
:headers (:content-type params) :headers (:content-type params)
:body {:content "Not Found" :body (wrap-json {:content "Not Found"
:title "Not Found"}} :title "Not Found"})}
(if (empty? query)
(process-hu-questiion question-json params)))) (process-hu-questiion
(merge
(defn fetch-hu-answers {:question (build-question (val (first (get-in question-json ["initialState" "entities" "questions"]))))}
[question-id params] {:answers (mapv build-answer (vals (get-in question-json ["initialState" "entities" "answers"])))}
(let [question-url (str/join ["https://www.zhihu.com/api/v4/questions/" (process-json-paging question-id (get-in question-json ["initialState" "question"])))
question-id params)
"/feeds?cursor=&include=data%5B%2A%5D.is_normal%2Cadmin_closed_comment%2Cis_collapsed%2Cannotation_action%2Cannotation_detail%2Ccollapse_reason%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cattachment%2Cvoteup_count%2Ccomment_permission%2Ccreated_time%2Cupdated_time%2Creview_info%2Crelevant_info%2Cquestion%2Cexcerpt%2Cis_labeled%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%3Bdata%5B%2A%5D.mark_infos%5B%2A%5D.url%3Bdata%5B%2A%5D.author.follower_count%2Cvip_info%2Cbadge%5B%2A%5D.topics%3Bdata%5B%2A%5D.settings.table_of_content.enabled&limit=5&offset=0&order=default&platform=desktop&session_id="]) (process-hu-questiion
question-json (-> (client/get question-url) :body parse-string)] (merge
question-json)) {:answers (mapv build-answer (get (fetch-hu-answers question-id query) "data"))}
(process-api-paging question-id (get (fetch-hu-answers question-id query) "paging")))
params)))))
(defn build-api-hu-question (defn build-api-hu-question
[request] [request]
(let [question-id (-> request :path-params :id)] (fetch-hu-question
(fetch-hu-question question-id request
{:content-type {"Content-Type" "application/json; charset=utf-8"} {:content-type {"Content-Type" "application/json; charset=utf-8"}
:wrap-fn wrap-json}))) :wrap-fn wrap-json}))