api: Use circular api calls until there are results

This commit is contained in:
Alex Vanin 2020-05-16 15:53:11 +03:00
parent 4de664eadc
commit e53f7f02c5
2 changed files with 16 additions and 7 deletions

View file

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

View file

@ -138,7 +138,8 @@ class TwitchClient:
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' and x['channel']['language'] == lang and x['game'] == name ]
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
@ -160,11 +161,10 @@ class TwitchClient:
q_template = "{}/streams?language={}&first={}&after={}&game_id={}"
data = self.do_q(init_q_template.format(base, lang, 100, game_id[0]), header)
result['streams'].extend(data['data'])
while len(data.get('data', [])) > 0.8*100:
while len(data.get('data', [])) > 0: # there must be non zero value, but search is kinda broken now
result['streams'].extend(data['data'])
data = self.do_q(q_template.format(base, lang, 100, data['pagination']['cursor'], game_id[0]), header)
result['_total'] = len(result['streams'])
return result
return self.unique_streams_v6(result)
def get_irl_live_streams_v6(self, lang):
header, base = self.get_base('v6')
@ -180,8 +180,17 @@ 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.get('data', [])) > 0.8*100:
while len(data.get('data', [])) > 0: # there must be non zero value, but search is kinda broken now
result['streams'].extend(data['data'])
data = self.do_q(q_template.format(base, lang, 100, data['pagination']['cursor'], game_id), header)
return self.unique_streams_v6(result)
def unique_streams_v6(self, result):
uniq_streams = []
streams = sorted(result['streams'], key=lambda k: k['viewer_count'])
for s in streams:
if s['user_name'] not in uniq_streams:
uniq_streams.append(s['user_name'])
result['streams'] = uniq_streams
result['_total'] = len(result['streams'])
return result