First working version

This commit is contained in:
2017-10-11 23:58:38 +03:00
parent 99aac25326
commit f6f3264e69
2 changed files with 40 additions and 8 deletions

View file

@ -10,11 +10,11 @@ class FleastServer(object):
def __init__(self):
try:
with open('.token', 'r') as reader:
twitch_token = reader.read().strip()
self.twitch_token = reader.read().strip()
except:
print("Cannot read token for twitch app, abort.")
exit(1)
client = TwitchClient(twitch_token, freq = 1)
self.client = TwitchClient(self.twitch_token, freq = 1)
@cherrypy.expose
def index(self):
@ -22,7 +22,24 @@ class FleastServer(object):
@cherrypy.expose
def fleast(self, game, lang):
return game
cherrypy.log('Getting game:"%s" language:%s' % (game, lang))
data = self.client.get_streams(game, lang)
if data is None:
return 'Error!' #Do better
if data['_total'] == 0:
return 'No streams found' #Do better
cherrypy.log('Found %d streams' % data['_total'])
result = '<html><head><title>Test</title></head><body>'
streams = sorted(data['streams'], key=lambda k: k['viewers'])
count = 0
for s in streams:
result += '<a href="{0}"><img src="{1}"></a><br>{2} :: {3}<br>{4}<hr>'.format( \
s['channel']['url'], s['preview']['medium'], s['channel']['status'], \
s['channel']['display_name'], s['viewers'])
return result
def main():
cherrypy.quickstart(FleastServer())

View file

@ -2,6 +2,8 @@ import requests
import time
import threading
import cherrypy
class TwitchClient:
def __init__(self, token, freq=2):
self.token = token
@ -9,8 +11,8 @@ class TwitchClient:
self.header_v5 = {'Client-ID': self.token, 'Accept': 'application/vnd.twitchtv.v5+json'}
self.header_v6 = {'Client-ID': self.token}
self.urlbase_v5 = 'https://api.twitch.tv/kraken/'
self.urlbase_v6 = 'https://api.twitch.tv/helix/'
self.urlbase_v5 = 'https://api.twitch.tv/kraken'
self.urlbase_v6 = 'https://api.twitch.tv/helix'
self.last_q = time.time()
self.delay = 1/freq
@ -19,14 +21,17 @@ class TwitchClient:
def do_q(self, base, header):
self.lock.acquire()
try:
cherrypy.log('Request: %s' % base)
delta = time.time() - self.last_q
if delta < self.delay:
time.sleep(delta)
r = requests.get(base, headers=header).json()
self.last_q = time.time()
except:
cherrypy.log('Request: FAIL')
r = None
finally:
cherrypy.log('Request: OK')
self.lock.release()
return r
@ -52,8 +57,18 @@ class TwitchClient:
if r and r.get('games'): return (r['games'][0]['_id'], r['games'][0]['name'])
return None
def get_game_list(self, name, lang, lim):
def get_streams(self, name, lang):
header, base = self.get_base('v5')
r = self.do_q('%s/streams/?game=%s&language=%s&limit=%s' % (base, name, lang, str(lim)), header)
return r
data = self.do_q('%s/streams/?game=%s&language=%s&limit=%s&stream_type=live' % (base, name, lang, 100), header)
if data is None: return None
total = data['_total']; streams = len(data['streams'])
while total > streams:
r = self.do_q('%s/streams/?game=%s&language=%s&limit=%s&stream_type=live&offset=%s' % (base, name, lang, 100, streams), header)
if r is None: return None
data['streams'].extend(r['streams'])
total = r['_total']; streams = len(data['streams'])
return data