| 
14 | 14 |    [twitter.oauth :as tw-auth]  | 
15 | 15 |    [twitter.api.restful :as tw-api]  | 
16 | 16 |    [twitter.callbacks.handlers :as tw-handlers])  | 
17 |  | -  (:import  | 
18 |  | -   (twitter.callbacks.protocols SyncSingleCallback))  | 
19 | 17 |   (:gen-class))  | 
20 | 18 | 
 
  | 
21 | 19 | (def github "https://status.github.com/api/status.json")  | 
 | 
25 | 23 | (def status-io-good "All Systems Operational")  | 
26 | 24 | (def github-good "good")  | 
27 | 25 | 
 
  | 
28 |  | -(def state (atom {:alarm-state ::bad  | 
 | 26 | +(def state (atom {:alarm-state ::good  | 
29 | 27 |                   :last-update (time/now)}))  | 
30 | 28 | 
 
  | 
31 | 29 | (defn parse-github  | 
 | 
39 | 37 | (defn fetch-json-status!  | 
40 | 38 |   [url]  | 
41 | 39 |   (timbre/info "fetching url" url)  | 
42 |  | -  (md/chain  | 
43 |  | -   (http/get url)  | 
44 |  | -   :body  | 
45 |  | -   bs/to-reader  | 
46 |  | -   (fn [s]  | 
47 |  | -     (timbre/info "got response from" url)  | 
48 |  | -     (json/parse-stream s true))))  | 
 | 40 | +  ;; these should be able to timeout  | 
 | 41 | +  (->  | 
 | 42 | +   (md/chain  | 
 | 43 | +    (http/get url)  | 
 | 44 | +    :body  | 
 | 45 | +    bs/to-reader  | 
 | 46 | +    (fn [s]  | 
 | 47 | +      (timbre/info "got response from" url)  | 
 | 48 | +      (json/parse-stream s true)))  | 
 | 49 | +   (md/catch Exception  | 
 | 50 | +       (fn [exc] (timbre/warn "exception while fetching" exc)))))  | 
 | 51 | + | 
49 | 52 | 
 
  | 
50 | 53 | (defn get-parse-statuses!  | 
51 | 54 |   "Fetch and parse the status messages for all of the providers.  | 
 | 
75 | 78 |     [::bad ::bad] ::dark  | 
76 | 79 |     [::bad ::good] ::brightening))  | 
77 | 80 | 
 
  | 
 | 81 | +(defn setup-twitter  | 
 | 82 | +  [env]  | 
 | 83 | +  (let [{:keys [api-key api-secret access-token access-token-secret]} env  | 
 | 84 | +        token (tw-auth/make-oauth-creds api-key  | 
 | 85 | +                                        api-secret  | 
 | 86 | +                                        access-token  | 
 | 87 | +                                        access-token-secret)]  | 
 | 88 | +    token))  | 
 | 89 | + | 
78 | 90 | (defn tweet!  | 
79 | 91 |   [message token]  | 
80 | 92 |   (timbre/info "tweeting" message)  | 
81 |  | -  (md/chain  | 
82 |  | -   ;; hmm. wrongness  | 
83 |  | -   (md/future  | 
84 |  | -     (tw-api/statuses-update :oauth-creds token  | 
85 |  | -                             :params {:status message}))  | 
86 |  | -   #(timbre/info "tweeted")))  | 
 | 93 | +  (let [token (setup-twitter env/env)]  | 
 | 94 | +    (->  | 
 | 95 | +     (md/chain  | 
 | 96 | +      ;; this should also be able to timeout  | 
 | 97 | +      (md/future (tw-api/statuses-update :oauth-creds token :params {:status message}))  | 
 | 98 | +      #(timbre/info "tweeted"))  | 
 | 99 | +     (md/catch  | 
 | 100 | +         Exception  | 
 | 101 | +         (fn [exc] (timbre/warn "exception while tweeting:" exc))))))  | 
87 | 102 | 
 
  | 
88 | 103 | (defn tweet-alert!  | 
89 | 104 |   [token status]  | 
 | 
108 | 123 | 
 
  | 
109 | 124 | (defn run-world!  | 
110 | 125 |   []  | 
111 |  | -  (md/let-flow [statuses (get-parse-statuses!)]  | 
 | 126 | +  (let [old-state @state]  | 
112 | 127 |     (md/chain  | 
113 |  | -     (alert! @state statuses)  | 
 | 128 | +     (get-parse-statuses!)  | 
 | 129 | +     #(alert! @state %)  | 
114 | 130 |      (fn [new-world]  | 
115 | 131 |        (reset! state new-world)  | 
116 |  | -       (timbre/info "current state of the world" (:alarm-state @state))))))  | 
 | 132 | +       (timbre/infof "s0=%s, s1=%s" (:alarm-state old-state) (:alarm-state @state))))))  | 
 | 133 | + | 
117 | 134 | 
 
  | 
118 | 135 | (defn keep-checking  | 
119 | 136 |   [period]  | 
 | 
123 | 140 |   []  | 
124 | 141 |   (.start (Thread. (fn [] (.join (Thread/currentThread))) "staying alive")))  | 
125 | 142 | 
 
  | 
126 |  | -(defn setup-twitter  | 
127 |  | -  [env]  | 
128 |  | -  (let [{:keys [api-key api-secret access-token access-token-secret]} env  | 
129 |  | -        token (tw-auth/make-oauth-creds api-key  | 
130 |  | -                                        api-secret  | 
131 |  | -                                        access-token  | 
132 |  | -                                        access-token-secret)]  | 
133 |  | -    token))  | 
134 |  | - | 
135 | 143 | (defn -main  | 
136 | 144 |   [& args]  | 
137 | 145 |   (staying-alive)  | 
138 |  | -  (swap! state conj {:token (setup-twitter env/env)})  | 
139 |  | -  (keep-checking (mt/minutes 2)))  | 
 | 146 | +  (keep-checking (mt/minutes 1)))  | 
0 commit comments