Catch Twitch API being down where needed
This commit is contained in:
@@ -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,6 +106,7 @@ 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:
|
||||||
|
try:
|
||||||
response = requests.get(
|
response = requests.get(
|
||||||
url="https://api.twitch.tv/helix/streams",
|
url="https://api.twitch.tv/helix/streams",
|
||||||
headers={
|
headers={
|
||||||
@@ -113,6 +115,9 @@ class TwitchClient:
|
|||||||
},
|
},
|
||||||
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,6 +152,7 @@ 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:
|
||||||
|
try:
|
||||||
response = requests.get(
|
response = requests.get(
|
||||||
url="https://api.twitch.tv/helix/videos",
|
url="https://api.twitch.tv/helix/videos",
|
||||||
headers={
|
headers={
|
||||||
@@ -155,6 +161,9 @@ class TwitchClient:
|
|||||||
},
|
},
|
||||||
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.")
|
||||||
|
|||||||
Reference in New Issue
Block a user