fleast: Use helix api to get live streams
This commit is contained in:
parent
f2329d79e0
commit
ee823ab215
2 changed files with 50 additions and 31 deletions
36
main.py
36
main.py
|
@ -5,7 +5,7 @@ import cherrypy
|
|||
from cherrypy.process.plugins import Daemonizer
|
||||
from twitch import TwitchClient
|
||||
|
||||
ver = '1.8'
|
||||
ver = '1.9-pre'
|
||||
|
||||
|
||||
class FleastServer(object):
|
||||
|
@ -83,28 +83,18 @@ class FleastServer(object):
|
|||
|
||||
cherrypy.log('Found %d streams' % data['_total'])
|
||||
|
||||
if game == "IRL":
|
||||
uniq_streams = []
|
||||
streams = sorted(data['streams'], key=lambda k: k['viewer_count'])
|
||||
result_str = ''
|
||||
irl_url = 'https://twitch.tv/{}'
|
||||
for s in streams:
|
||||
if s['user_name'] not in uniq_streams:
|
||||
uniq_streams.append(s['user_name'])
|
||||
result_str += self.templ_stream.format(irl_url.format(s['user_name']),
|
||||
s['thumbnail_url'].format(width=320, height=180),
|
||||
self.to_html(s['title']),
|
||||
s['user_name'],
|
||||
s['viewer_count']) + '\n'
|
||||
else:
|
||||
streams = sorted(data['streams'], key=lambda k: k['viewers'])
|
||||
result_str = ''
|
||||
for s in streams:
|
||||
result_str += self.templ_stream.format(s['channel']['url'],
|
||||
s['preview']['medium'],
|
||||
self.to_html(s['channel']['status']),
|
||||
s['channel']['display_name'],
|
||||
s['viewers']) + '\n'
|
||||
uniq_streams = []
|
||||
streams = sorted(data['streams'], key=lambda k: k['viewer_count'])
|
||||
result_str = ''
|
||||
irl_url = 'https://twitch.tv/{}'
|
||||
for s in streams:
|
||||
if s['user_name'] not in uniq_streams:
|
||||
uniq_streams.append(s['user_name'])
|
||||
result_str += self.templ_stream.format(irl_url.format(s['user_name']),
|
||||
s['thumbnail_url'].format(width=320, height=180),
|
||||
self.to_html(s['title']),
|
||||
s['user_name'],
|
||||
s['viewer_count']) + '\n'
|
||||
|
||||
return self.templ_main.format(_stream_num_=len(streams),
|
||||
_game_name_=game,
|
||||
|
|
45
twitch.py
45
twitch.py
|
@ -91,7 +91,7 @@ class TwitchClient:
|
|||
:param lang: string with the shortcut of language
|
||||
:return: list of all streams which are live
|
||||
"""
|
||||
return self.get_live_streams_v5(name, lang)
|
||||
return self.get_live_streams_v6(name, lang)
|
||||
|
||||
def get_game_id_v5(self, name):
|
||||
"""
|
||||
|
@ -105,14 +105,19 @@ class TwitchClient:
|
|||
return r['games'][0]['_id'], r['games'][0]['name']
|
||||
return None, None
|
||||
|
||||
def get_game_id_v6(self, name):
|
||||
"""
|
||||
Getting game id with API v6
|
||||
:param name: string with the name of game
|
||||
:return: tuple of integer with game id and string with game name or (None,None)
|
||||
"""
|
||||
|
||||
header, base = self.get_base('v6')
|
||||
r = self.do_q('{}/games?name={}'.format(base, name), header)
|
||||
if r and r.get('data'):
|
||||
return r['data'][0]['id'], r['data'][0]['name']
|
||||
|
||||
def get_live_streams_v5(self, name, lang):
|
||||
"""
|
||||
Getting list of livestreams with API v5
|
||||
:param name: string with the name of game
|
||||
:param lang: string with the shortcut of language
|
||||
:return: list of all streams which are live with this format -
|
||||
https://dev.twitch.tv/docs/v5/reference/search/#search-streams
|
||||
"""
|
||||
header, base = self.get_base('v5')
|
||||
init_q_template = '{}/search/streams?query={}&limit={}'
|
||||
q_template = '{}/search/streams?query={}&limit={}&offset={}'
|
||||
|
@ -134,6 +139,30 @@ class TwitchClient:
|
|||
data['_total'] = len(data['streams'])
|
||||
return data
|
||||
|
||||
def get_live_streams_v6(self, name, lang):
|
||||
"""
|
||||
Getting list of livestreams with API v5
|
||||
:param name: string with the name of game
|
||||
:param lang: string with the shortcut of language
|
||||
:return: list of all streams which are live with this format -
|
||||
https://dev.twitch.tv/docs/v5/reference/search/#search-streams
|
||||
"""
|
||||
result = {'_total': 0, 'streams': []}
|
||||
game_id = self.get_game_id_v6(name)
|
||||
if game_id[0] is None:
|
||||
return result
|
||||
|
||||
header, base = self.get_base('v6')
|
||||
init_q_template = "{}/streams?language={}&first={}&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)
|
||||
while len(data.get('data', [])) > 0.8*100:
|
||||
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
|
||||
|
||||
def get_irl_live_streams_v6(self, lang):
|
||||
header, base = self.get_base('v6')
|
||||
init_q_template = "{}/streams?language={}&first={}{}"
|
||||
|
|
Loading…
Reference in a new issue