Update format

This commit is contained in:
Andros Fenollosa 2019-08-04 11:00:35 +02:00
parent 4ceb781e10
commit c7383a6a5a
2 changed files with 52 additions and 32 deletions

View File

@ -2,10 +2,30 @@
## Installation ## Installation
``` sh
cp config-example.edn config.edn cp config-example.edn config.edn
```
## Usage ## Usage
$ java -jar isahn-0.1.0-standalone.jar [args] ``` sh
java -jar isahn-[version]-standalone.jar
```
## Check format
``` sh
lein cljfmt check
```
## Check format
``` sh
lein cljfmt check
```
## Linter
``` sh
lein auto kibit
```

View File

@ -3,10 +3,10 @@
[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 :as json])
(:gen-class)) (:gen-class))
" VARIABLES " ;; VARIABLES
" URL send for Telegram " " URL send for Telegram "
(def url_telegram_send (str "https://api.telegram.org/" (:bot_token env) "/sendMessage")) (def url_telegram_send (str "https://api.telegram.org/" (:bot_token env) "/sendMessage"))
@ -22,10 +22,10 @@
(def min_time (- now unixtime24h)) (def min_time (- now unixtime24h))
" 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)) (json/parse-string (slurp (io/file path_history)) true) (str "")))
(def history_ids (map :id history)) (def history_ids (map :id history))
" FUNCTIONS " ;; FUNCTIONS
(defn one_story (defn one_story
" Get url from item " " Get url from item "
@ -35,56 +35,56 @@
(defn get_all_stories (defn get_all_stories
" Get all stories " " Get all stories "
[url_all_stories] [url_all_stories]
" Get all ids stories" " Get all ids stories "
(def ids_stories (parse-string (:body (client/get url_all_stories {:accept :json})))) (let [ids_stories (json/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)) (let [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 #(json/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] (defn set-interval
" Run function every ms " " Run function every ms "
[callback ms]
(future (while true (do (Thread/sleep ms) (callback))))) (future (while true (do (Thread/sleep ms) (callback)))))
(defn add_history (defn add_history
" Add to file history news_stories " " Add to file history news_stories "
[news_stories] [news_stories]
(def history_news_ids (concat history_ids (map #(get-in % ["id"]) news_stories))) (let [history_news_ids (concat history_ids (map #(get-in % ["id"]) news_stories))
(def history_all (map #(assoc {} :id %) (vec history_news_ids))) history_all (map #(assoc {} :id %) (vec history_news_ids))]
(with-open [w (clojure.java.io/writer path_history :append false)] (with-open [w (clojure.java.io/writer path_history :append false)]
(.write w (generate-string history_all))) (.write w (json/generate-string history_all)))))
)
(defn filter_stories (defn filter_stories
" Filter stories by last 24h, remove histories and lower score " " Filter stories by last 24h, remove histories and lower score "
[stories] [stories]
" Filter created less 24h " " Filter created less 24h "
(def stories_24h (filter #(> (get-in % ["time"]) min_time) stories)) (let [stories_24h (filter #(> (get-in % ["time"]) min_time) stories)]
" Filter history " " Filter history "
(def stories_without_histories (filter #(not (lazy-contains? history_ids (get-in % ["id"]))) stories_24h)) (let [stories_without_histories (filter #(not (lazy-contains? history_ids (get-in % ["id"]))) stories_24h)]
" Filter with score min_score " " Filter with score min_score "
(filter #(> (get-in % ["score"]) min_score) stories_without_histories)) (filter #(> (get-in % ["score"]) min_score) stories_without_histories))))
(defn send_stories_telegram (defn send_stories_telegram
[stories]
" Send stories by Telegram Channel " " Send stories by Telegram Channel "
(doall (iterate #((client/post url_telegram_send {:body (generate-string {:chat_id (:chat env) [stories]
:text (str (get-in % ["title"]) ": " (get-in % ["url"])) (if (not-empty stories) (doall (iterate #((client/post url_telegram_send {:body (json/generate-string {:chat_id (:chat env)
:disable_notification true}) :text (str (get-in % ["title"]) ": " (get-in % ["url"]))
:content-type :json :disable_notification true})
:accept :json})) stories))) :content-type :json
:accept :json})) stories))))
(defn check_stories (defn check_stories
" Check news stories and send message to Telegram " " Check news stories and send message to Telegram "
[] []
(def stories_top (filter_stories (get_all_stories url_all_stories))) (let [stories_top (filter_stories (get_all_stories url_all_stories))]
(doall (add_history stories_top)) (doall (add_history stories_top))
(doall (send_stories_telegram stories_top))) (doall (send_stories_telegram stories_top))))
(defn -main (defn -main
"Main execution" "Main execution"