Catch Twitch API being down where needed

This commit is contained in:
Deko
2023-03-04 11:12:24 +01:00
parent 13d64dac3d
commit 98d978dea0

View File

@@ -5,6 +5,7 @@ from dataclasses import dataclass
import requests import requests
from loguru import logger from loguru import logger
from requests import HTTPError from requests import HTTPError
from urllib3.exceptions import NewConnectionError
@dataclass @dataclass
@@ -105,14 +106,18 @@ class TwitchClient:
return response.json()["data"][0]["profile_image_url"] return response.json()["data"][0]["profile_image_url"]
def get_stream(self, is_retry: bool = False) -> StreamInformation | None: def get_stream(self, is_retry: bool = False) -> StreamInformation | None:
response = requests.get( try:
url="https://api.twitch.tv/helix/streams", response = requests.get(
headers={ url="https://api.twitch.tv/helix/streams",
"Client-Id": self._client_id, headers={
"Authorization": f"Bearer {self._access_token}", "Client-Id": self._client_id,
}, "Authorization": f"Bearer {self._access_token}",
params={"user_login": self.streamer}, },
) params={"user_login": self.streamer},
)
except NewConnectionError:
logger.warning("Twitch API is not reachable at the moment.")
return None
if response.status_code == 401: if response.status_code == 401:
logger.info("Getting streams returned an auth issue.") logger.info("Getting streams returned an auth issue.")
@@ -147,14 +152,18 @@ class TwitchClient:
) )
def get_vod(self, user_id: str, is_retry: bool = False) -> str | None: def get_vod(self, user_id: str, is_retry: bool = False) -> str | None:
response = requests.get( try:
url="https://api.twitch.tv/helix/videos", response = requests.get(
headers={ url="https://api.twitch.tv/helix/videos",
"Client-Id": self._client_id, headers={
"Authorization": f"Bearer {self._access_token}", "Client-Id": self._client_id,
}, "Authorization": f"Bearer {self._access_token}",
params={"user_id": user_id}, },
) params={"user_id": user_id},
)
except NewConnectionError:
logger.warning("Twitch API is not reachable at the moment.")
return None
if response.status_code == 401: if response.status_code == 401:
logger.info("Getting vod returned an auth issue.") logger.info("Getting vod returned an auth issue.")