-
Notifications
You must be signed in to change notification settings - Fork 2
/
weather.clj
59 lines (47 loc) · 1.35 KB
/
weather.clj
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
(ns weather
(:require [clojure.string :as string]
[clojure.core.memoize :as memoize]
[clj-http.client :as client]))
; Constants
(def ^:private YQL_QUERY
(str
"select * from weather.forecast where woeid in "
"(select woeid from geo.places(1) "
"where text=\"%s\") and u='c'"))
(def ^:private API_URL
"https://query.yahooapis.com/v1/public/yql")
; Internal API
(defn- send-get [url q]
(let [p {:as :json
:insecure? true
:follow-redirects true
:max-redirects 10
:socket-timeout 30000
:conn-timeout 30000
:query-params q}
res (client/get url p)]
(:body res)))
(defn- fetch-weather [location]
(let [query (format YQL_QUERY (string/trim location))]
(send-get API_URL {:q query :format "json"})))
(defn- report [data]
(string/join
"\n"
(map
#(string/join
", "
[(str "Date: " (:date %))
(str "High: " (:high %))
(str "Low: " (:low %))])
data)))
(defn- error [msg]
(str msg "\n Do not forget to specify city name, ex: \"weather Novosibirsk\""))
; External API
(defn init [data]
(try
(let [location (:text data)
json (fetch-weather location)
data (get-in json [:query :results :channel :item :forecast])]
(report data))
(catch Exception e
(error (.getMessage e)))))