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): def __init__(self):
try: try:
with open('.token', 'r') as reader: with open('.token', 'r') as reader:
twitch_token = reader.read().strip() self.twitch_token = reader.read().strip()
except: except:
print("Cannot read token for twitch app, abort.") print("Cannot read token for twitch app, abort.")
exit(1) exit(1)
client = TwitchClient(twitch_token, freq = 1) self.client = TwitchClient(self.twitch_token, freq = 1)
@cherrypy.expose @cherrypy.expose
def index(self): def index(self):
@ -22,7 +22,24 @@ class FleastServer(object):
@cherrypy.expose @cherrypy.expose
def fleast(self, game, lang): 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(): def main():
cherrypy.quickstart(FleastServer()) cherrypy.quickstart(FleastServer())

View file

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