api: Save any non empty results during game search and use char escaping in game search

This commit is contained in:
Alex Vanin 2020-03-28 18:40:36 +03:00
parent ee823ab215
commit 1053dfad9a
2 changed files with 6 additions and 4 deletions

View file

@ -68,7 +68,7 @@ class FleastServer(object):
if game == "IRL": if game == "IRL":
data = self.client.get_irl_live_streams_v6(lang) data = self.client.get_irl_live_streams_v6(lang)
else: else:
data = self.client.get_live_streams(game.replace(' ', '%20').replace('&', '%26'), lang) data = self.client.get_live_streams(game, lang)
if data is None: if data is None:
return 'Internal Error<br>Tell me more at ' \ return 'Internal Error<br>Tell me more at ' \

View file

@ -2,6 +2,7 @@ import cherrypy
import requests import requests
import threading import threading
import time import time
from urllib.parse import quote
class TwitchClient: class TwitchClient:
@ -148,15 +149,15 @@ class TwitchClient:
https://dev.twitch.tv/docs/v5/reference/search/#search-streams https://dev.twitch.tv/docs/v5/reference/search/#search-streams
""" """
result = {'_total': 0, 'streams': []} result = {'_total': 0, 'streams': []}
game_id = self.get_game_id_v6(name) game_id = self.get_game_id_v6(quote(name))
if game_id[0] is None: if game_id is None:
return result return result
header, base = self.get_base('v6') header, base = self.get_base('v6')
init_q_template = "{}/streams?language={}&first={}&game_id={}" init_q_template = "{}/streams?language={}&first={}&game_id={}"
q_template = "{}/streams?language={}&first={}&after={}&game_id={}" q_template = "{}/streams?language={}&first={}&after={}&game_id={}"
data = self.do_q(init_q_template.format(base, lang, 100, game_id[0]), header) 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.8*100:
result['streams'].extend(data['data']) result['streams'].extend(data['data'])
data = self.do_q(q_template.format(base, lang, 100, data['pagination']['cursor'], game_id[0]), header) data = self.do_q(q_template.format(base, lang, 100, data['pagination']['cursor'], game_id[0]), header)
@ -176,6 +177,7 @@ 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)
result['streams'].extend(data['data'])
while len(data.get('data', [])) > 0.8*100: while len(data.get('data', [])) > 0.8*100:
result['streams'].extend(data['data']) result['streams'].extend(data['data'])
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)