]>
Commit | Line | Data |
---|---|---|
f0603331 | 1 | # encoding: utf-8 |
491792a2 | 2 | from __future__ import unicode_literals, print_function |
f0603331 | 3 | |
3930cc7b MV |
4 | try: |
5 | import urllib.request as urllib_request | |
6 | import urllib.error as urllib_error | |
7 | except ImportError: | |
8 | import urllib2 as urllib_request | |
9 | import urllib2 as urllib_error | |
7364ea65 | 10 | |
a403f1b3 JL |
11 | try: |
12 | from cStringIO import StringIO | |
13 | except ImportError: | |
6204d0da | 14 | from io import BytesIO as StringIO |
a403f1b3 | 15 | |
14fc6b25 MG |
16 | from .twitter_globals import POST_ACTIONS |
17 | from .auth import NoAuth | |
4e9d6343 | 18 | |
2ee65672 | 19 | import re |
745061c1 | 20 | import sys |
a403f1b3 | 21 | import gzip |
491792a2 | 22 | from time import sleep, time |
23dcf621 | 23 | |
24 | try: | |
dea9a3e7 | 25 | import http.client as http_client |
23dcf621 | 26 | except ImportError: |
dea9a3e7 | 27 | import httplib as http_client |
2ee65672 | 28 | |
4b12a3a0 | 29 | try: |
f1a8ed67 | 30 | import json |
4b12a3a0 | 31 | except ImportError: |
f1a8ed67 | 32 | import simplejson as json |
33 | ||
fcf08b18 | 34 | |
652c5402 MV |
35 | class _DEFAULT(object): |
36 | pass | |
37 | ||
fcf08b18 | 38 | |
5251ea48 | 39 | class TwitterError(Exception): |
21e3bd23 | 40 | """ |
64a8d213 B |
41 | Base Exception thrown by the Twitter object when there is a |
42 | general error interacting with the API. | |
21e3bd23 | 43 | """ |
5251ea48 | 44 | pass |
45 | ||
fcf08b18 | 46 | |
64a8d213 B |
47 | class TwitterHTTPError(TwitterError): |
48 | """ | |
49 | Exception thrown by the Twitter object when there is an | |
50 | HTTP error interacting with twitter.com. | |
51 | """ | |
1be4ce71 | 52 | def __init__(self, e, uri, format, uriparts): |
4b12a3a0 MV |
53 | self.e = e |
54 | self.uri = uri | |
55 | self.format = format | |
56 | self.uriparts = uriparts | |
7fe9aab6 HN |
57 | try: |
58 | data = self.e.fp.read() | |
dea9a3e7 | 59 | except http_client.IncompleteRead as e: |
7fe9aab6 HN |
60 | # can't read the error text |
61 | # let's try some of it | |
62 | data = e.partial | |
e9fc8d86 | 63 | if self.e.headers.get('Content-Encoding') == 'gzip': |
7fe9aab6 | 64 | buf = StringIO(data) |
84d2da3d | 65 | f = gzip.GzipFile(fileobj=buf) |
66 | self.response_data = f.read() | |
67 | else: | |
7fe9aab6 | 68 | self.response_data = data |
9f04d75d | 69 | super(TwitterHTTPError, self).__init__(str(self)) |
64a8d213 B |
70 | |
71 | def __str__(self): | |
57b54437 | 72 | fmt = ("." + self.format) if self.format else "" |
68b3e2ee | 73 | return ( |
57b54437 | 74 | "Twitter sent status %i for URL: %s%s using parameters: " |
fcf08b18 | 75 | "(%s)\ndetails: %s" % ( |
57b54437 | 76 | self.e.code, self.uri, fmt, self.uriparts, |
c7dd86d1 | 77 | self.response_data)) |
64a8d213 | 78 | |
fcf08b18 | 79 | |
84d0a294 MV |
80 | class TwitterResponse(object): |
81 | """ | |
82 | Response from a twitter request. Behaves like a list or a string | |
83 | (depending on requested format) but it has a few other interesting | |
84 | attributes. | |
85 | ||
86 | `headers` gives you access to the response headers as an | |
87 | httplib.HTTPHeaders instance. You can do | |
ba02331e | 88 | `response.headers.get('h')` to retrieve a header. |
84d0a294 | 89 | """ |
84d0a294 | 90 | |
84d0a294 MV |
91 | @property |
92 | def rate_limit_remaining(self): | |
93 | """ | |
94 | Remaining requests in the current rate-limit. | |
95 | """ | |
eeec9b00 IA |
96 | return int(self.headers.get('X-Rate-Limit-Remaining', "0")) |
97 | ||
98 | @property | |
99 | def rate_limit_limit(self): | |
100 | """ | |
c53558ad | 101 | The rate limit ceiling for that given request. |
eeec9b00 IA |
102 | """ |
103 | return int(self.headers.get('X-Rate-Limit-Limit', "0")) | |
84d0a294 MV |
104 | |
105 | @property | |
106 | def rate_limit_reset(self): | |
107 | """ | |
108 | Time in UTC epoch seconds when the rate limit will reset. | |
109 | """ | |
eeec9b00 | 110 | return int(self.headers.get('X-Rate-Limit-Reset', "0")) |
84d0a294 MV |
111 | |
112 | ||
ed23f46c MV |
113 | class TwitterDictResponse(dict, TwitterResponse): |
114 | pass | |
12bba6ac | 115 | |
abddd419 | 116 | |
ed23f46c MV |
117 | class TwitterListResponse(list, TwitterResponse): |
118 | pass | |
fcf08b18 | 119 | |
94803fc9 | 120 | |
ed23f46c MV |
121 | def wrap_response(response, headers): |
122 | response_typ = type(response) | |
123 | if response_typ is dict: | |
124 | res = TwitterDictResponse(response) | |
125 | res.headers = headers | |
126 | elif response_typ is list: | |
127 | res = TwitterListResponse(response) | |
128 | res.headers = headers | |
129 | else: | |
130 | res = response | |
131 | return res | |
abddd419 | 132 | |
4e5c4880 MV |
133 | def method_for_uri(uri): |
134 | method = "GET" | |
135 | for action in POST_ACTIONS: | |
136 | if re.search("%s(/\d+)?$" % action, uri): | |
137 | method = "POST" | |
138 | break | |
139 | return method | |
0d6c0646 | 140 | |
7364ea65 | 141 | class TwitterCall(object): |
dd648a25 | 142 | |
6d508c66 BB |
143 | TWITTER_UNAVAILABLE_WAIT = 30 # delay after HTTP codes 502, 503 or 504 |
144 | ||
c8d451e8 | 145 | def __init__( |
fcf08b18 | 146 | self, auth, format, domain, callable_cls, uri="", |
491792a2 | 147 | uriparts=None, secure=True, timeout=None, gzip=False, retry=False): |
568331a9 | 148 | self.auth = auth |
a55e6a11 | 149 | self.format = format |
153dee29 | 150 | self.domain = domain |
dd648a25 | 151 | self.callable_cls = callable_cls |
7364ea65 | 152 | self.uri = uri |
b0dedfc0 | 153 | self.uriparts = uriparts |
9a148ed1 | 154 | self.secure = secure |
effd06bb | 155 | self.timeout = timeout |
86318060 | 156 | self.gzip = gzip |
491792a2 | 157 | self.retry = retry |
fd2bc885 | 158 | |
7364ea65 | 159 | def __getattr__(self, k): |
160 | try: | |
161 | return object.__getattr__(self, k) | |
162 | except AttributeError: | |
e748eed8 | 163 | def extend_call(arg): |
164 | return self.callable_cls( | |
165 | auth=self.auth, format=self.format, domain=self.domain, | |
ff3ca197 | 166 | callable_cls=self.callable_cls, timeout=self.timeout, |
491792a2 | 167 | secure=self.secure, gzip=self.gzip, retry=self.retry, |
ff3ca197 | 168 | uriparts=self.uriparts + (arg,)) |
e748eed8 | 169 | if k == "_": |
170 | return extend_call | |
171 | else: | |
172 | return extend_call(k) | |
fd2bc885 | 173 | |
7364ea65 | 174 | def __call__(self, **kwargs): |
aec68959 | 175 | # Build the uri. |
1be4ce71 | 176 | uriparts = [] |
b0dedfc0 | 177 | for uripart in self.uriparts: |
aec68959 MV |
178 | # If this part matches a keyword argument, use the |
179 | # supplied value otherwise, just use the part. | |
f7e63802 MV |
180 | uriparts.append(str(kwargs.pop(uripart, uripart))) |
181 | uri = '/'.join(uriparts) | |
1be4ce71 | 182 | |
4e5c4880 | 183 | method = kwargs.pop('_method', None) or method_for_uri(uri) |
612ececa | 184 | |
aec68959 MV |
185 | # If an id kwarg is present and there is no id to fill in in |
186 | # the list of uriparts, assume the id goes at the end. | |
da45d039 MV |
187 | id = kwargs.pop('id', None) |
188 | if id: | |
fcf08b18 | 189 | uri += "/%s" % (id) |
4e9d6343 | 190 | |
920528cd MV |
191 | # If an _id kwarg is present, this is treated as id as a CGI |
192 | # param. | |
193 | _id = kwargs.pop('_id', None) | |
194 | if _id: | |
195 | kwargs['id'] = _id | |
be5f32da | 196 | |
8fd7289d IA |
197 | # If an _timeout is specified in kwargs, use it |
198 | _timeout = kwargs.pop('_timeout', None) | |
920528cd | 199 | |
568331a9 MH |
200 | secure_str = '' |
201 | if self.secure: | |
202 | secure_str = 's' | |
6c527e72 | 203 | dot = "" |
1be4ce71 | 204 | if self.format: |
6c527e72 | 205 | dot = "." |
fcf08b18 | 206 | uriBase = "http%s://%s/%s%s%s" % ( |
207 | secure_str, self.domain, uri, dot, self.format) | |
568331a9 | 208 | |
c1d973eb A |
209 | # Check if argument tells whether img is already base64 encoded |
210 | b64_convert = True | |
211 | if "_base64" in kwargs: | |
212 | b64_convert = not kwargs.pop("_base64") | |
213 | if b64_convert: | |
214 | import base64 | |
215 | ||
94fb8fab R |
216 | # Catch media arguments to handle oauth query differently for multipart |
217 | media = None | |
0e197382 | 218 | for arg in ['media[]']: |
94fb8fab R |
219 | if arg in kwargs: |
220 | media = kwargs.pop(arg) | |
525c9c31 | 221 | if b64_convert: |
525c9c31 | 222 | media = base64.b64encode(media) |
745061c1 R |
223 | if sys.version_info >= (3, 0): |
224 | media = str(media, 'utf8') | |
94fb8fab R |
225 | mediafield = arg |
226 | break | |
227 | ||
c1d973eb A |
228 | # Catch media arguments that are not accepted through multipart |
229 | # and are not yet base64 encoded | |
230 | if b64_convert: | |
0e197382 | 231 | for arg in ['banner', 'image']: |
c1d973eb A |
232 | if arg in kwargs: |
233 | kwargs[arg] = base64.b64encode(kwargs[arg]) | |
234 | ||
86318060 | 235 | headers = {'Accept-Encoding': 'gzip'} if self.gzip else dict() |
1ff50236 | 236 | body = None |
237 | arg_data = None | |
1be4ce71 | 238 | if self.auth: |
568331a9 | 239 | headers.update(self.auth.generate_headers()) |
94fb8fab R |
240 | # Use urlencoded oauth args with no params when sending media |
241 | # via multipart and send it directly via uri even for post | |
1ff50236 | 242 | arg_data = self.auth.encode_params( |
243 | uriBase, method, {} if media else kwargs) | |
94fb8fab | 244 | if method == 'GET' or media: |
1be4ce71 | 245 | uriBase += '?' + arg_data |
1be4ce71 | 246 | else: |
8eb73aab | 247 | body = arg_data.encode('utf8') |
c53558ad | 248 | |
94fb8fab R |
249 | # Handle query as multipart when sending media |
250 | if media: | |
251 | BOUNDARY = "###Python-Twitter###" | |
252 | bod = [] | |
253 | bod.append('--' + BOUNDARY) | |
1ff50236 | 254 | bod.append( |
255 | 'Content-Disposition: form-data; name="%s"' % mediafield) | |
525c9c31 | 256 | bod.append('Content-Transfer-Encoding: base64') |
94fb8fab R |
257 | bod.append('') |
258 | bod.append(media) | |
259 | for k, v in kwargs.items(): | |
260 | bod.append('--' + BOUNDARY) | |
261 | bod.append('Content-Disposition: form-data; name="%s"' % k) | |
262 | bod.append('') | |
263 | bod.append(v) | |
264 | bod.append('--' + BOUNDARY + '--') | |
c1d973eb | 265 | body = '\r\n'.join(bod).encode('utf8') |
1ff50236 | 266 | headers['Content-Type'] = \ |
267 | 'multipart/form-data; boundary=%s' % BOUNDARY | |
94fb8fab | 268 | |
3930cc7b | 269 | req = urllib_request.Request(uriBase, body, headers) |
491792a2 BB |
270 | if self.retry: |
271 | return self._handle_response_with_retry(req, uri, arg_data, _timeout) | |
272 | else: | |
273 | return self._handle_response(req, uri, arg_data, _timeout) | |
102acdb1 | 274 | |
8fd7289d | 275 | def _handle_response(self, req, uri, arg_data, _timeout=None): |
a5aab114 | 276 | kwargs = {} |
8fd7289d IA |
277 | if _timeout: |
278 | kwargs['timeout'] = _timeout | |
7364ea65 | 279 | try: |
a5aab114 | 280 | handle = urllib_request.urlopen(req, **kwargs) |
918b8b48 GC |
281 | if handle.headers['Content-Type'] in ['image/jpeg', 'image/png']: |
282 | return handle | |
0fdfdc3d DM |
283 | try: |
284 | data = handle.read() | |
dea9a3e7 | 285 | except http_client.IncompleteRead as e: |
0fdfdc3d DM |
286 | # Even if we don't get all the bytes we should have there |
287 | # may be a complete response in e.partial | |
288 | data = e.partial | |
289 | if handle.info().get('Content-Encoding') == 'gzip': | |
a403f1b3 | 290 | # Handle gzip decompression |
0fdfdc3d | 291 | buf = StringIO(data) |
a403f1b3 JL |
292 | f = gzip.GzipFile(fileobj=buf) |
293 | data = f.read() | |
c1d973eb A |
294 | if len(data) == 0: |
295 | return wrap_response({}, handle.headers) | |
296 | elif "json" == self.format: | |
a403f1b3 | 297 | res = json.loads(data.decode('utf8')) |
abddd419 | 298 | return wrap_response(res, handle.headers) |
de072195 | 299 | else: |
456ec92b | 300 | return wrap_response( |
a403f1b3 | 301 | data.decode('utf8'), handle.headers) |
3930cc7b | 302 | except urllib_error.HTTPError as e: |
de072195 | 303 | if (e.code == 304): |
7364ea65 | 304 | return [] |
de072195 | 305 | else: |
aec68959 | 306 | raise TwitterHTTPError(e, uri, self.format, arg_data) |
102acdb1 | 307 | |
491792a2 | 308 | def _handle_response_with_retry(self, req, uri, arg_data, _timeout=None): |
73a242d6 BB |
309 | retry = self.retry |
310 | while retry: | |
491792a2 BB |
311 | try: |
312 | return self._handle_response(req, uri, arg_data, _timeout) | |
313 | except TwitterHTTPError as e: | |
314 | if e.e.code == 429: | |
315 | # API rate limit reached | |
316 | reset = int(e.e.headers.get('X-Rate-Limit-Reset', time() + 30)) | |
317 | delay = int(reset - time() + 2) # add some extra margin | |
318 | print("API rate limit reached; waiting for %ds..." % delay, file=sys.stderr) | |
319 | elif e.e.code in (502, 503, 504): | |
6d508c66 | 320 | delay = self.TWITTER_UNAVAILABLE_WAIT |
491792a2 BB |
321 | print("Service unavailable; waiting for %ds..." % delay, file=sys.stderr) |
322 | else: | |
323 | raise | |
73a242d6 BB |
324 | if isinstance(retry, int): |
325 | if retry <= 0: | |
326 | raise | |
327 | retry -= 1 | |
491792a2 BB |
328 | sleep(delay) |
329 | ||
fcf08b18 | 330 | |
7364ea65 | 331 | class Twitter(TwitterCall): |
332 | """ | |
333 | The minimalist yet fully featured Twitter API class. | |
4e9d6343 | 334 | |
7364ea65 | 335 | Get RESTful data by accessing members of this class. The result |
336 | is decoded python objects (lists and dicts). | |
337 | ||
51e0b8f1 | 338 | The Twitter API is documented at: |
153dee29 | 339 | |
aec68959 MV |
340 | http://dev.twitter.com/doc |
341 | ||
4e9d6343 | 342 | |
7364ea65 | 343 | Examples:: |
4e9d6343 | 344 | |
d4f3123e MV |
345 | from twitter import * |
346 | ||
d09c0dd3 | 347 | t = Twitter( |
51e0b8f1 | 348 | auth=OAuth(token, token_key, con_secret, con_secret_key))) |
4e9d6343 | 349 | |
58ccea4e MV |
350 | # Get your "home" timeline |
351 | t.statuses.home_timeline() | |
4e9d6343 | 352 | |
d4f3123e MV |
353 | # Get a particular friend's timeline |
354 | t.statuses.user_timeline(screen_name="billybob") | |
355 | ||
356 | # to pass in GET/POST parameters, such as `count` | |
357 | t.statuses.home_timeline(count=5) | |
358 | ||
359 | # to pass in the GET/POST parameter `id` you need to use `_id` | |
360 | t.statuses.oembed(_id=1234567890) | |
d09c0dd3 MV |
361 | |
362 | # Update your status | |
363 | t.statuses.update( | |
364 | status="Using @sixohsix's sweet Python Twitter Tools.") | |
4e9d6343 | 365 | |
51e0b8f1 | 366 | # Send a direct message |
d09c0dd3 | 367 | t.direct_messages.new( |
51e0b8f1 MV |
368 | user="billybob", |
369 | text="I think yer swell!") | |
7364ea65 | 370 | |
d09c0dd3 MV |
371 | # Get the members of tamtar's list "Things That Are Rad" |
372 | t._("tamtar")._("things-that-are-rad").members() | |
373 | ||
374 | # Note how the magic `_` method can be used to insert data | |
375 | # into the middle of a call. You can also use replacement: | |
376 | t.user.list.members(user="tamtar", list="things-that-are-rad") | |
be5f32da | 377 | |
8fd7289d | 378 | # An *optional* `_timeout` parameter can also be used for API |
a5aab114 | 379 | # calls which take much more time than normal or twitter stops |
d4f3123e | 380 | # responding for some reason: |
a5aab114 IA |
381 | t.users.lookup( |
382 | screen_name=','.join(A_LIST_OF_100_SCREEN_NAMES), \ | |
8fd7289d | 383 | _timeout=1) |
a5aab114 | 384 | |
5a412b39 R |
385 | # Overriding Method: GET/POST |
386 | # you should not need to use this method as this library properly | |
387 | # detects whether GET or POST should be used, Nevertheless | |
388 | # to force a particular method, use `_method` | |
389 | t.statuses.oembed(_id=1234567890, _method='GET') | |
390 | ||
391 | # Send a tweet with an image included (or set your banner or logo similarily) | |
d4f3123e | 392 | # by just reading your image from the web or a file in a string: |
5a412b39 R |
393 | with open("example.png", "rb") as imagefile: |
394 | params = {"media[]": imagefile.read(), "status": "PTT"} | |
395 | t.statuses.update_with_media(**params) | |
d4f3123e | 396 | |
5a412b39 R |
397 | # Or by sending a base64 encoded image: |
398 | params = {"media[]": base64_image, "status": "PTT", "_base64": True} | |
399 | t.statuses.update_with_media(**params) | |
400 | ||
b0dedfc0 | 401 | |
153dee29 | 402 | Searching Twitter:: |
4e9d6343 | 403 | |
58ccea4e MV |
404 | # Search for the latest tweets about #pycon |
405 | t.search.tweets(q="#pycon") | |
153dee29 | 406 | |
7364ea65 | 407 | |
68b3e2ee MV |
408 | Using the data returned |
409 | ----------------------- | |
410 | ||
411 | Twitter API calls return decoded JSON. This is converted into | |
412 | a bunch of Python lists, dicts, ints, and strings. For example:: | |
7364ea65 | 413 | |
58ccea4e | 414 | x = twitter.statuses.home_timeline() |
7364ea65 | 415 | |
51e0b8f1 MV |
416 | # The first 'tweet' in the timeline |
417 | x[0] | |
7364ea65 | 418 | |
51e0b8f1 MV |
419 | # The screen name of the user who wrote the first 'tweet' |
420 | x[0]['user']['screen_name'] | |
4e9d6343 | 421 | |
4e9d6343 | 422 | |
68b3e2ee MV |
423 | Getting raw XML data |
424 | -------------------- | |
425 | ||
426 | If you prefer to get your Twitter data in XML format, pass | |
427 | format="xml" to the Twitter object when you instantiate it:: | |
4e9d6343 | 428 | |
51e0b8f1 | 429 | twitter = Twitter(format="xml") |
4e9d6343 | 430 | |
51e0b8f1 MV |
431 | The output will not be parsed in any way. It will be a raw string |
432 | of XML. | |
68b3e2ee | 433 | |
7364ea65 | 434 | """ |
45688301 | 435 | def __init__( |
fcf08b18 | 436 | self, format="json", |
437 | domain="api.twitter.com", secure=True, auth=None, | |
491792a2 | 438 | api_version=_DEFAULT, retry=False): |
7364ea65 | 439 | """ |
68b3e2ee MV |
440 | Create a new twitter API connector. |
441 | ||
442 | Pass an `auth` parameter to use the credentials of a specific | |
443 | user. Generally you'll want to pass an `OAuth` | |
69e1f98e MV |
444 | instance:: |
445 | ||
446 | twitter = Twitter(auth=OAuth( | |
447 | token, token_secret, consumer_key, consumer_secret)) | |
448 | ||
449 | ||
68b3e2ee | 450 | `domain` lets you change the domain you are connecting. By |
fcf08b18 | 451 | default it's `api.twitter.com`. |
68b3e2ee MV |
452 | |
453 | If `secure` is False you will connect with HTTP instead of | |
454 | HTTPS. | |
455 | ||
1cc9ab0b | 456 | `api_version` is used to set the base uri. By default it's |
fcf08b18 | 457 | '1.1'. |
491792a2 BB |
458 | |
459 | If `retry` is True, API rate limits will automatically be | |
460 | handled by waiting until the next reset, as indicated by | |
73a242d6 BB |
461 | the X-Rate-Limit-Reset HTTP header. If retry is an integer, |
462 | it defines the number of retries attempted. | |
7364ea65 | 463 | """ |
d20da7f3 MV |
464 | if not auth: |
465 | auth = NoAuth() | |
466 | ||
6c527e72 | 467 | if (format not in ("json", "xml", "")): |
fcf08b18 | 468 | raise ValueError("Unknown data format '%s'" % (format)) |
68b3e2ee | 469 | |
652c5402 | 470 | if api_version is _DEFAULT: |
82a93c03 | 471 | api_version = '1.1' |
652c5402 | 472 | |
1be4ce71 | 473 | uriparts = () |
68b3e2ee | 474 | if api_version: |
1be4ce71 | 475 | uriparts += (str(api_version),) |
68b3e2ee | 476 | |
9a148ed1 | 477 | TwitterCall.__init__( |
aec68959 | 478 | self, auth=auth, format=format, domain=domain, |
dd648a25 | 479 | callable_cls=TwitterCall, |
491792a2 | 480 | secure=secure, uriparts=uriparts, retry=retry) |
7e43e2ed | 481 | |
7364ea65 | 482 | |
abddd419 | 483 | __all__ = ["Twitter", "TwitterError", "TwitterHTTPError", "TwitterResponse"] |