summaryrefslogtreecommitdiff
path: root/src/weepinbell/metadata/json_ld.clj
blob: 5ff098a41f18baa8458f9335075793eb730aa67a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
(ns weepinbell.metadata.json-ld
  "JSON-LD parser that finds JSON-LD string in a hickroy document
   and then tries to convert it to a JF2 object."
  (:require [weepinbell.metadata.html-head :refer [get-child]]
            [cheshire.core :as json]))

(defn- find-json-ld [hickory]
  (->> hickory (get-child :html) (get-child :head) :content
       (filter #(and (= (:tag %) :script)
                     (= "application/ld+json"
                        (get-in % [:attrs :type]))))
       first :content first))

(defn parse-json-ld
  "Takes hickory document, finds <script type=\"application/ld+json\">,
   parses JSON, returns a JF2 map."
  [hickory]
  (let [data (-> hickory find-json-ld json/parse-string)
        author (get data "author")]
    {:type "entry"
     :name (or (get data "headline")
               (get data "name"))
     :url (get data "url")
     :author (when author
               {:type "card"
                :name (get author "name")
                :url (get author "url")
                :photo (get-in author ["image" "url"])})
     :published (get data "datePublished")
     :content ""
     :wm-property :mention
     :summary (get data "description")}))