Update dev tools and fix write history

This commit is contained in:
Andros Fenollosa 2019-08-04 01:13:30 +02:00
parent 564341e209
commit e77b46af01
3 changed files with 39 additions and 19 deletions

View File

@ -2,4 +2,5 @@
:min_score 600 :min_score 600
:chat "" :chat ""
:bot_token "" :bot_token ""
} :run_every_miliseconds 3600000
}

View File

@ -13,6 +13,12 @@
; Parse JSON ; Parse JSON
[cheshire "5.8.1"] [cheshire "5.8.1"]
] ]
:plugins [
; Dev Tools
[lein-auto "0.1.3"]
[lein-kibit "0.1.7"]
[lein-cljfmt "0.6.4"]
]
:main ^:skip-aot isahn.core :main ^:skip-aot isahn.core
:target-path "target/%s" :target-path "target/%s"
:profiles {:uberjar {:aot :all}}) :profiles {:uberjar {:aot :all}})

View File

@ -3,11 +3,9 @@
[config.core :refer [env]] [config.core :refer [env]]
[clojure.java.io :as io] [clojure.java.io :as io]
[clj-http.client :as client] [clj-http.client :as client]
[cheshire.core :refer :all] [cheshire.core :refer :all])
)
(:gen-class)) (:gen-class))
" VARIABLES " " VARIABLES "
" URL send for Telegram " " URL send for Telegram "
@ -25,7 +23,7 @@
" Path file save history " " Path file save history "
(def path_history "isahn_history.json") (def path_history "isahn_history.json")
(def history (if (.exists (io/file path_history)) (parse-string (slurp (io/file path_history)) true) (str ""))) (def history (if (.exists (io/file path_history)) (parse-string (slurp (io/file path_history)) true) (str "")))
(def history_ids (map #(:id %) history)) (def history_ids (map :id history))
" FUNCTIONS " " FUNCTIONS "
@ -40,17 +38,26 @@
" Get all ids stories" " Get all ids stories"
(def ids_stories (parse-string (:body (client/get url_all_stories {:accept :json})))) (def ids_stories (parse-string (:body (client/get url_all_stories {:accept :json}))))
" Get all API urls stories " " Get all API urls stories "
(def urls_stories (map #(one_story %) ids_stories)) (def urls_stories (map one_story ids_stories))
" Get all data stories " " Get all data stories "
(map #(parse-string (:body (client/get % {:accept :json}))) urls_stories)) (map #(parse-string (:body (client/get % {:accept :json}))) urls_stories))
(defn lazy-contains? [col key] (defn lazy-contains? [col key]
(some #{key} col)) (some #{key} col))
(defn set-interval [callback ms]
" Run function every ms "
(future (while true (do (Thread/sleep ms) (callback)))))
(defn save_history (defn add_history
[] " Add to file history news_stories "
(prn "salvado") [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)))
) )
(defn filter_stories (defn filter_stories
@ -68,19 +75,25 @@
(defn send_stories_telegram (defn send_stories_telegram
[stories] [stories]
" Send stories by Telegram Channel " " Send stories by Telegram Channel "
(doall (iterate #((client/post url_telegram_send {:basic-auth ["user" "pass"] (doall (iterate #((client/post url_telegram_send {:body (generate-string {:chat_id (:chat env)
:body (generate-string {
:chat_id (:chat env)
:text (str (get-in % ["title"]) ": " (get-in % ["url"])) :text (str (get-in % ["title"]) ": " (get-in % ["url"]))
:disable_notification true :disable_notification true})
})
:content-type :json :content-type :json
:accept :json})) stories))) :accept :json})) stories)))
(defn check_stories
" Check news stories and send message to Telegram "
[]
(def stories_top (filter_stories (get_all_stories url_all_stories)))
(doall (add_history stories_top))
;;(doall (add_history ({"id" 45} {"id" 55})))
(prn stories_top)
)
(defn -main (defn -main
"Main execution" "Main execution"
[] []
" Run first time "
(def stories_top (filter_stories (get_all_stories url_all_stories))) (check_stories)
(prn stories_top) " Run every :run_every_miliseconds "
) (set-interval check_stories (:run_every_miliseconds env)))