From 6a2e86dc2b5b6d638c2387956c878d4e70c64749 Mon Sep 17 00:00:00 2001 From: Aleksej Vanin Date: Wed, 11 Oct 2017 12:05:50 +0300 Subject: [PATCH] Added function for getting stream info and threadlock in queries --- src/twitch.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/twitch.py b/src/twitch.py index 7358c91..80c815f 100644 --- a/src/twitch.py +++ b/src/twitch.py @@ -1,30 +1,35 @@ import requests import time +import threading class TwitchClient: def __init__(self, token, freq=2): self.token = token + self.lock = threading.Lock() self.header_v5 = {'Client-ID': self.token, 'Accept': 'application/vnd.twitchtv.v5+json'} 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): - if time.time() - self.last_q < self.delay: - time.sleep(self.delay) + 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 def get_base(self, ver): if ver == 'v5': return (self.header_v5, self.urlbase_v5) elif ver == 'v6': return (self.header_v6, self.urlbase_v6) - else: return (None, None) + else: return None # - # - # @@ -43,3 +48,8 @@ class TwitchClient: if r.get('games', None): return (r['games'][0]['_id'], r['games'][0]['name']) return None + def get_game_list(self, name, lang, lim): + header, base = self.get_base('v5') + r = self.do_q('%s/streams/?game=%s&language=%s&limit=%s' % (base, name, lang, str(lim)), header) + return r +