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

2
src/server.conf Normal file
View file

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

View file

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