X-Git-Url: https://jfr.im/git/z_archive/twitter.git/blobdiff_plain/51e0b8f18c733f6095d76c39ba80e8886f8235a7..309620de933db096de639d87e4f93be460d54a86:/twitter/api.py diff --git a/twitter/api.py b/twitter/api.py index 7611ebb..8ed3ad8 100644 --- a/twitter/api.py +++ b/twitter/api.py @@ -5,10 +5,16 @@ except ImportError: import urllib2 as urllib_request import urllib2 as urllib_error +try: + from cStringIO import StringIO +except ImportError: + from StringIO import StringIO + from twitter.twitter_globals import POST_ACTIONS from twitter.auth import NoAuth import re +import gzip try: import json @@ -153,7 +159,7 @@ class TwitterCall(object): uriBase = "http%s://%s/%s%s%s" %( secure_str, self.domain, uri, dot, self.format) - headers = {} + headers = {'Accept-Encoding': 'gzip'} if self.auth: headers.update(self.auth.generate_headers()) arg_data = self.auth.encode_params(uriBase, method, kwargs) @@ -169,12 +175,22 @@ class TwitterCall(object): def _handle_response(self, req, uri, arg_data): try: handle = urllib_request.urlopen(req) + if handle.headers['Content-Type'] in ['image/jpeg', 'image/png']: + return handle + elif handle.info().get('Content-Encoding') == 'gzip': + # Handle gzip decompression + buf = StringIO(handle.read()) + f = gzip.GzipFile(fileobj=buf) + data = f.read() + else: + data = handle.read() + if "json" == self.format: - res = json.loads(handle.read().decode('utf8')) + res = json.loads(data.decode('utf8')) return wrap_response(res, handle.headers) else: return wrap_response( - handle.read().decode('utf8'), handle.headers) + data.decode('utf8'), handle.headers) except urllib_error.HTTPError as e: if (e.code == 304): return [] @@ -195,25 +211,33 @@ class Twitter(TwitterCall): Examples:: - twitter = Twitter( + t = Twitter( auth=OAuth(token, token_key, con_secret, con_secret_key))) # Get the public timeline - twitter.statuses.public_timeline() + t.statuses.public_timeline() # Get a particular friend's timeline - twitter.statuses.friends_timeline(id="billybob") + t.statuses.friends_timeline(id="billybob") # Also supported (but totally weird) - twitter.statuses.friends_timeline.billybob() + t.statuses.friends_timeline.billybob() + + # Update your status + t.statuses.update( + status="Using @sixohsix's sweet Python Twitter Tools.") # Send a direct message - twitter.direct_messages.new( + t.direct_messages.new( user="billybob", text="I think yer swell!") - # Get the members of a particular list of a particular friend - twitter.user.listname.members(user="billybob", listname="billysbuds") + # Get the members of tamtar's list "Things That Are Rad" + t._("tamtar")._("things-that-are-rad").members() + + # Note how the magic `_` method can be used to insert data + # into the middle of a call. You can also use replacement: + t.user.list.members(user="tamtar", list="things-that-are-rad") Searching Twitter::