tangled
alpha
login
or
join now
m1emi1em.dev
/
bb-scripts
0
fork
atom
this repo has no description
0
fork
atom
overview
issues
pulls
pipelines
simplify downloading with get->data :3
m1emi1em.dev
1 year ago
1359c605
4342f626
+12
-15
1 changed file
expand all
collapse all
unified
split
weather
weather.bb
+12
-15
weather/weather.bb
reviewed
···
12
12
(for [k keyset]
13
13
(vector k (get coll k))))
14
14
15
15
+
(defn get->data [url get-fn pre-fn]
16
16
+
(let [response (get-fn url)
17
17
+
http-code (:status response)]
18
18
+
(condp = http-code
19
19
+
200 (-> response :body pre-fn)
20
20
+
500 (throw (Exception. "Server responded with 500. Rate limit (probably) exceeded. Wait a bit then try again"))
21
21
+
(throw (Exception. (str "Unexpected HTTP status " http-code))))))
22
22
+
15
23
(defn download-geonames-data [cache-path country]
16
16
-
(let [base-url "http://download.geonames.org/export/zip/"
17
17
-
url (str base-url country ".zip")
18
18
-
response (curl/get url {:as :bytes})]
19
19
-
(if (= (:status response) 200)
20
20
-
(do (-> response :body io/input-stream (fs/unzip cache-path))
21
21
-
(str cache-path country ".txt"))
22
22
-
(throw (Exception. str "Wrong HTTP Response Code: " (:status response))))))
24
24
+
(-> "http://download.geonames.org/export/zip/"
25
25
+
(str country ".zip")
26
26
+
(get->data #(curl/get % {:as :bytes}) io/input-stream)
27
27
+
(fs/unzip cache-path)))
23
28
24
29
(defn get-geonames-data-path
25
30
([] (get-geonames-data-path cache-path "US"))
···
39
44
(first (filter
40
45
(fn [{zc :postal_code}] (= zc zipcode))
41
46
(read-csv-to-maps))))
42
42
-
43
43
-
(defn get->data [url get-fn pre-fn]
44
44
-
(let [response (get-fn url)
45
45
-
http-code (:status response)]
46
46
-
(condp = http-code
47
47
-
200 (-> response :body pre-fn)
48
48
-
500 (throw (Exception. "Server responded with 500. Rate limit (probably) exceeded. Wait a bit then try again"))
49
49
-
(throw (Exception. (str "Unexpected HTTP status " http-code))))))
50
47
51
48
(defn get->json [url] (get->data url curl/get json/parse-string))
52
49