api: Update game search queries for twitch api v5 and v6

There is a change in TwitchAPIv5 for game search. Now it uses
query keyword without game name specification or language.

There is a change in TwitchAPIv6 for game search. Not it is
not guaranteed that search query returns 100 streams at max.
Application must fetching new data until query returns 0 streams.
This commit is contained in:
Alex Vanin 2020-03-27 22:53:31 +03:00
parent 6904b11f74
commit ffd1719c53
2 changed files with 8 additions and 9 deletions

View file

@ -5,7 +5,7 @@ import cherrypy
from cherrypy.process.plugins import Daemonizer
from twitch import TwitchClient
ver = '1.7'
ver = '1.8'
class FleastServer(object):

View file

@ -114,23 +114,23 @@ class TwitchClient:
https://dev.twitch.tv/docs/v5/reference/search/#search-streams
"""
header, base = self.get_base('v5')
init_q_template = '{}/streams/?game={}&language={}&limit={}&stream_type=live'
q_template = '{}/streams/?game={}&language={}&limit={}&stream_type=live&offset={}'
data = self.do_q(init_q_template.format(base, name, lang, 100), header)
init_q_template = '{}/search/streams?query={}&limit={}'
q_template = '{}/search/streams?query={}&limit={}&offset={}'
data = self.do_q(init_q_template.format(base, name, 100), header)
if data is None:
return []
total = data['_total']
streams = len(data['streams'])
while total > streams:
r = self.do_q(q_template.format(base, name, lang, 100, streams), header)
r = self.do_q(q_template.format(base, name, 100, streams), header)
if r is None:
return None
data['streams'].extend(r['streams'])
total = r['_total']
streams = len(data['streams'])
# Tweak for getting only live sterams
data['streams'] = [x for x in data['streams'] if x['stream_type'] == 'live']
data['streams'] = [x for x in data['streams'] if x['stream_type'] == 'live' and x['channel']['language'] == lang and x['game'] == name ]
data['_total'] = len(data['streams'])
return data
@ -147,9 +147,8 @@ class TwitchClient:
result = {'_total': 0, 'streams': []}
data = self.do_q(init_q_template.format(base, lang, 100, game_id), header)
while len(data.get('data', [])) != 0:
result['streams'].extend(data['data'])
while len(data['data']) == 100:
data = self.do_q(q_template.format(base, lang, 100, data['pagination']['cursor'], game_id), header)
result['streams'].extend(data['data'])
result['_total'] = len(result['streams'])
return result