Created
May 25, 2012 14:55
-
-
Save j2labs/2788569 to your computer and use it in GitHub Desktop.
gzip with requests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import gzip | |
class GzipWrap(object): | |
"""The normal gzip interface requires writing / reading from gzipped files | |
on the file system. I think that will cause significant slow-down in | |
performance so this is a wrapper that implements a file-like interface, but | |
works entirely from memory instead. | |
Got this little trick from stack overflow: http://bit.ly/AjFHAL | |
This strategy is more efficient than using StringIO because I don't need to | |
open multiple file-like interfaces. StringIO would require one for reading | |
and one for writing. | |
We can see an example here: http://bit.ly/ApiTRG | |
""" | |
def __init__(self, input, filename=None): | |
"""Connects the class instance to the input, which is a file pointer, | |
and creates a `GzipFile` instance with `self` as the fileobj argument. | |
""" | |
self.input = input | |
self.buffer = '' | |
self.offset = 0 | |
self.zipper = gzip.GzipFile(filename, mode='wb', fileobj=self) | |
self._zip_read() | |
def _zip_read(self): | |
for s in self.input: | |
self.zipper.write(s) | |
self.zipper.flush() | |
self.zipper.close() | |
def read(self, size=-1): | |
if (size < 0): | |
return self.buffer | |
elif (size > 0) and (self.offset + size) < len(self.buffer): | |
start = self.offset | |
end = self.offset + size | |
chunk = self.buffer[start:end] | |
self.offset += size | |
return chunk | |
else: | |
chunk = self.buffer[self.offset:] | |
self.offset = len(self.buffer) | |
return chunk | |
def flush(self): | |
pass | |
def seek(self, offset): | |
self.offset = offset | |
def tell(self): | |
return self.offset | |
def write(self, data): | |
self.buffer += data | |
def close(self): | |
self.input.close() | |
data = 'This is my data that I want to compress and send in' | |
gzdata = GzipWrap(data).read() | |
r = requests.post('https://stream.twitter.com/1/statuses/filter.json', data=gzdata)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment