From: Mike Date: Sun, 23 Jan 2011 07:08:49 +0000 (+0800) Subject: Add compression support. X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=a2219c88a164597b503e17958af30aefdedff7cb;p=tweepy.git Add compression support. Conflicts: tweepy/api.py --- diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 7d95a42..12956df 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -28,3 +28,4 @@ Can Duruk Jan Schaumann (@jschauma) Stuart Powers Jeff Hull (@jsh2134) +Mike (mikeandmore) diff --git a/tweepy/api.py b/tweepy/api.py index f694334..3e9fe2a 100644 --- a/tweepy/api.py +++ b/tweepy/api.py @@ -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 diff --git a/tweepy/binder.py b/tweepy/binder.py index b4b91e6..c5e99b9 100644 --- a/tweepy/binder.py +++ b/tweepy/binder.py @@ -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()