|
6 | 6 |
|
7 | 7 | (enable-console-print!)
|
8 | 8 |
|
| 9 | +(defn sort-by [key] |
| 10 | + (fn [x y] |
| 11 | + (if (not (= (key x) (key y))) |
| 12 | + (> (key x) (key y)) |
| 13 | + (> (:id x) (:id y))))) |
| 14 | + |
9 | 15 | (def app-state (atom {:count 0
|
10 | 16 | :tweets-map {}
|
11 |
| - :by-followers (sorted-set-by (fn [x y] (> (:followers_count x) (:followers_count y)))) |
12 |
| - :by-retweets (sorted-set-by (fn [x y] (> (:retweet_count x) (:retweet_count y)))) |
13 |
| - :by-favorites (sorted-set-by (fn [x y] (> (:favorite_count x) (:favorite_count y)))) |
| 17 | + :rt-since-startup {} |
| 18 | + :by-followers (sorted-set-by (sort-by :followers_count)) |
| 19 | + :by-retweets (sorted-set-by (sort-by :retweet_count)) |
| 20 | + :by-rt-since-startup (sorted-set-by (sort-by :count)) |
| 21 | + :by-favorites (sorted-set-by (sort-by :favorite_count)) |
14 | 22 | :by-id (sorted-set-by >)
|
15 | 23 | :n 10
|
16 | 24 | :sorted :by-followers}))
|
|
35 | 43 |
|
36 | 44 | (defn add-rt-status [tweet]
|
37 | 45 | (if (contains? tweet :retweeted_status)
|
38 |
| - (let [rt (:retweeted_status tweet) prev ((keyword (:id_str rt)) (:tweets-map @app-state))] |
| 46 | + (let [rt (:retweeted_status tweet) |
| 47 | + prev ((keyword (:id_str rt)) (:tweets-map @app-state)) |
| 48 | + prev-rt-count ((keyword (:id_str rt)) (:rt-since-startup @app-state))] |
39 | 49 | (if (not (nil? prev))
|
40 | 50 | (do
|
41 | 51 | (swap! app-state assoc :by-retweets (disj (:by-retweets @app-state)
|
42 | 52 | {:retweet_count (:retweet_count prev)
|
43 |
| - :id (:id_str rt)}))) |
| 53 | + :id (:id_str rt)})) |
44 | 54 | (swap! app-state assoc :by-favorites (disj (:by-favorites @app-state)
|
45 | 55 | {:favorite_count (:favorite_count prev)
|
46 |
| - :id (:id_str rt)}))) |
| 56 | + :id (:id_str rt)})))) |
| 57 | + (if (not (nil? rt)) |
| 58 | + (do |
| 59 | + (if (not (nil? prev-rt-count)) |
| 60 | + (swap! app-state assoc :by-rt-since-startup (disj (:by-rt-since-startup @app-state) |
| 61 | + {:count prev-rt-count |
| 62 | + :id (:id_str rt)}))) |
| 63 | + |
| 64 | + (swap! app-state assoc-in [:rt-since-startup (keyword (:id_str rt))] |
| 65 | + (inc ((keyword (:id_str rt)) (:rt-since-startup @app-state)))) |
| 66 | + |
| 67 | + (swap! app-state assoc :by-rt-since-startup (conj (:by-rt-since-startup @app-state) |
| 68 | + {:count ((keyword (:id_str rt)) (:rt-since-startup @app-state)) |
| 69 | + :id (:id_str rt)})))) |
47 | 70 | (add-to-tweets-map rt)
|
48 | 71 | (swap! app-state assoc :by-retweets (conj (:by-retweets @app-state)
|
49 | 72 | {:retweet_count (:retweet_count rt)
|
|
0 commit comments