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
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)
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 []
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::