From 842da0100a983dc2bb5b690edc1f7c9cc123cbb5 Mon Sep 17 00:00:00 2001 From: Laszlo Zeke Date: Tue, 3 Apr 2018 00:05:49 +0200 Subject: [PATCH] Use gzipped fetches from twitch --- TwitchRSS/twitchrss.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/TwitchRSS/twitchrss.py b/TwitchRSS/twitchrss.py index 5151cd3..5509aad 100644 --- a/TwitchRSS/twitchrss.py +++ b/TwitchRSS/twitchrss.py @@ -23,6 +23,8 @@ import logging from feedformatter import Feed from google.appengine.api import memcache from app_id import TWITCH_CLIENT_ID +from StringIO import StringIO +import gzip VODCACHE_PREFIX = 'vodcache' @@ -114,7 +116,8 @@ class RSSVoDServer(webapp2.RequestHandler): url = url_template % id headers = { 'Accept': 'application/vnd.twitchtv.v5+json', - 'Client-ID': TWITCH_CLIENT_ID + 'Client-ID': TWITCH_CLIENT_ID, + 'Accept-Encoding': 'gzip' } request = urllib2.Request(url, headers=headers) retries = 0 @@ -122,6 +125,11 @@ class RSSVoDServer(webapp2.RequestHandler): try: result = urllib2.urlopen(request, timeout=3) logging.debug('Fetch from twitch for %s with code %s' % (id, result.getcode())) + if result.info().get('Content-Encoding') == 'gzip': + logging.debug('Fetched gzip content') + buf = StringIO(result.read()) + f = gzip.GzipFile(fileobj=buf) + return f.read() return result.read() except BaseException as e: logging.warning("Fetch exception caught: %s" % e)