temporary commit

This commit is contained in:
Aleksej Vanin 2017-10-11 16:34:34 +03:00
parent 6a2e86dc2b
commit 99aac25326
3 changed files with 34 additions and 14 deletions

View file

@ -4,14 +4,28 @@
from twitch import TwitchClient
import json
import cherrypy
class FleastServer(object):
def __init__(self):
try:
with open('.token', 'r') as reader:
twitch_token = reader.read().strip()
except:
print("Cannot read token for twitch app, abort.")
exit(1)
client = TwitchClient(twitch_token, freq = 1)
@cherrypy.expose
def index(self):
return 'Hello World'
@cherrypy.expose
def fleast(self, game, lang):
return game
def main():
with open('.token', 'r') as reader:
twitch_token = reader.read().strip()
client = TwitchClient(twitch_token)
r = client.raw_query_v5('streams/?game=IRL&language=ru&limit=2')
print(json.dumps(r, indent=4))
cherrypy.quickstart(FleastServer())
if __name__ == '__main__':

2
src/server.conf Normal file
View file

@ -0,0 +1,2 @@
[global]
server.socket_port: 8080

View file

@ -18,13 +18,17 @@ class TwitchClient:
def do_q(self, base, header):
self.lock.acquire()
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()
self.lock.release()
return r
try:
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:
r = None
finally:
self.lock.release()
return r
def get_base(self, ver):
if ver == 'v5': return (self.header_v5, self.urlbase_v5)
@ -45,7 +49,7 @@ class TwitchClient:
def get_game_id(self, name):
header, base = self.get_base('v5')
r = self.do_q('%s/search/games?query=%s' % (base, name), header)
if r.get('games', None): 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
def get_game_list(self, name, lang, lim):