Skip to content

Commit

Permalink
fix: twitch and yt oauth didn't show validate failed message when ent…
Browse files Browse the repository at this point in the history
…er the wrong token
  • Loading branch information
Mantouisyummy committed Jul 16, 2024
1 parent 1c6ca4d commit bcdfb6d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
10 changes: 10 additions & 0 deletions tystream/async_api/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ async def is_token_expired(token_info):
now = int(time.time())
return now - token_info["expires_in"] < 60

async def validation_token(self):
async with aiohttp.ClientSession() as session:
async with session.get(
f"https://id.twitch.tv/oauth2/validate"
) as response:
if response.ok:
return True
else:
raise OauthException("Twitch API Validation Failed.")

async def get_access_token(self) -> str:
token_info = self.cache_handler.get_cached_token()
if token_info and not await self.is_token_expired(token_info):
Expand Down
6 changes: 4 additions & 2 deletions tystream/async_api/twitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __init__(self, client_id: str, client_secret: str) -> None:

async def _renew_token(self):
oauth = TwitchOauth(self.client_id, self.client_secret)
await oauth.validation_token()
return await oauth.get_access_token()

async def _get_headers(self):
Expand Down Expand Up @@ -52,10 +53,11 @@ async def get_user(self, streamer_name: str) -> TwitchUserData:
headers=headers,
timeout=10
) as user:
user_data = await user.json()['data']
data = await user.json()
user_data = data["data"][0]
return TwitchUserData(**user_data)

async def check_stream_live(self, streamer_name: str) -> TwitchStreamData:
async def check_stream_live(self, streamer_name: str) -> TwitchStreamData | bool:
"""
Check if stream is live.
Expand Down
6 changes: 3 additions & 3 deletions tystream/async_api/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ def __init__(self, api_key: str) -> None:

self.oauth = YoutubeOauth(api_key)

self.oauth.validation_token()

self.logger = logging.getLogger(__name__)
self.BASE_URL = "https://www.googleapis.com/youtube/v3"

Expand Down Expand Up @@ -90,11 +88,13 @@ async def check_stream_live(self, username: str) -> YoutubeStreamData:
An instance of the YoutubeStreamData class containing information about the live stream.
If the stream is not live, returned False.
"""
await self.oauth.validation_token()

channelId = await self._get_channel_id(username)
LiveId = await self._get_live_id(channelId)

if LiveId:
url = f'{self.BASE_URL}/youtube/v3/videos?part=id%2C+snippet&id={LiveId}&key={self.oauth.api_key}'
url = f"{self.BASE_URL}/videos?part=id%2C+snippet&id={LiveId}&key={self.oauth.api_key}"

async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
Expand Down
7 changes: 7 additions & 0 deletions tystream/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ def is_token_expired(token_info):
now = int(time.time())
return now - token_info["expires_in"] < 60

def validation_token(self):
response = self.session.get("https://id.twitch.tv/oauth2/validate")
if response.ok:
return True
else:
raise OauthException("Twitch API Validation Failed.")

def get_access_token(self) -> str:
token_info = self.cache_handler.get_cached_token()
if token_info and not self.is_token_expired(token_info):
Expand Down
1 change: 1 addition & 0 deletions tystream/twitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init__(

def _renew_token(self):
oauth = TwitchOauth(self.client_id, self.client_secret, self.session)
oauth.validation_token()
return oauth.get_access_token()

def _get_headers(self):
Expand Down

0 comments on commit bcdfb6d

Please sign in to comment.