From ffd1719c53d687a9bc07c1cbae49d4abd8b19670 Mon Sep 17 00:00:00 2001 From: alexvanin Date: Fri, 27 Mar 2020 22:53:31 +0300 Subject: [PATCH] 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. --- main.py | 2 +- twitch.py | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/main.py b/main.py index f960ab7..356fd26 100755 --- a/main.py +++ b/main.py @@ -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): diff --git a/twitch.py b/twitch.py index d105edc..eb14ae2 100644 --- a/twitch.py +++ b/twitch.py @@ -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) - 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) + while len(data.get('data', [])) != 0: result['streams'].extend(data['data']) + data = self.do_q(q_template.format(base, lang, 100, data['pagination']['cursor'], game_id), header) result['_total'] = len(result['streams']) return result