temporary commit
This commit is contained in:
parent
6a2e86dc2b
commit
99aac25326
3 changed files with 34 additions and 14 deletions
22
src/main.py
22
src/main.py
|
@ -4,14 +4,28 @@
|
|||
from twitch import TwitchClient
|
||||
|
||||
import json
|
||||
import cherrypy
|
||||
|
||||
def main():
|
||||
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)
|
||||
|
||||
client = TwitchClient(twitch_token)
|
||||
r = client.raw_query_v5('streams/?game=IRL&language=ru&limit=2')
|
||||
print(json.dumps(r, indent=4))
|
||||
@cherrypy.expose
|
||||
def index(self):
|
||||
return 'Hello World'
|
||||
|
||||
@cherrypy.expose
|
||||
def fleast(self, game, lang):
|
||||
return game
|
||||
|
||||
def main():
|
||||
cherrypy.quickstart(FleastServer())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
2
src/server.conf
Normal file
2
src/server.conf
Normal file
|
@ -0,0 +1,2 @@
|
|||
[global]
|
||||
server.socket_port: 8080
|
|
@ -18,11 +18,15 @@ class TwitchClient:
|
|||
|
||||
def do_q(self, base, header):
|
||||
self.lock.acquire()
|
||||
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
|
||||
|
||||
|
@ -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):
|
||||
|
|
Loading…
Reference in a new issue