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 cherrypy.process.plugins import Daemonizer
from twitch import TwitchClient from twitch import TwitchClient
ver = '1.7' ver = '1.8'
class FleastServer(object): class FleastServer(object):

View file

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