Added skeleton for chat and video.

This commit is contained in:
Vitalii Lebedynskyi
2022-08-08 14:24:06 +03:00
parent a84e8abc14
commit e00fb5bd70
5 changed files with 85 additions and 29 deletions

View File

@@ -1,23 +1,41 @@
import threading
import requests
import logging
TOKEN_URL = "https://id.twitch.tv/oauth2/token?client_id={0}&client_secret={1}&grant_type=client_credentials"
logger = logging.getLogger(__name__)
def synchronized(func):
func.__lock__ = threading.Lock()
def synced_func(*args, **kws):
with func.__lock__:
return func(*args, **kws)
return synced_func
class TwitchAuthenticator:
cached_token = None
def __init__(self, client_id, client_secret):
def __init__(self, client_id, client_secret, username):
self.username = username
self.client_id = client_id
self.client_secret = client_secret
self.token_url = TOKEN_URL.format(self.client_id, self.client_secret)
@synchronized
def get_token(self):
if self.cached_token is None:
self._fetch_token()
return self.cached_token
@synchronized
def refresh_token(self):
# TODO what if both will call refresh ?
self._fetch_token()
return self.cached_token
@@ -26,3 +44,5 @@ class TwitchAuthenticator:
token_response.raise_for_status()
token = token_response.json()
self.cached_token = token["access_token"]
logger.info("Fetched new token %s", self.cached_token)