2 from __future__
import unicode_literals
, print_function
4 from .util
import PY_3_OR_HIGHER
, actually_bytes
7 import urllib
.request
as urllib_request
8 import urllib
.error
as urllib_error
10 import urllib2
as urllib_request
11 import urllib2
as urllib_error
14 from cStringIO
import StringIO
16 from io
import BytesIO
as StringIO
18 from .twitter_globals
import POST_ACTIONS
19 from .auth
import NoAuth
24 from time
import sleep
, time
27 import http
.client
as http_client
29 import httplib
as http_client
34 import simplejson
as json
37 class _DEFAULT(object):
41 class TwitterError(Exception):
43 Base Exception thrown by the Twitter object when there is a
44 general error interacting with the API.
49 class TwitterHTTPError(TwitterError
):
51 Exception thrown by the Twitter object when there is an
52 HTTP error interacting with twitter.com.
54 def __init__(self
, e
, uri
, format
, uriparts
):
58 self
.uriparts
= uriparts
60 data
= self
.e
.fp
.read()
61 except http_client
.IncompleteRead
as e
:
62 # can't read the error text
63 # let's try some of it
65 if self
.e
.headers
.get('Content-Encoding') == 'gzip':
67 f
= gzip
.GzipFile(fileobj
=buf
)
68 self
.response_data
= f
.read()
70 self
.response_data
= data
71 super(TwitterHTTPError
, self
).__init
__(str(self
))
74 fmt
= ("." + self
.format
) if self
.format
else ""
76 "Twitter sent status %i for URL: %s%s using parameters: "
77 "(%s)\ndetails: %s" % (
78 self
.e
.code
, self
.uri
, fmt
, self
.uriparts
,
82 class TwitterResponse(object):
84 Response from a twitter request. Behaves like a list or a string
85 (depending on requested format) but it has a few other interesting
88 `headers` gives you access to the response headers as an
89 httplib.HTTPHeaders instance. You can do
90 `response.headers.get('h')` to retrieve a header.
94 def rate_limit_remaining(self
):
96 Remaining requests in the current rate-limit.
98 return int(self
.headers
.get('X-Rate-Limit-Remaining', "0"))
101 def rate_limit_limit(self
):
103 The rate limit ceiling for that given request.
105 return int(self
.headers
.get('X-Rate-Limit-Limit', "0"))
108 def rate_limit_reset(self
):
110 Time in UTC epoch seconds when the rate limit will reset.
112 return int(self
.headers
.get('X-Rate-Limit-Reset', "0"))
115 class TwitterDictResponse(dict, TwitterResponse
):
119 class TwitterListResponse(list, TwitterResponse
):
123 def wrap_response(response
, headers
):
124 response_typ
= type(response
)
125 if response_typ
is dict:
126 res
= TwitterDictResponse(response
)
127 res
.headers
= headers
128 elif response_typ
is list:
129 res
= TwitterListResponse(response
)
130 res
.headers
= headers
136 POST_ACTIONS_RE
= re
.compile('(' + '|'.join(POST_ACTIONS
) + r
')(/\d+)?$')
138 def method_for_uri(uri
):
139 if POST_ACTIONS_RE
.search(uri
):
144 def build_uri(orig_uriparts
, kwargs
):
146 Build the URI from the original uriparts and kwargs. Modifies kwargs.
149 for uripart
in orig_uriparts
:
150 # If this part matches a keyword argument (starting with _), use
151 # the supplied value. Otherwise, just use the part.
152 if uripart
.startswith("_"):
153 part
= (str(kwargs
.pop(uripart
, uripart
)))
156 uriparts
.append(part
)
157 uri
= '/'.join(uriparts
)
159 # If an id kwarg is present and there is no id to fill in in
160 # the list of uriparts, assume the id goes at the end.
161 id = kwargs
.pop('id', None)
168 class TwitterCall(object):
170 TWITTER_UNAVAILABLE_WAIT
= 30 # delay after HTTP codes 502, 503 or 504
173 self
, auth
, format
, domain
, callable_cls
, uri
="",
174 uriparts
=None, secure
=True, timeout
=None, gzip
=False, retry
=False):
178 self
.callable_cls
= callable_cls
180 self
.uriparts
= uriparts
182 self
.timeout
= timeout
186 def __getattr__(self
, k
):
188 return object.__getattr
__(self
, k
)
189 except AttributeError:
190 def extend_call(arg
):
191 return self
.callable_cls(
192 auth
=self
.auth
, format
=self
.format
, domain
=self
.domain
,
193 callable_cls
=self
.callable_cls
, timeout
=self
.timeout
,
194 secure
=self
.secure
, gzip
=self
.gzip
, retry
=self
.retry
,
195 uriparts
=self
.uriparts
+ (arg
,))
199 return extend_call(k
)
201 def __call__(self
, **kwargs
):
202 kwargs
= dict(kwargs
)
203 uri
= build_uri(self
.uriparts
, kwargs
)
204 method
= kwargs
.pop('_method', None) or method_for_uri(uri
)
207 # If an _id kwarg is present, this is treated as id as a CGI
209 _id
= kwargs
.pop('_id', None)
213 # If an _timeout is specified in kwargs, use it
214 _timeout
= kwargs
.pop('_timeout', None)
222 url_base
= "http%s://%s/%s%s%s" % (
223 secure_str
, domain
, uri
, dot
, self
.format
)
225 # Check if argument tells whether img is already base64 encoded
226 b64_convert
= not kwargs
.pop("_base64", False)
230 # Catch media arguments to handle oauth query differently for multipart
232 if 'media' in kwargs
:
234 media
= kwargs
.pop('media')
236 elif 'media[]' in kwargs
:
237 mediafield
= 'media[]'
238 media
= kwargs
.pop('media[]')
240 media
= base64
.b64encode(media
)
243 # Catch media arguments that are not accepted through multipart
244 # and are not yet base64 encoded
246 for arg
in ['banner', 'image']:
248 kwargs
[arg
] = base64
.b64encode(kwargs
[arg
])
250 headers
= {'Accept-Encoding': 'gzip'}
if self
.gzip
else dict()
254 headers
.update(self
.auth
.generate_headers())
255 # Use urlencoded oauth args with no params when sending media
256 # via multipart and send it directly via uri even for post
257 arg_data
= self
.auth
.encode_params(
258 url_base
, method
, {} if media
else kwargs
)
259 if method
== 'GET' or media
:
260 url_base
+= '?' + arg_data
262 body
= arg_data
.encode('utf-8')
264 # Handle query as multipart when sending media
266 BOUNDARY
= b
"###Python-Twitter###"
268 bod
.append(b
'--' + BOUNDARY
)
270 b
'Content-Disposition: form-data; name="'
271 + actually_bytes(mediafield
)
273 bod
.append(b
'Content-Type: application/octet-stream')
275 bod
.append(b
'Content-Transfer-Encoding: base64')
277 bod
.append(actually_bytes(media
))
278 for k
, v
in kwargs
.items():
279 k
= actually_bytes(k
)
280 v
= actually_bytes(v
)
281 bod
.append(b
'--' + BOUNDARY
)
282 bod
.append(b
'Content-Disposition: form-data; name="' + k
+ b
'"')
283 bod
.append(b
'Content-Type: text/plain;charset=utf-8')
286 bod
.append(b
'--' + BOUNDARY
+ b
'--')
289 body
= b
'\r\n'.join(bod
)
290 # print(body.decode('utf-8', errors='ignore'))
291 headers
[b
'Content-Type'] = \
292 b
'multipart/form-data; boundary=' + BOUNDARY
295 headers
[actually_bytes(k
)] = actually_bytes(headers
.pop(k
))
298 if not PY_3_OR_HIGHER
:
299 url_base
= url_base
.encode("utf-8")
301 req
= urllib_request
.Request(url_base
, data
=body
, headers
=headers
)
303 return self
._handle
_response
_with
_retry
(req
, uri
, arg_data
, _timeout
)
305 return self
._handle
_response
(req
, uri
, arg_data
, _timeout
)
307 def _handle_response(self
, req
, uri
, arg_data
, _timeout
=None):
310 kwargs
['timeout'] = _timeout
312 handle
= urllib_request
.urlopen(req
, **kwargs
)
313 if handle
.headers
['Content-Type'] in ['image/jpeg', 'image/png']:
317 except http_client
.IncompleteRead
as e
:
318 # Even if we don't get all the bytes we should have there
319 # may be a complete response in e.partial
321 if handle
.info().get('Content-Encoding') == 'gzip':
322 # Handle gzip decompression
324 f
= gzip
.GzipFile(fileobj
=buf
)
327 return wrap_response({}, handle
.headers
)
328 elif "json" == self
.format
:
329 res
= json
.loads(data
.decode('utf8'))
330 return wrap_response(res
, handle
.headers
)
332 return wrap_response(
333 data
.decode('utf8'), handle
.headers
)
334 except urllib_error
.HTTPError
as e
:
338 raise TwitterHTTPError(e
, uri
, self
.format
, arg_data
)
340 def _handle_response_with_retry(self
, req
, uri
, arg_data
, _timeout
=None):
344 return self
._handle
_response
(req
, uri
, arg_data
, _timeout
)
345 except TwitterHTTPError
as e
:
347 # API rate limit reached
348 reset
= int(e
.e
.headers
.get('X-Rate-Limit-Reset', time() + 30))
349 delay
= int(reset
- time() + 2) # add some extra margin
350 print("API rate limit reached; waiting for %ds..." % delay
, file=sys
.stderr
)
351 elif e
.e
.code
in (502, 503, 504):
352 delay
= self
.TWITTER_UNAVAILABLE_WAIT
353 print("Service unavailable; waiting for %ds..." % delay
, file=sys
.stderr
)
356 if isinstance(retry
, int):
363 class Twitter(TwitterCall
):
365 The minimalist yet fully featured Twitter API class.
367 Get RESTful data by accessing members of this class. The result
368 is decoded python objects (lists and dicts).
370 The Twitter API is documented at:
372 http://dev.twitter.com/doc
377 from twitter import *
380 auth=OAuth(token, token_key, con_secret, con_secret_key)))
382 # Get your "home" timeline
383 t.statuses.home_timeline()
385 # Get a particular friend's timeline
386 t.statuses.user_timeline(screen_name="billybob")
388 # to pass in GET/POST parameters, such as `count`
389 t.statuses.home_timeline(count=5)
391 # to pass in the GET/POST parameter `id` you need to use `_id`
392 t.statuses.oembed(_id=1234567890)
396 status="Using @sixohsix's sweet Python Twitter Tools.")
398 # Send a direct message
399 t.direct_messages.new(
401 text="I think yer swell!")
403 # Get the members of tamtar's list "Things That Are Rad"
404 t._("tamtar")._("things-that-are-rad").members()
406 # Note how the magic `_` method can be used to insert data
407 # into the middle of a call. You can also use replacement:
408 t.user.list.members(user="tamtar", list="things-that-are-rad")
410 # An *optional* `_timeout` parameter can also be used for API
411 # calls which take much more time than normal or twitter stops
412 # responding for some reason:
414 screen_name=','.join(A_LIST_OF_100_SCREEN_NAMES), \
417 # Overriding Method: GET/POST
418 # you should not need to use this method as this library properly
419 # detects whether GET or POST should be used, Nevertheless
420 # to force a particular method, use `_method`
421 t.statuses.oembed(_id=1234567890, _method='GET')
423 # Send a tweet with an image included (or set your banner or logo similarily)
424 # by just reading your image from the web or a file in a string:
426 with open("example.png", "rb") as imagefile:
427 params = {"media[]": imagefile.read(), "status": status}
428 t.statuses.update_with_media(**params)
430 # Or by sending a base64 encoded image:
431 params = {"media[]": base64_image, "status": status, "_base64": True}
432 t.statuses.update_with_media(**params)
437 # Search for the latest tweets about #pycon
438 t.search.tweets(q="#pycon")
441 Using the data returned
442 -----------------------
444 Twitter API calls return decoded JSON. This is converted into
445 a bunch of Python lists, dicts, ints, and strings. For example::
447 x = twitter.statuses.home_timeline()
449 # The first 'tweet' in the timeline
452 # The screen name of the user who wrote the first 'tweet'
453 x[0]['user']['screen_name']
459 If you prefer to get your Twitter data in XML format, pass
460 format="xml" to the Twitter object when you instantiate it::
462 twitter = Twitter(format="xml")
464 The output will not be parsed in any way. It will be a raw string
470 domain
="api.twitter.com", secure
=True, auth
=None,
471 api_version
=_DEFAULT
, retry
=False):
473 Create a new twitter API connector.
475 Pass an `auth` parameter to use the credentials of a specific
476 user. Generally you'll want to pass an `OAuth`
479 twitter = Twitter(auth=OAuth(
480 token, token_secret, consumer_key, consumer_secret))
483 `domain` lets you change the domain you are connecting. By
484 default it's `api.twitter.com`.
486 If `secure` is False you will connect with HTTP instead of
489 `api_version` is used to set the base uri. By default it's
492 If `retry` is True, API rate limits will automatically be
493 handled by waiting until the next reset, as indicated by
494 the X-Rate-Limit-Reset HTTP header. If retry is an integer,
495 it defines the number of retries attempted.
500 if (format
not in ("json", "xml", "")):
501 raise ValueError("Unknown data format '%s'" % (format
))
503 if api_version
is _DEFAULT
:
508 uriparts
+= (str(api_version
),)
510 TwitterCall
.__init
__(
511 self
, auth
=auth
, format
=format
, domain
=domain
,
512 callable_cls
=TwitterCall
,
513 secure
=secure
, uriparts
=uriparts
, retry
=retry
)
516 __all__
= ["Twitter", "TwitterError", "TwitterHTTPError", "TwitterResponse"]