2019-07-29 20:02:33 +02:00
|
|
|
(ns isahn.core
|
|
|
|
(:require
|
2019-08-02 00:01:34 +02:00
|
|
|
[config.core :refer [env]]
|
|
|
|
[clojure.java.io :as io]
|
2019-07-29 20:02:33 +02:00
|
|
|
[clj-http.client :as client]
|
2019-08-04 11:00:35 +02:00
|
|
|
[cheshire.core :as json])
|
2019-07-29 20:02:33 +02:00
|
|
|
(:gen-class))
|
|
|
|
|
2019-08-04 11:00:35 +02:00
|
|
|
;; VARIABLES
|
2019-07-29 20:02:33 +02:00
|
|
|
|
2019-08-02 00:01:34 +02:00
|
|
|
" URL send for Telegram "
|
|
|
|
(def url_telegram_send (str "https://api.telegram.org/" (:bot_token env) "/sendMessage"))
|
|
|
|
" URLs from API Hacker News "
|
|
|
|
(def url_all_stories "https://hacker-news.firebaseio.com/v0/topstories.json")
|
2019-07-29 20:02:33 +02:00
|
|
|
" Min score "
|
2019-08-02 00:01:34 +02:00
|
|
|
(def min_score (:min_score env))
|
|
|
|
" Now unixtime "
|
2019-07-29 20:02:33 +02:00
|
|
|
(def now (quot (System/currentTimeMillis) 1000))
|
2019-08-02 00:01:34 +02:00
|
|
|
" 24h in unixtime "
|
2019-07-29 20:02:33 +02:00
|
|
|
(def unixtime24h 86400)
|
2019-08-02 00:01:34 +02:00
|
|
|
" Now - 24h "
|
|
|
|
(def min_time (- now unixtime24h))
|
|
|
|
" Path file save history "
|
|
|
|
(def path_history "isahn_history.json")
|
2019-08-04 11:00:35 +02:00
|
|
|
(def history (if (.exists (io/file path_history)) (json/parse-string (slurp (io/file path_history)) true) (str "")))
|
2019-08-04 01:13:30 +02:00
|
|
|
(def history_ids (map :id history))
|
2019-07-29 20:02:33 +02:00
|
|
|
|
2019-08-04 11:00:35 +02:00
|
|
|
;; FUNCTIONS
|
2019-07-29 20:02:33 +02:00
|
|
|
|
2019-08-02 00:01:34 +02:00
|
|
|
(defn one_story
|
|
|
|
" Get url from item "
|
|
|
|
[id]
|
|
|
|
(str "https://hacker-news.firebaseio.com/v0/item/" id ".json"))
|
|
|
|
|
|
|
|
(defn get_all_stories
|
|
|
|
" Get all stories "
|
|
|
|
[url_all_stories]
|
2019-08-04 11:00:35 +02:00
|
|
|
" Get all ids stories "
|
|
|
|
(let [ids_stories (json/parse-string (:body (client/get url_all_stories {:accept :json})))]
|
|
|
|
" Get all API urls stories "
|
|
|
|
(let [urls_stories (map one_story ids_stories)]
|
|
|
|
" Get all data stories "
|
|
|
|
(map #(json/parse-string (:body (client/get % {:accept :json}))) urls_stories))))
|
2019-07-29 20:02:33 +02:00
|
|
|
|
2019-08-03 16:19:50 +02:00
|
|
|
(defn lazy-contains? [col key]
|
|
|
|
(some #{key} col))
|
|
|
|
|
2019-08-04 11:00:35 +02:00
|
|
|
(defn set-interval
|
2019-08-04 01:13:30 +02:00
|
|
|
" Run function every ms "
|
2019-08-04 11:00:35 +02:00
|
|
|
[callback ms]
|
2019-08-04 01:13:30 +02:00
|
|
|
(future (while true (do (Thread/sleep ms) (callback)))))
|
2019-08-03 16:19:50 +02:00
|
|
|
|
2019-08-04 01:13:30 +02:00
|
|
|
(defn add_history
|
|
|
|
" Add to file history news_stories "
|
|
|
|
[news_stories]
|
2019-08-04 11:00:35 +02:00
|
|
|
(let [history_news_ids (concat history_ids (map #(get-in % ["id"]) news_stories))
|
|
|
|
history_all (map #(assoc {} :id %) (vec history_news_ids))]
|
|
|
|
(with-open [w (clojure.java.io/writer path_history :append false)]
|
|
|
|
(.write w (json/generate-string history_all)))))
|
2019-08-03 16:19:50 +02:00
|
|
|
|
2019-08-02 00:01:34 +02:00
|
|
|
(defn filter_stories
|
2019-08-03 16:19:50 +02:00
|
|
|
" Filter stories by last 24h, remove histories and lower score "
|
2019-08-02 00:01:34 +02:00
|
|
|
[stories]
|
2019-07-29 20:02:33 +02:00
|
|
|
" Filter created less 24h "
|
2019-08-04 11:00:35 +02:00
|
|
|
(let [stories_24h (filter #(> (get-in % ["time"]) min_time) stories)]
|
2019-07-29 20:02:33 +02:00
|
|
|
|
2019-08-04 11:00:35 +02:00
|
|
|
" Filter history "
|
|
|
|
(let [stories_without_histories (filter #(not (lazy-contains? history_ids (get-in % ["id"]))) stories_24h)]
|
2019-08-03 12:34:36 +02:00
|
|
|
|
2019-08-04 11:00:35 +02:00
|
|
|
" Filter with score min_score "
|
|
|
|
(filter #(> (get-in % ["score"]) min_score) stories_without_histories))))
|
2019-08-02 00:01:34 +02:00
|
|
|
|
2019-08-03 16:19:50 +02:00
|
|
|
(defn send_stories_telegram
|
|
|
|
" Send stories by Telegram Channel "
|
2019-08-04 11:00:35 +02:00
|
|
|
[stories]
|
|
|
|
(if (not-empty stories) (doall (iterate #((client/post url_telegram_send {:body (json/generate-string {:chat_id (:chat env)
|
|
|
|
:text (str (get-in % ["title"]) ": " (get-in % ["url"]))
|
|
|
|
:disable_notification true})
|
|
|
|
:content-type :json
|
|
|
|
:accept :json})) stories))))
|
2019-07-29 20:02:33 +02:00
|
|
|
|
2019-08-04 01:13:30 +02:00
|
|
|
(defn check_stories
|
|
|
|
" Check news stories and send message to Telegram "
|
2019-08-02 00:01:34 +02:00
|
|
|
[]
|
2019-08-04 11:00:35 +02:00
|
|
|
(let [stories_top (filter_stories (get_all_stories url_all_stories))]
|
|
|
|
(doall (add_history stories_top))
|
|
|
|
(doall (send_stories_telegram stories_top))))
|
2019-08-04 01:13:30 +02:00
|
|
|
|
|
|
|
(defn -main
|
|
|
|
"Main execution"
|
|
|
|
[]
|
|
|
|
" Run first time "
|
|
|
|
(check_stories)
|
|
|
|
" Run every :run_every_miliseconds "
|
|
|
|
(set-interval check_stories (:run_every_miliseconds env)))
|