mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 11:18:54 -05:00
Fix error handling for API calls to videos endpoint in YouTube alerts (#4745)
This commit is contained in:
parent
5b670c074f
commit
dc68bc5d37
@ -127,7 +127,24 @@ class YoutubeStream(Stream):
|
|||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.get(YOUTUBE_VIDEOS_ENDPOINT, params=params) as r:
|
async with session.get(YOUTUBE_VIDEOS_ENDPOINT, params=params) as r:
|
||||||
data = await r.json()
|
data = await r.json()
|
||||||
stream_data = data.get("items", [{}])[0].get("liveStreamingDetails", {})
|
try:
|
||||||
|
self._check_api_errors(data)
|
||||||
|
except InvalidYoutubeCredentials:
|
||||||
|
log.error("The YouTube API key is either invalid or has not been set.")
|
||||||
|
break
|
||||||
|
except YoutubeQuotaExceeded:
|
||||||
|
log.error("YouTube quota has been exceeded.")
|
||||||
|
break
|
||||||
|
except APIError as e:
|
||||||
|
log.error(
|
||||||
|
"Something went wrong whilst trying to"
|
||||||
|
" contact the stream service's API.\n"
|
||||||
|
"Raw response data:\n%r",
|
||||||
|
e,
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
video_data = data.get("items", [{}])[0]
|
||||||
|
stream_data = video_data.get("liveStreamingDetails", {})
|
||||||
log.debug(f"stream_data for {video_id}: {stream_data}")
|
log.debug(f"stream_data for {video_id}: {stream_data}")
|
||||||
if (
|
if (
|
||||||
stream_data
|
stream_data
|
||||||
@ -143,9 +160,9 @@ class YoutubeStream(Stream):
|
|||||||
elif actual_start_time is None:
|
elif actual_start_time is None:
|
||||||
continue
|
continue
|
||||||
if video_id not in self.livestreams:
|
if video_id not in self.livestreams:
|
||||||
self.livestreams.append(data["items"][0]["id"])
|
self.livestreams.append(video_id)
|
||||||
else:
|
else:
|
||||||
self.not_livestreams.append(data["items"][0]["id"])
|
self.not_livestreams.append(video_id)
|
||||||
if video_id in self.livestreams:
|
if video_id in self.livestreams:
|
||||||
self.livestreams.remove(video_id)
|
self.livestreams.remove(video_id)
|
||||||
log.debug(f"livestreams for {self.name}: {self.livestreams}")
|
log.debug(f"livestreams for {self.name}: {self.livestreams}")
|
||||||
@ -225,6 +242,20 @@ class YoutubeStream(Stream):
|
|||||||
async with session.get(YOUTUBE_CHANNELS_ENDPOINT, params=params) as r:
|
async with session.get(YOUTUBE_CHANNELS_ENDPOINT, params=params) as r:
|
||||||
data = await r.json()
|
data = await r.json()
|
||||||
|
|
||||||
|
self._check_api_errors(data)
|
||||||
|
if "items" in data and len(data["items"]) == 0:
|
||||||
|
raise StreamNotFound()
|
||||||
|
elif "items" in data:
|
||||||
|
return data["items"][0][resource]
|
||||||
|
elif (
|
||||||
|
"pageInfo" in data
|
||||||
|
and "totalResults" in data["pageInfo"]
|
||||||
|
and data["pageInfo"]["totalResults"] < 1
|
||||||
|
):
|
||||||
|
raise StreamNotFound()
|
||||||
|
raise APIError(data)
|
||||||
|
|
||||||
|
def _check_api_errors(self, data: dict):
|
||||||
if "error" in data:
|
if "error" in data:
|
||||||
error_code = data["error"]["code"]
|
error_code = data["error"]["code"]
|
||||||
if error_code == 400 and data["error"]["errors"][0]["reason"] == "keyInvalid":
|
if error_code == 400 and data["error"]["errors"][0]["reason"] == "keyInvalid":
|
||||||
@ -235,16 +266,6 @@ class YoutubeStream(Stream):
|
|||||||
"rateLimitExceeded",
|
"rateLimitExceeded",
|
||||||
):
|
):
|
||||||
raise YoutubeQuotaExceeded()
|
raise YoutubeQuotaExceeded()
|
||||||
elif "items" in data and len(data["items"]) == 0:
|
|
||||||
raise StreamNotFound()
|
|
||||||
elif "items" in data:
|
|
||||||
return data["items"][0][resource]
|
|
||||||
elif (
|
|
||||||
"pageInfo" in data
|
|
||||||
and "totalResults" in data["pageInfo"]
|
|
||||||
and data["pageInfo"]["totalResults"] < 1
|
|
||||||
):
|
|
||||||
raise StreamNotFound()
|
|
||||||
raise APIError(data)
|
raise APIError(data)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user