1
1
(ns twitter
2
- (:use [clojure.contrib.json :only [read-json]]
3
- [clojure.contrib.java-utils :only [as-str]])
2
+ (:use [clojure.data.json :only [read-json]])
4
3
(:require [clojure.set :as set]
5
4
[clojure.string :as string]
6
5
[com.twinql.clojure.http :as http]
13
12
14
13
(declare status-handler )
15
14
16
- (def *oauth-consumer* nil )
17
- (def *oauth-access-token* nil )
18
- (def *oauth-access-token-secret* nil )
19
- (def *protocol* " http" )
15
+ (def ^:dynamic *oauth-consumer* nil )
16
+ (def ^:dynamic *oauth-access-token* nil )
17
+ (def ^:dynamic *oauth-access-token-secret* nil )
18
+ (def ^:dynamic *protocol* " http" )
20
19
21
- ; ; Get JSON from clj-apache-http
20
+ ; ; Get JSON from clj-apache-http
22
21
(defmethod http /entity-as :json [entity as state]
23
22
(read-json (http/entity-as entity :string state)))
24
23
28
27
`(binding [*oauth-consumer* ~consumer
29
28
*oauth-access-token* ~access-token
30
29
*oauth-access-token-secret* ~access-token-secret]
31
- (do
30
+ (do
32
31
~@body)))
33
32
34
33
(defmacro with-https
35
34
[ & body]
36
35
`(binding [*protocol* " https" ]
37
- (do
36
+ (do
38
37
~@body)))
39
38
40
39
(defmacro def-twitter-method
@@ -51,21 +50,34 @@ take any required and optional arguments and call the associated Twitter method.
51
50
rest-map# (apply hash-map rest#)
52
51
provided-optional-params# (set/intersection (set ~optional-params)
53
52
(set (keys rest-map#)))
54
- required-query-param-names# (map (fn [x#]
55
- (keyword (string/replace (name x#) #"-" " _" )))
56
- ~required-params)
57
- optional-query-param-names-mapping# (map (fn [x#]
58
- [x# (keyword (string/replace (name x#) #"-" " _" ))])
59
- provided-optional-params#)
60
- query-params# (merge (apply hash-map
61
- (vec (interleave required-query-param-names# ~required-fn-params)))
62
- (apply merge
63
- (map (fn [x#] {(second x#) ((first x#) rest-map#)}) optional-query-param-names-mapping#)))
53
+ required-query-param-names#
54
+ (map (fn [x#]
55
+ (keyword (string/replace (name x#) #"-" " _" )))
56
+ ~required-params)
57
+
58
+ optional-query-param-names-mapping#
59
+ (map (fn [x#]
60
+ [x# (keyword (string/replace (name x#) #"-" " _" ))])
61
+ provided-optional-params#)
62
+
63
+ query-params#(merge (apply hash-map
64
+ (vec (interleave
65
+ required-query-param-names#
66
+ ~required-fn-params)))
67
+ (apply merge
68
+ (map (fn [x#] {(second x#)
69
+ ((first x#)
70
+ rest-map#)})
71
+ optional-query-param-names-mapping#)))
64
72
need-to-url-encode# (if (= :get ~req-method)
65
- (into {} (map (fn [[k# v#]] [k# (oauth.signature/url-encode v#)]) query-params#))
73
+ (into {}
74
+ (map (fn [[k# v#]]
75
+ [k#
76
+ (oauth.signature/url-encode v#)])
77
+ query-params#))
66
78
query-params#)
67
- oauth-creds# (when (and *oauth-consumer*
68
- *oauth-access-token*)
79
+ oauth-creds# (when (and *oauth-consumer*
80
+ *oauth-access-token*)
69
81
(oauth/credentials *oauth-consumer*
70
82
*oauth-access-token*
71
83
*oauth-access-token-secret*
@@ -76,7 +88,7 @@ take any required and optional arguments and call the associated Twitter method.
76
88
req-uri#
77
89
:query (merge query-params#
78
90
oauth-creds#)
79
- :parameters (http/map->params
91
+ :parameters (http/map->params
80
92
{:use-expect-continue false })
81
93
:as :json ))))))
82
94
@@ -282,7 +294,7 @@ take any required and optional arguments and call the associated Twitter method.
282
294
(def-twitter-method friends-of-name
283
295
:get
284
296
" api.twitter.com/1/friends/ids.json"
285
- [:screen-name ]
297
+ [:screen-name ]
286
298
[]
287
299
(comp #(:content %) status-handler))
288
300
@@ -349,7 +361,7 @@ take any required and optional arguments and call the associated Twitter method.
349
361
350
362
(defn update-profile-image [^String image]
351
363
(let [req-uri__9408__auto__ " http://api.twitter.com/1/account/update_profile_image.json"
352
-
364
+
353
365
oauth-creds__9414__auto__ (when
354
366
(and
355
367
*oauth-consumer*
@@ -379,45 +391,47 @@ take any required and optional arguments and call the associated Twitter method.
379
391
(comp #(:content %) status-handler)))
380
392
381
393
(defn update-profile-background-image [^String image & rest__2570__auto__]
382
- (let [req-uri__2571__auto__ " http://api.twitter.com/1/account/update_profile_background_image.json"
383
- rest-map__2572__auto__ (apply hash-map rest__2570__auto__)
384
- provided-optional-params__2573__auto__ (set/intersection
385
- (set [:title ])
386
- (set
387
- (keys
388
- rest-map__2572__auto__)))
389
- query-param-names__2574__auto__ (sort
390
- (map
391
- (fn
392
- [x__2575__auto__]
393
- (keyword
394
- (string/replace
395
- (name
396
- x__2575__auto__)
397
- #"-"
398
- " _"
399
- )))
400
- provided-optional-params__2573__auto__))
401
- query-params__2576__auto__ (apply
402
- hash-map
403
- (interleave
404
- query-param-names__2574__auto__
405
- (vec
406
- (vals
407
- (sort
408
- (select-keys
409
- rest-map__2572__auto__
410
- provided-optional-params__2573__auto__))))))
411
- oauth-creds__2577__auto__ (when
412
- (and
413
- *oauth-consumer*
414
- *oauth-access-token*)
415
- (oauth/credentials
416
- *oauth-consumer*
417
- *oauth-access-token*
418
- :post
419
- req-uri__2571__auto__
420
- query-params__2576__auto__))]
394
+ (let [req-uri__2571__auto__
395
+ " http://api.twitter.com/1/account/update_profile_background_image.json"
396
+
397
+ rest-map__2572__auto__ (apply hash-map rest__2570__auto__)
398
+ provided-optional-params__2573__auto__ (set/intersection
399
+ (set [:title ])
400
+ (set
401
+ (keys
402
+ rest-map__2572__auto__)))
403
+ query-param-names__2574__auto__ (sort
404
+ (map
405
+ (fn
406
+ [x__2575__auto__]
407
+ (keyword
408
+ (string/replace
409
+ (name
410
+ x__2575__auto__)
411
+ #"-"
412
+ " _"
413
+ )))
414
+ provided-optional-params__2573__auto__))
415
+ query-params__2576__auto__ (apply
416
+ hash-map
417
+ (interleave
418
+ query-param-names__2574__auto__
419
+ (vec
420
+ (vals
421
+ (sort
422
+ (select-keys
423
+ rest-map__2572__auto__
424
+ provided-optional-params__2573__auto__))))))
425
+ oauth-creds__2577__auto__ (when
426
+ (and
427
+ *oauth-consumer*
428
+ *oauth-access-token*)
429
+ (oauth/credentials
430
+ *oauth-consumer*
431
+ *oauth-access-token*
432
+ :post
433
+ req-uri__2571__auto__
434
+ query-params__2576__auto__))]
421
435
((comp #(:content %) status-handler)
422
436
(http/post req-uri__2571__auto__
423
437
:query (merge query-params__2576__auto__ oauth-creds__2577__auto__)
@@ -430,7 +444,7 @@ take any required and optional arguments and call the associated Twitter method.
430
444
:post
431
445
" api.twitter.com/1/account/update_profile.json"
432
446
[]
433
- [:name
447
+ [:name
434
448
:email
435
449
:url
436
450
:location
@@ -605,21 +619,23 @@ take any required and optional arguments and call the associated Twitter method.
605
619
" Handle the various HTTP status codes that may be returned when accessing
606
620
the Twitter API."
607
621
[result]
608
- (condp #(if (coll? %1 )
622
+ (condp #(if (coll? %1 )
609
623
(first (filter (fn [x] (== x %2 )) %1 ))
610
624
(== %2 %1 )) (:code result)
611
625
200 result
612
626
304 nil
613
- [400 401 403 404 406 500 502 503 ] (let [body (:content result)
614
- headers (into {} (:headers result))
615
- error-msg (:error body)
616
- error-code (:code result)
617
- request-uri (:request body)]
618
- (throw (proxy [Exception] [(str " [" error-code " ] " error-msg " . [" request-uri " ]" )]
619
- (request [] (body " request" ))
620
- (remaining-requests [] (headers " X-RateLimit-Remaining" ))
621
- (rate-limit-reset [] (java.util.Date.
622
- (long (headers " X-RateLimit-Reset" )))))))))
627
+ [400 401 403 404 406 500 502 503 ]
628
+ (let [body (:content result)
629
+ headers (into {} (:headers result))
630
+ error-msg (:error body)
631
+ error-code (:code result)
632
+ request-uri (:request body)]
633
+ (throw (proxy [Exception]
634
+ [(str " [" error-code " ] " error-msg " . [" request-uri " ]" )]
635
+ (request [] (body " request" ))
636
+ (remaining-requests [] (headers " X-RateLimit-Remaining" ))
637
+ (rate-limit-reset [] (java.util.Date.
638
+ (long (headers " X-RateLimit-Reset" )))))))))
623
639
624
640
(defn make-rate-limit-handler
625
641
" Creates a handler that will only be called if the API rate limit has been exceeded."
0 commit comments