X-Git-Url: https://jfr.im/git/z_archive/twitter.git/blobdiff_plain/bbf3f5fb3eb28aef322a1b94b77ba5089ba7e858..e4bec6bdddd1e2b2b0f4db950adf28d0b711f44a:/twitter/api.py diff --git a/twitter/api.py b/twitter/api.py index c0f791f..d1ded34 100644 --- a/twitter/api.py +++ b/twitter/api.py @@ -21,7 +21,10 @@ try: except ImportError: import httplib as http_client -import json +try: + import json +except ImportError: + import simplejson as json class _DEFAULT(object): @@ -56,6 +59,7 @@ class TwitterHTTPError(TwitterError): self.response_data = f.read() else: self.response_data = data + super(TwitterHTTPError, self).__init__(str(self)) def __str__(self): fmt = ("." + self.format) if self.format else "" @@ -125,7 +129,7 @@ class TwitterCall(object): def __init__( self, auth, format, domain, callable_cls, uri="", - uriparts=None, secure=True, timeout=None): + uriparts=None, secure=True, timeout=None, gzip=False): self.auth = auth self.format = format self.domain = domain @@ -134,6 +138,7 @@ class TwitterCall(object): self.uriparts = uriparts self.secure = secure self.timeout = timeout + self.gzip = gzip def __getattr__(self, k): try: @@ -142,9 +147,9 @@ class TwitterCall(object): def extend_call(arg): return self.callable_cls( auth=self.auth, format=self.format, domain=self.domain, - callable_cls=self.callable_cls, timeout=self.timeout, uriparts=self.uriparts \ - + (arg,), - secure=self.secure) + callable_cls=self.callable_cls, timeout=self.timeout, + secure=self.secure, gzip=self.gzip, + uriparts=self.uriparts + (arg,)) if k == "_": return extend_call else: @@ -191,16 +196,45 @@ class TwitterCall(object): uriBase = "http%s://%s/%s%s%s" %( secure_str, self.domain, uri, dot, self.format) - headers = {'Accept-Encoding': 'gzip'} + # Catch media arguments to handle oauth query differently for multipart + media = None + for arg in ['media[]', 'banner', 'image']: + if arg in kwargs: + media = kwargs.pop(arg) + mediafield = arg + break + + headers = {'Accept-Encoding': 'gzip'} if self.gzip else dict() + body = None; arg_data = None if self.auth: headers.update(self.auth.generate_headers()) - arg_data = self.auth.encode_params(uriBase, method, kwargs) - if method == 'GET': + # Use urlencoded oauth args with no params when sending media + # via multipart and send it directly via uri even for post + arg_data = self.auth.encode_params(uriBase, method, + {} if media else kwargs ) + if method == 'GET' or media: uriBase += '?' + arg_data - body = None else: body = arg_data.encode('utf8') + # Handle query as multipart when sending media + if media: + BOUNDARY = "###Python-Twitter###" + bod = [] + bod.append('--' + BOUNDARY) + bod.append('Content-Disposition: form-data; name="%s"' % + mediafield) + bod.append('') + bod.append(media) + for k, v in kwargs.items(): + bod.append('--' + BOUNDARY) + bod.append('Content-Disposition: form-data; name="%s"' % k) + bod.append('') + bod.append(v) + bod.append('--' + BOUNDARY + '--') + body = '\r\n'.join(bod) + headers['Content-Type'] = 'multipart/form-data; boundary=%s' % BOUNDARY + req = urllib_request.Request(uriBase, body, headers) return self._handle_response(req, uri, arg_data, _timeout) @@ -255,11 +289,8 @@ class Twitter(TwitterCall): # Get your "home" timeline t.statuses.home_timeline() - # Get a particular friend's timeline - t.statuses.friends_timeline(id="billybob") - - # Also supported (but totally weird) - t.statuses.friends_timeline.billybob() + # Get a particular friend's tweets + t.statuses.user_timeline(user_id="billybob") # Update your status t.statuses.update(