Explorar el Código

Accept gzipped data and decompress it in api_query().

tags/v0.1^2
Ben Kurtovic hace 13 años
padre
commit
77c541a513
Se han modificado 1 ficheros con 8 adiciones y 1 borrados
  1. +8
    -1
      wiki/tools/site.py

+ 8
- 1
wiki/tools/site.py Ver fichero

@@ -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):


Cargando…
Cancelar
Guardar