Commit a2219c88 authored by Mike's avatar Mike Committed by Joshua Roesslein
Browse files

Add compression support.

Conflicts:
	tweepy/api.py
parent c060f7b3
......@@ -28,3 +28,4 @@ Can Duruk
Jan Schaumann (@jschauma)
Stuart Powers
Jeff Hull (@jsh2134)
Mike (mikeandmore)
......@@ -18,7 +18,7 @@ class API(object):
host='api.twitter.com', search_host='search.twitter.com',
cache=None, secure=True, api_root='/1.1', search_root='',
retry_count=0, retry_delay=0, retry_errors=None, timeout=60,
parser=None):
parser=None, compression=False):
self.auth = auth_handler
self.host = host
self.search_host = search_host
......@@ -26,6 +26,7 @@ class API(object):
self.search_root = search_root
self.cache = cache
self.secure = secure
self.compression = compression
self.retry_count = retry_count
self.retry_delay = retry_delay
self.retry_errors = retry_errors
......
......@@ -6,6 +6,8 @@ import httplib
import urllib
import time
import re
from StringIO import StringIO
import gzip
from tweepy.error import TweepError
from tweepy.utils import convert_to_utf8_str
......@@ -140,6 +142,10 @@ def bind_api(**config):
self.method, self.headers, self.parameters
)
# Request compression if configured
if self.api.compression:
self.headers['Accept-encoding'] = 'gzip'
# Execute request
try:
conn.request(self.method, url, headers=self.headers, body=self.post_data)
......@@ -167,7 +173,14 @@ def bind_api(**config):
raise TweepError(error_msg, resp)
# Parse the response payload
result = self.api.parser.parse(self, resp.read())
body = resp.read()
if resp.getheader('Content-Encoding', '') == 'gzip':
try:
zipper = gzip.GzipFile(fileobj=StringIO(body))
body = zipper.read()
except Exception, e:
raise TweepError('Failed to decompress data: %s' % e)
result = self.api.parser.parse(self, body)
conn.close()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment