multimethods

This commit is contained in:
Rüdiger Diedrich 2020-06-30 18:36:19 +02:00
parent 0c81a2f51a
commit 00f57ae117

View File

@ -14,52 +14,51 @@
:tag :div
:content content})
(defn element-node [kw htree]
(case kw
(defmulti element-node (fn [kw htree] kw))
:author
(defmethod element-node :author [_ htree]
(-> (s/select (s/child (s/class :author)) htree)
first :content first)
first :content first))
:bookmarks
(defmethod element-node :bookmarks [_ htree]
(s/select (s/descendant
(s/id :forum) (s/tag :tbody) (s/tag :tr)) htree)
(s/id :forum) (s/tag :tbody) (s/tag :tr)) htree))
:pagecount
(defmethod element-node :pagecount [_ htree]
(-> (s/select (s/descendant
(s/class :pages) (s/tag :option)) htree)
last :content first Integer/parseInt)
last :content first Integer/parseInt))
:title
(defmethod element-node :title [_ htree]
(-> (s/select (s/child (s/tag :title)) htree)
first :content first
(string/replace #" - The Something Awful Forums" ""))
(string/replace #" - The Something Awful Forums" "")))
:thread
(defmethod element-node :thread [_ htree]
(let [thread-tree (first (s/select (s/descendant
(s/id :thread)) htree))
td-classes [:userinfo :postdate :postbody]]
(for [class-key td-classes]
(s/select (s/descendant
(s/and (s/tag :td) (s/class class-key))) thread-tree)))))
(s/and (s/tag :td) (s/class class-key))) thread-tree))))
(defn processed-element [kw elem]
(case kw
:bookmark
(defmulti processed-element (fn [kw elem] kw))
(defmethod processed-element :bookmark [_ elem]
(when-let [link (first
(s/select (s/descendant
(s/class :info) (s/tag :a)) elem))]
(let [thread-id (re-find #"\d+$" (:href (:attrs link)))
title (-> link :content first string/trim)]
{:id thread-id :title title}))
{:id thread-id :title title})))
:postbody
(hickory-to-hiccup (hickory-div (:content elem) "postbody"))
(defmethod processed-element :postbody [_ elem]
(hickory-to-hiccup (hickory-div (:content elem) "postbody")))
:postdate
(string/trim (last (hickory-to-hiccup elem)))
(defmethod processed-element :postdate [_ elem]
(string/trim (last (hickory-to-hiccup elem))))
:userinfo
(defmethod processed-element :userinfo [_ elem]
(let [ui (first (s/select (s/descendant (s/tag :dl)) elem))
author (-> (s/select (s/descendant (s/class :author)) ui)
first :content first)
@ -71,7 +70,7 @@
:regdate regdate
:avatar-title (hickory-to-hiccup
(hickory-div (:content title) "avatar-title"))
:avatar (when avatar (hickory-to-hiccup avatar))})))
:avatar (when avatar (hickory-to-hiccup avatar))}))
(defn thread-map [id page doc]
(let [htree (hickory-doc doc)