important-stories-alert-for.../src/isahn/core.clj

72 lines
2.4 KiB
Clojure
Raw Normal View History

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]
[cheshire.core :refer :all]
)
(: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-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 "
(def urls_stories (map #(one_story %) ids_stories))
" 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-02 00:01:34 +02:00
(defn filter_stories
[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 "
(def stories_without_histories (filter #(contains? history (get-in % ["id"])) stories_24h))
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-07-29 20:02:33 +02:00
2019-08-02 00:01:34 +02:00
(defn -main
"Main execution"
[]
2019-08-03 12:34:36 +02:00
(def stories_top (filter_stories (get_all_stories url_all_stories)))
(prn stories_top)
2019-08-02 00:01:34 +02:00
;; (doall (iterate #((client/post url_telegram_send {:basic-auth ["user" "pass"]
;; :body (generate-string {
;; :chat_id (:chat env)
;; :text (str (get-in % ["title"]) ": " (get-in % ["url"]))
;; :disable_notification true
;; })
;; :content-type :json
;; :accept :json})) stories_top))
2019-07-29 20:02:33 +02:00
)