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

104 lines
3.6 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]
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-04 11:08:33 +02:00
;; URL send for Telegram
2019-08-02 00:01:34 +02:00
(def url_telegram_send (str "https://api.telegram.org/" (:bot_token env) "/sendMessage"))
2019-09-05 23:15:00 +02:00
;; URLs from API Hacker News
2019-08-02 00:01:34 +02:00
(def url_all_stories "https://hacker-news.firebaseio.com/v0/topstories.json")
2019-08-04 11:08:33 +02:00
;; Min score
2019-08-02 00:01:34 +02:00
(def min_score (:min_score env))
2019-08-04 11:08:33 +02:00
;; Now unixtime
2019-07-29 20:02:33 +02:00
(def now (quot (System/currentTimeMillis) 1000))
2019-08-04 11:08:33 +02:00
;; 24h in unixtime
2019-07-29 20:02:33 +02:00
(def unixtime24h 86400)
2019-08-04 11:08:33 +02:00
;; Now - 24h
2019-08-02 00:01:34 +02:00
(def min_time (- now unixtime24h))
2019-08-04 11:08:33 +02:00
;; Path file save history
2019-08-02 00:01:34 +02:00
(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
2019-08-06 09:40:25 +02:00
"Get url from item"
2019-08-02 00:01:34 +02:00
[id]
(str "https://hacker-news.firebaseio.com/v0/item/" id ".json"))
(defn get_all_stories
2019-08-06 09:39:17 +02:00
"Get all stories"
2019-08-02 00:01:34 +02:00
[url_all_stories]
2019-08-04 11:08:33 +02:00
;; Get all ids stories
2019-08-04 11:11:09 +02:00
(let [;; Get all API urls stories
ids_stories (json/parse-string (:body (client/get url_all_stories {:accept :json})))
;; Get all data stories
2019-08-04 11:08:33 +02:00
urls_stories (map one_story ids_stories)]
(map #(json/parse-string (:body (client/get % {:accept :json}))) urls_stories)))
2019-07-29 20:02:33 +02:00
2019-08-06 09:39:17 +02:00
(defn lazy_contains? [col key]
2019-08-03 16:19:50 +02:00
(some #{key} col))
2019-08-04 01:13:30 +02:00
(defn add_history
2019-08-04 11:08:33 +02:00
"Add to file history news_stories"
2019-08-04 01:13:30 +02:00
[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-04 11:08:33 +02:00
"Filter stories by last 24h, remove histories and lower score"
2019-08-02 00:01:34 +02:00
[stories]
2019-08-04 11:08:33 +02:00
;; Filter created less 24h
(let [stories_24h (filter #(> (get-in % ["time"]) min_time) stories)
;; Filter history
2022-01-02 10:06:34 +01:00
stories_without_histories (filter #(not (lazy_contains? history_ids (get-in % ["id"]))) stories_24h)
;; Filter "Ask HN:"
stories_without_ask_HN (filter #(not (lazy_contains? "Ask HN:" (get-in % ["title"]))) stories_without_histories)
;; Filter with score min_score
stories_less_24h (filter #(> (get-in % ["score"]) min_score) stories_without_ask_HN)]
;; Return
stories_less_24h))
2019-08-02 00:01:34 +02:00
(defn filter_with_url
"Filter by removing stories that do not have the URL property"
[stories]
(filter (fn [story] (contains? story :url))) stories)
2019-08-03 16:19:50 +02:00
(defn send_stories_telegram
2019-08-04 11:08:33 +02:00
"Send stories by Telegram Channel"
2019-08-04 11:00:35 +02:00
[stories]
2019-08-05 10:20:56 +02:00
(doseq [story stories] (client/post url_telegram_send {:body (json/generate-string {:chat_id (:chat env)
:text (str (get-in story ["title"]) ": " (get-in story ["url"]))
:disable_notification true})
:content-type :json
:accept :json})))
2019-07-29 20:02:33 +02:00
(defn stories_top
"Get all Top Stories with all data"
[]
(->> url_all_stories
(get_all_stories)
(filter_stories)
(filter_with_url)))
(defn send_new_alert
2019-08-05 10:20:56 +02:00
"Check news stories and send message to Telegram"
2019-08-02 00:01:34 +02:00
[]
(let [stories_top (stories_top)]
2019-08-05 10:20:56 +02:00
(add_history stories_top)
(send_stories_telegram stories_top)))
2019-08-04 01:13:30 +02:00
(defn -main
"Main execution"
[]
(send_new_alert))