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 01:13:30 +02:00
|
|
|
[cheshire.core :refer :all])
|
2019-07-29 20:02:33 +02:00
|
|
|
(:gen-class))
|
|
|
|
|
2019-08-02 00:01:34 +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-03 12:34:36 +02:00
|
|
|
(def history (if (.exists (io/file path_history)) (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-02 00:01:34 +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]
|
|
|
|
" Get all ids stories"
|
|
|
|
(def ids_stories (parse-string (:body (client/get url_all_stories {:accept :json}))))
|
2019-07-29 20:02:33 +02:00
|
|
|
" Get all API urls stories "
|
2019-08-04 01:13:30 +02:00
|
|
|
(def urls_stories (map one_story ids_stories))
|
2019-07-29 20:02:33 +02:00
|
|
|
" Get all data stories "
|
2019-08-02 00:01:34 +02:00
|
|
|
(map #(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 01:13:30 +02:00
|
|
|
(defn set-interval [callback ms]
|
|
|
|
" Run function every ms "
|
|
|
|
(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]
|
|
|
|
(def history_news_ids (concat history_ids (map #(get-in % ["id"]) news_stories)))
|
|
|
|
(def history_all (map #(assoc {} :id %) (vec history_news_ids)))
|
|
|
|
(prn history_all)
|
|
|
|
(prn (generate-string history_all))
|
|
|
|
(with-open [w (clojure.java.io/writer path_history :append false)]
|
|
|
|
(.write w (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-02 00:01:34 +02:00
|
|
|
(def stories_24h (filter #(> (get-in % ["time"]) min_time) stories))
|
2019-07-29 20:02:33 +02:00
|
|
|
|
2019-08-03 12:34:36 +02:00
|
|
|
" Filter history "
|
2019-08-03 16:19:50 +02:00
|
|
|
(def stories_without_histories (filter #(not (lazy-contains? history_ids (get-in % ["id"]))) stories_24h))
|
2019-08-03 12:34:36 +02:00
|
|
|
|
2019-07-29 20:02:33 +02:00
|
|
|
" Filter with score min_score "
|
2019-08-03 12:34:36 +02:00
|
|
|
(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
|
|
|
|
[stories]
|
|
|
|
" Send stories by Telegram Channel "
|
2019-08-04 01:13:30 +02:00
|
|
|
(doall (iterate #((client/post url_telegram_send {:body (generate-string {:chat_id (:chat env)
|
2019-08-03 16:19:50 +02:00
|
|
|
:text (str (get-in % ["title"]) ": " (get-in % ["url"]))
|
2019-08-04 01:13:30 +02:00
|
|
|
:disable_notification true})
|
2019-08-03 16:19:50 +02:00
|
|
|
: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-03 12:34:36 +02:00
|
|
|
(def stories_top (filter_stories (get_all_stories url_all_stories)))
|
2019-08-04 01:13:30 +02:00
|
|
|
(doall (add_history stories_top))
|
|
|
|
;;(doall (add_history ({"id" 45} {"id" 55})))
|
2019-08-03 12:34:36 +02:00
|
|
|
(prn stories_top)
|
2019-07-29 20:02:33 +02:00
|
|
|
)
|
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)))
|