-
Notifications
You must be signed in to change notification settings - Fork 0
/
bb.edn
32 lines (31 loc) · 1.44 KB
/
bb.edn
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{:tasks
{:requires ([clojure.string :as str]
[babashka.fs :as fs]
[babashka.curl :as curl])
haata
{:task
(let [target-dir "resources/haata"
build-url (fn [i] (str "https://chart-studio.plotly.com/~haata/" i ".csv"))
save-csv! (fn [url res]
(let [[_ title]
(re-find (re-pattern "<title>(.*)</title>")
(:body (curl/get (str/replace url (re-pattern ".csv$") ""))))
[switch-name] (str/split title (re-pattern " \\| "))
file-name (str switch-name ".csv")]
(when-not (or (str/blank? title)
(str/includes? (str/lower-case title) "untitled")
(str/blank? switch-name))
(println (str "Saving file " file-name))
(spit (fs/file target-dir file-name) (:body res)))))]
(when (fs/exists? target-dir)
(println "Clearing old CSV files")
(fs/delete-tree target-dir))
(fs/create-dir target-dir)
(dotimes [i 1000]
(let [url (build-url i)
_ (println (str "Checking " url))
res (curl/get url {:throw false})]
(when (and (= 200 (:status res))
(= "text/csv" (get-in res [:headers "content-type"])))
(println "Found CSV")
(save-csv! url res)))))}}}