Skip to content

Commit a2219c8

Browse files
mikeandmorejoshthecoder
authored andcommitted
Add compression support.
Conflicts: tweepy/api.py
1 parent c060f7b commit a2219c8

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

CONTRIBUTORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ Can Duruk
2828
Jan Schaumann (@jschauma)
2929
Stuart Powers
3030
Jeff Hull (@jsh2134)
31+
Mike (mikeandmore)

tweepy/api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ def __init__(self, auth_handler=None,
1818
host='api.twitter.com', search_host='search.twitter.com',
1919
cache=None, secure=True, api_root='/1.1', search_root='',
2020
retry_count=0, retry_delay=0, retry_errors=None, timeout=60,
21-
parser=None):
21+
parser=None, compression=False):
2222
self.auth = auth_handler
2323
self.host = host
2424
self.search_host = search_host
2525
self.api_root = api_root
2626
self.search_root = search_root
2727
self.cache = cache
2828
self.secure = secure
29+
self.compression = compression
2930
self.retry_count = retry_count
3031
self.retry_delay = retry_delay
3132
self.retry_errors = retry_errors

tweepy/binder.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import urllib
77
import time
88
import re
9+
from StringIO import StringIO
10+
import gzip
911

1012
from tweepy.error import TweepError
1113
from tweepy.utils import convert_to_utf8_str
@@ -140,6 +142,10 @@ def execute(self):
140142
self.method, self.headers, self.parameters
141143
)
142144

145+
# Request compression if configured
146+
if self.api.compression:
147+
self.headers['Accept-encoding'] = 'gzip'
148+
143149
# Execute request
144150
try:
145151
conn.request(self.method, url, headers=self.headers, body=self.post_data)
@@ -167,7 +173,14 @@ def execute(self):
167173
raise TweepError(error_msg, resp)
168174

169175
# Parse the response payload
170-
result = self.api.parser.parse(self, resp.read())
176+
body = resp.read()
177+
if resp.getheader('Content-Encoding', '') == 'gzip':
178+
try:
179+
zipper = gzip.GzipFile(fileobj=StringIO(body))
180+
body = zipper.read()
181+
except Exception, e:
182+
raise TweepError('Failed to decompress data: %s' % e)
183+
result = self.api.parser.parse(self, body)
171184

172185
conn.close()
173186

0 commit comments

Comments
 (0)