From 77c541a5133af8fd3b4d77383cb38ee61dfb0b0f Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sun, 31 Jul 2011 23:22:41 -0400 Subject: [PATCH] Accept gzipped data and decompress it in api_query(). --- wiki/tools/site.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/wiki/tools/site.py b/wiki/tools/site.py index 982cd30..db3b7d2 100644 --- a/wiki/tools/site.py +++ b/wiki/tools/site.py @@ -1,8 +1,10 @@ # -*- coding: utf-8 -*- from cookielib import CookieJar +from gzip import GzipFile from json import loads from re import escape as re_escape, match as re_match +from StringIO import StringIO from urllib import unquote_plus, urlencode from urllib2 import build_opener, HTTPCookieProcessor, URLError from urlparse import urlparse @@ -41,7 +43,8 @@ class Site(object): else: self._cookiejar = CookieJar() self._opener = build_opener(HTTPCookieProcessor(self._cookiejar)) - self._opener.addheaders = [('User-agent', USER_AGENT)] + self._opener.addheaders = [("User-Agent", USER_AGENT), + ("Accept-Encoding", "gzip")] # get all of the above attributes that were not specified as arguments self._load_attributes() @@ -270,6 +273,10 @@ class Site(object): raise SiteAPIError(e) else: result = response.read() + if response.headers.get("Content-Encoding") == "gzip": + stream = StringIO(result) + gzipper = GzipFile(fileobj=stream) + result = gzipper.read() return loads(result) # parse as a JSON object def name(self):