2021-06-02 07:35:26 +02:00
|
|
|
(ns rsspaper.feeds
|
|
|
|
(:require
|
|
|
|
[rsspaper.config :refer [config]]
|
2021-07-01 21:59:50 +02:00
|
|
|
[clj-time.coerce :as c]
|
|
|
|
[clj-time.format :as f]
|
2021-06-02 07:35:26 +02:00
|
|
|
[remus :refer [parse-url]]))
|
|
|
|
|
2021-07-01 21:59:50 +02:00
|
|
|
(def date-custom-formatter (f/formatter "dd MM yyyy"))
|
|
|
|
|
|
|
|
(defn datetimes-to-unixtime
|
2021-07-02 21:29:55 +02:00
|
|
|
[articles]
|
|
|
|
(map (fn [article]
|
|
|
|
(assoc article :published-date (c/to-long (:published-date article)))) articles))
|
2021-07-01 21:59:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
(defn add-datetimes-formatter
|
2021-07-02 21:29:55 +02:00
|
|
|
[articles]
|
|
|
|
(map (fn [article]
|
|
|
|
(assoc article :published-date-formatter (f/unparse date-custom-formatter (c/from-long (:published-date article))))) articles))
|
2021-07-01 21:59:50 +02:00
|
|
|
|
|
|
|
|
2021-07-02 21:29:55 +02:00
|
|
|
(defn zip-feeds-in-articles
|
|
|
|
[feeds]
|
|
|
|
;; Flat all articles
|
|
|
|
(reduce (fn [articles feed]
|
|
|
|
;; Add in every article, all information from feed
|
|
|
|
(concat articles (map (fn [article] (assoc article :feed (:feed (update-in feed [:feed] dissoc :entries)) )) (get-in feed [:feed :entries])))) [] feeds))
|
|
|
|
|
|
|
|
|
|
|
|
(defn add-cover-article
|
|
|
|
[articles]
|
|
|
|
;; Add cover to article search first image in description
|
|
|
|
;; Iterate every blog
|
|
|
|
(map (fn [article]
|
2021-07-03 01:20:35 +02:00
|
|
|
(let [url-article (second (re-find #"<img[^>]+src=\"([^\">]+)\"" (str (get-in article [:description :value]))))]
|
|
|
|
(assoc article :cover (if (nil? url-article) (get-in article [:feed :image :url]) url-article)))
|
2021-07-02 21:29:55 +02:00
|
|
|
) articles))
|
|
|
|
|
|
|
|
|
|
|
|
(defn get-articles
|
2021-06-02 07:35:26 +02:00
|
|
|
[]
|
|
|
|
;; Get all feeds from config -> feeds
|
2021-07-02 21:29:55 +02:00
|
|
|
(reverse (sort-by :published-date (->
|
2021-07-01 21:59:50 +02:00
|
|
|
(reduce
|
|
|
|
(fn [feeds feed-url]
|
|
|
|
(conj feeds
|
|
|
|
(parse-url feed-url {:insecure? true :throw-exceptions false}))
|
|
|
|
) [] (:feeds config))
|
2021-07-02 21:29:55 +02:00
|
|
|
zip-feeds-in-articles
|
|
|
|
add-cover-article
|
2021-07-01 21:59:50 +02:00
|
|
|
datetimes-to-unixtime
|
2021-07-02 21:29:55 +02:00
|
|
|
add-datetimes-formatter))))
|