What’s the recommended way to handle Content-Encoding: gzip files when using urlgrabber?
Right now I’m monkey-patching it like this:
g = URLGrabber(http_headers=(("Accept-Encoding", "gzip"),))
g.is_compressed = False # I don't know yet if the server will send me compressed data
# Backup current method of handling downloaded headers
try:
PyCurlFileObject.orig_hdr_retrieve
except AttributeError:
PyCurlFileObject.orig_hdr_retrieve = PyCurlFileObject._hdr_retrieve
def hdr_retrieve(instance, buf):
r = PyCurlFileObject.orig_hdr_retrieve(instance, buf)
if "content-encoding" in buf.lower() and "zip" in buf.lower():
g.is_compressed = True
return r
PyCurlFileObject._hdr_retrieve = hdr_retrieve
g.urlgrab(url, dest)
if g.is_compressed:
# ungzip file here
But it doesn’t look very clean and I fear it’s not threadsafe either…
I think I’ve found a threadsafe solution:
but it still doesn’t feel quite “clean” 🙂