diff --git a/src/main.py b/src/main.py
index cbf17b1..0d27c8c 100755
--- a/src/main.py
+++ b/src/main.py
@@ -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 = '
Test'
+ streams = sorted(data['streams'], key=lambda k: k['viewers'])
+ count = 0
+ for s in streams:
+ result += '
{2} :: {3}
{4}
'.format( \
+ s['channel']['url'], s['preview']['medium'], s['channel']['status'], \
+ s['channel']['display_name'], s['viewers'])
+ return result
+
def main():
cherrypy.quickstart(FleastServer())
diff --git a/src/twitch.py b/src/twitch.py
index 8c587c6..78b75a4 100644
--- a/src/twitch.py
+++ b/src/twitch.py
@@ -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
+