|
12 | 12 | [digest] |
13 | 13 | [llar.item] |
14 | 14 | [java-time.api :as time] |
15 | | - [clojure.spec.alpha :as s])) |
| 15 | + [clojure.spec.alpha :as s]) |
| 16 | + (:import |
| 17 | + [java.time LocalDate LocalDateTime ZoneOffset ZonedDateTime] |
| 18 | + [java.time.format DateTimeFormatter DateTimeFormatterBuilder])) |
16 | 19 |
|
17 | 20 | (defrecord ReadabilityItem |
18 | 21 | [meta |
|
79 | 82 | (assoc :body (when processed (hick-r/hickory-to-html processed))) |
80 | 83 | (assoc :hickory processed)))))) |
81 | 84 |
|
| 85 | +(def ^:private published-time-formatter |
| 86 | + (-> (DateTimeFormatterBuilder.) |
| 87 | + (.append DateTimeFormatter/ISO_LOCAL_DATE_TIME) |
| 88 | + ;; Readability emits both extended (-05:00 / XXX) and compact |
| 89 | + ;; (-0500 / XX) numeric offsets. |
| 90 | + (.appendPattern "[XXX][XX]['['VV']']") |
| 91 | + (.toFormatter))) |
| 92 | + |
| 93 | +(def ^:private local-published-time-formatter |
| 94 | + (-> (DateTimeFormatterBuilder.) |
| 95 | + (.append DateTimeFormatter/ISO_LOCAL_DATE) |
| 96 | + ;; Upstream fixtures contain both ISO's T and a space separator. |
| 97 | + (.appendPattern "['T'][' ']") |
| 98 | + (.append DateTimeFormatter/ISO_LOCAL_TIME) |
| 99 | + (.toFormatter))) |
| 100 | + |
| 101 | +(defn- parse-published-time [timestamp] |
| 102 | + (try |
| 103 | + (ZonedDateTime/parse timestamp published-time-formatter) |
| 104 | + (catch java.time.format.DateTimeParseException zoned-error |
| 105 | + (try |
| 106 | + (.atZone (LocalDateTime/parse timestamp local-published-time-formatter) |
| 107 | + ZoneOffset/UTC) |
| 108 | + (catch java.time.format.DateTimeParseException _ |
| 109 | + (try |
| 110 | + (.atStartOfDay (LocalDate/parse timestamp DateTimeFormatter/ISO_LOCAL_DATE) |
| 111 | + ZoneOffset/UTC) |
| 112 | + (catch java.time.format.DateTimeParseException _ |
| 113 | + (throw zoned-error)))))))) |
| 114 | + |
82 | 115 | (extend-protocol FetchSource |
83 | 116 | llar.src.Readability |
84 | 117 | (fetch-source [src _conditional-tokens] |
|
91 | 124 | (when-not (string/blank? (get-in fetch [:summary :title])) (get-in fetch [:summary :title])) |
92 | 125 | (when-not (string/blank? (:title metadata)) (:title metadata)) |
93 | 126 | "") |
94 | | - pub-ts (or (when-not (string/blank? (:publishedTime data)) (time/zoned-date-time (time/formatter :iso-zoned-date-time) |
95 | | - (:publishedTime data))) |
96 | | - (when-not (string/blank? (:date metadata)) (time/zoned-date-time (time/formatter :iso-zoned-date-time) |
97 | | - (:date metadata))) |
| 127 | + pub-ts (or (when-not (string/blank? (:publishedTime data)) (parse-published-time (:publishedTime data))) |
| 128 | + (when-not (string/blank? (:date metadata)) (parse-published-time (:date metadata))) |
98 | 129 | (get-in fetch [:summary :ts]))] |
99 | 130 | [(make-readability-item |
100 | 131 | (fetch/make-meta src) |
|
0 commit comments