temporary commit
This commit is contained in:
parent
6a2e86dc2b
commit
99aac25326
3 changed files with 34 additions and 14 deletions
26
src/main.py
26
src/main.py
|
@ -4,14 +4,28 @@
|
||||||
from twitch import TwitchClient
|
from twitch import TwitchClient
|
||||||
|
|
||||||
import json
|
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():
|
def main():
|
||||||
with open('.token', 'r') as reader:
|
cherrypy.quickstart(FleastServer())
|
||||||
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))
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
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,13 +18,17 @@ class TwitchClient:
|
||||||
|
|
||||||
def do_q(self, base, header):
|
def do_q(self, base, header):
|
||||||
self.lock.acquire()
|
self.lock.acquire()
|
||||||
delta = time.time() - self.last_q
|
try:
|
||||||
if delta < self.delay:
|
delta = time.time() - self.last_q
|
||||||
time.sleep(delta)
|
if delta < self.delay:
|
||||||
r = requests.get(base, headers=header).json()
|
time.sleep(delta)
|
||||||
self.last_q = time.time()
|
r = requests.get(base, headers=header).json()
|
||||||
self.lock.release()
|
self.last_q = time.time()
|
||||||
return r
|
except:
|
||||||
|
r = None
|
||||||
|
finally:
|
||||||
|
self.lock.release()
|
||||||
|
return r
|
||||||
|
|
||||||
def get_base(self, ver):
|
def get_base(self, ver):
|
||||||
if ver == 'v5': return (self.header_v5, self.urlbase_v5)
|
if ver == 'v5': return (self.header_v5, self.urlbase_v5)
|
||||||
|
@ -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):
|
||||||
|
|
Loading…
Reference in a new issue