2018-03-05 13:57:23 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import cherrypy
|
|
|
|
from cherrypy.process.plugins import Daemonizer
|
2018-08-13 19:10:52 +00:00
|
|
|
from twitch import TwitchClient
|
2018-03-05 13:57:23 +00:00
|
|
|
|
2018-12-09 08:25:34 +00:00
|
|
|
ver = '1.7'
|
2018-03-05 13:57:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FleastServer(object):
|
|
|
|
def __init__(self):
|
|
|
|
try:
|
|
|
|
with open('.token', 'r') as reader:
|
|
|
|
self.twitch_token = reader.read().strip()
|
|
|
|
with open('./web/fl.html', 'r') as reader:
|
|
|
|
self.index_page = reader.read()
|
|
|
|
with open('./web/fl_template_main.html', 'r') as reader:
|
|
|
|
self.templ_main = reader.read()
|
|
|
|
with open('./web/fl_template_stream.html', 'r') as reader:
|
|
|
|
self.templ_stream = reader.read()
|
|
|
|
with open('./web/fl_template_lang.html', 'r') as reader:
|
|
|
|
self.templ_lang = reader.read().splitlines()
|
2018-08-13 19:10:52 +00:00
|
|
|
self.client = TwitchClient(self.twitch_token, freq=1)
|
2018-03-05 13:57:23 +00:00
|
|
|
except:
|
|
|
|
print("Cannot read token for twitch app or templates, abort.")
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
def set_templ_lang(self, lang):
|
2018-08-13 19:10:52 +00:00
|
|
|
"""
|
|
|
|
Set autoselected language in html
|
|
|
|
:param lang: string with the shortcut of language
|
|
|
|
:return: string with html list of languages and one is selected
|
|
|
|
"""
|
2018-03-05 13:57:23 +00:00
|
|
|
templ = ''
|
|
|
|
end = False
|
|
|
|
for l in self.templ_lang:
|
|
|
|
if not end and 'option value="{}"'.format(lang) in l:
|
|
|
|
templ += l.format('selected') + '\n'
|
|
|
|
end = True
|
|
|
|
continue
|
|
|
|
templ += l.format(' ') + '\n'
|
|
|
|
return templ.rstrip()
|
|
|
|
|
|
|
|
def to_html(self, text):
|
2018-08-13 19:10:52 +00:00
|
|
|
"""
|
|
|
|
Relace all non xml symbols with aliases
|
|
|
|
:param text: string with html source code
|
|
|
|
:return: filtered string with html source code
|
|
|
|
"""
|
2018-12-09 08:25:34 +00:00
|
|
|
repl = {'"': '"', '<': '<', '>': '>'}
|
|
|
|
text.replace('&', '&')
|
2018-03-05 13:57:23 +00:00
|
|
|
for i in repl:
|
|
|
|
text = text.replace(i, repl[i])
|
|
|
|
return text
|
|
|
|
|
|
|
|
@cherrypy.expose
|
|
|
|
def index(self, game=None, lang=None):
|
|
|
|
return self.fleast(game, lang)
|
|
|
|
|
|
|
|
@cherrypy.expose
|
|
|
|
def fleast(self, game=None, lang=None):
|
|
|
|
if game is None or game == '':
|
2018-08-13 19:10:52 +00:00
|
|
|
return self.index_page.format(_version_=ver,
|
|
|
|
_opt_langs_=self.set_templ_lang('ru'))
|
2018-03-05 13:57:23 +00:00
|
|
|
game = game.rstrip()
|
2018-08-13 19:10:52 +00:00
|
|
|
cherrypy.log('Getting game:"{}" language:{}'.format(game, lang))
|
2018-11-25 12:35:50 +00:00
|
|
|
if game == "IRL":
|
|
|
|
data = self.client.get_irl_live_streams_v6(lang)
|
|
|
|
else:
|
|
|
|
data = self.client.get_live_streams(game.replace(' ', '%20').replace('&', '%26'), lang)
|
2018-03-05 13:57:23 +00:00
|
|
|
|
|
|
|
if data is None:
|
2018-08-13 19:10:52 +00:00
|
|
|
return 'Internal Error<br>Tell me more at ' \
|
|
|
|
'<a href="https://twitter.com/alexvanin">https://twitter.com/alexvanin</a>'
|
|
|
|
|
|
|
|
if data['_total'] == 0:
|
|
|
|
return self.templ_main.format(_stream_num_=data['_total'],
|
|
|
|
_game_name_=game,
|
|
|
|
_opt_langs_=self.set_templ_lang(lang),
|
|
|
|
_stream_list_='',
|
|
|
|
_version_=ver)
|
2018-03-05 13:57:23 +00:00
|
|
|
|
|
|
|
cherrypy.log('Found %d streams' % data['_total'])
|
|
|
|
|
2018-11-25 12:35:50 +00:00
|
|
|
if game == "IRL":
|
2018-12-01 17:37:05 +00:00
|
|
|
uniq_streams = []
|
2018-11-25 12:35:50 +00:00
|
|
|
streams = sorted(data['streams'], key=lambda k: k['viewer_count'])
|
|
|
|
result_str = ''
|
|
|
|
irl_url = 'https://twitch.tv/{}'
|
|
|
|
for s in streams:
|
2018-12-01 17:37:05 +00:00
|
|
|
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'
|
2018-11-25 12:35:50 +00:00
|
|
|
else:
|
2018-12-01 17:37:05 +00:00
|
|
|
|
2018-11-25 12:35:50 +00:00
|
|
|
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'
|
2018-08-13 19:10:52 +00:00
|
|
|
|
|
|
|
return self.templ_main.format(_stream_num_=data['_total'],
|
|
|
|
_game_name_=game,
|
|
|
|
_opt_langs_=self.set_templ_lang(lang),
|
|
|
|
_stream_list_=result_str,
|
|
|
|
_version_=ver)
|
2018-03-05 13:57:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
server = FleastServer()
|
2018-12-01 17:38:07 +00:00
|
|
|
d = Daemonizer(cherrypy.engine).subscribe()
|
2018-03-05 13:57:23 +00:00
|
|
|
cherrypy.quickstart(server, '/', './server.conf')
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|