]> jfr.im git - z_archive/twitter.git/blobdiff - twitter/api.py
Merge branch 'master' of git://github.com/jameslafa/twitter
[z_archive/twitter.git] / twitter / api.py
index 7611ebb9c82eeaa8159aad228431d9900360e8a0..8ed3ad8090c8d3a8cb362c1db67ea6ac31b4e53b 100644 (file)
@@ -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::