fleast/twitch.py

137 lines
5 KiB
Python
Raw Normal View History

2018-08-13 18:25:06 +00:00
import cherrypy
2018-03-05 13:57:23 +00:00
import requests
import threading
2018-08-13 18:25:06 +00:00
import time
2018-03-05 13:57:23 +00:00
class TwitchClient:
def __init__(self, token, freq=2):
self.token = token
self.lock = threading.Lock()
2018-08-13 18:25:06 +00:00
self.header_v5 = {'Client-ID': self.token,
'Accept': 'application/vnd.twitchtv.v5+json'}
2018-03-05 13:57:23 +00:00
self.header_v6 = {'Client-ID': self.token}
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
def do_q(self, base, header):
2018-08-13 18:25:06 +00:00
"""
Do query for twitch server
:param base: string with requesting URL
:param header: dictionary of http headers
:return: string with response or None
"""
self.lock.acquire() # Lock for 1 at time query
2018-03-05 13:57:23 +00:00
try:
cherrypy.log('Request: %s' % base)
2018-08-13 18:25:06 +00:00
delta = time.time() - self.last_q # Delta for correct query freq
2018-03-05 13:57:23 +00:00
if delta < self.delay:
2018-08-13 18:25:06 +00:00
time.sleep(delta) # Sleep remaining time
r = requests.get(base, headers=header).json()
2018-03-05 13:57:23 +00:00
self.last_q = time.time()
cherrypy.log('Request: OK')
2018-08-13 18:25:06 +00:00
except requests.exceptions.RequestException as e:
2018-03-05 13:57:23 +00:00
cherrypy.log('Request: FAIL')
2018-08-13 18:25:06 +00:00
cherrypy.log('Error: {}'.format(e))
2018-03-05 13:57:23 +00:00
r = None
finally:
2018-08-13 18:25:06 +00:00
self.lock.release() # Do not forget to release lock
2018-03-05 13:57:23 +00:00
return r
def get_base(self, ver):
2018-08-13 18:25:06 +00:00
"""
Get base which is depended on API version
:param ver: string with API version ('v5' or 'v6')
:return: tuple with list of headers and URL string
:raises: value error on incorrect API version
"""
if ver == 'v5':
return self.header_v5, self.urlbase_v5
elif ver == 'v6':
return self.header_v6, self.urlbase_v6
else:
raise ValueError('Not supported API version')
2018-03-05 13:57:23 +00:00
# - # - #
def raw_query_v6(self, q):
2018-08-13 18:25:06 +00:00
"""
Do a query with API v6
:param q: query string
:return: string with get query result or None
"""
2018-03-05 13:57:23 +00:00
header, base = self.get_base('v6')
return self.do_q(base+q, header)
def raw_query_v5(self, q):
2018-08-13 18:25:06 +00:00
"""
Do a query with API v5
:param q: query string
:return: string with get query result or None
"""
2018-03-05 13:57:23 +00:00
header, base = self.get_base('v5')
return self.do_q(base+q, header)
def get_game_id(self, name):
2018-08-13 18:25:06 +00:00
"""
Getting game id
:param name: string with the name of game
:return: tuple of integer with game id and string with game name or None, None
"""
return self.get_game_id_v5(name)
2018-03-05 13:57:23 +00:00
def get_live_streams(self, name, lang):
2018-08-13 18:25:06 +00:00
"""
Getting list of livestreams
:param name: string with the name of game
:param lang: string with the shortcut of language
:return: list of all streams which are live
"""
return self.get_live_streams_v5(name, lang)
def get_game_id_v5(self, name):
"""
Getting game id with API v5
:param name: string with the name of game
:return: tuple of integer with game id and string with game name or None, None
"""
2018-03-05 13:57:23 +00:00
header, base = self.get_base('v5')
2018-08-13 18:25:06 +00:00
r = self.do_q('{}/search/games?query={}'.format(base, name), header)
if r and r.get('games'):
return r['games'][0]['_id'], r['games'][0]['name']
return None, None
def get_live_streams_v5(self, name, lang):
"""
Getting list of livestreams with API v5
:param name: string with the name of game
:param lang: string with the shortcut of language
:return: list of all streams which are live with this format -
https://dev.twitch.tv/docs/v5/reference/search/#search-streams
"""
header, base = self.get_base('v5')
init_q_template = '{}/streams/?game={}&language={}&limit={}&stream_type=live'
q_template = '{}/streams/?game={}&language={}&limit={}&stream_type=live&offset={}'
data = self.do_q(init_q_template.format(base, name, lang, 100), header)
if data is None:
return []
total = data['_total']
streams = len(data['streams'])
2018-03-05 13:57:23 +00:00
while total > streams:
2018-08-13 18:25:06 +00:00
r = self.do_q(q_template.format(base, name, lang, 100, streams), header)
if r is None:
return []
2018-03-05 13:57:23 +00:00
data['streams'].extend(r['streams'])
2018-08-13 18:25:06 +00:00
total = r['_total']
streams = len(data['streams'])
2018-03-05 13:57:23 +00:00
# Tweak for getting only live sterams
data['streams'] = [x for x in data['streams'] if x['stream_type'] == 'live']
data['_total'] = len(data['streams'])
return data