Use gzipped fetches from twitch

This commit is contained in:
Laszlo Zeke 2018-04-03 00:05:49 +02:00
parent ba12777444
commit 842da0100a
1 changed files with 9 additions and 1 deletions

View File

@ -23,6 +23,8 @@ import logging
from feedformatter import Feed from feedformatter import Feed
from google.appengine.api import memcache from google.appengine.api import memcache
from app_id import TWITCH_CLIENT_ID from app_id import TWITCH_CLIENT_ID
from StringIO import StringIO
import gzip
VODCACHE_PREFIX = 'vodcache' VODCACHE_PREFIX = 'vodcache'
@ -114,7 +116,8 @@ class RSSVoDServer(webapp2.RequestHandler):
url = url_template % id url = url_template % id
headers = { headers = {
'Accept': 'application/vnd.twitchtv.v5+json', '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) request = urllib2.Request(url, headers=headers)
retries = 0 retries = 0
@ -122,6 +125,11 @@ class RSSVoDServer(webapp2.RequestHandler):
try: try:
result = urllib2.urlopen(request, timeout=3) result = urllib2.urlopen(request, timeout=3)
logging.debug('Fetch from twitch for %s with code %s' % (id, result.getcode())) 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() return result.read()
except BaseException as e: except BaseException as e:
logging.warning("Fetch exception caught: %s" % e) logging.warning("Fetch exception caught: %s" % e)