Merge pull request #347 from bitbot-irc/launchd/yt-api-error

modules/youtube.py: add api exception handling
This commit is contained in:
David Schultz 2022-07-11 21:47:33 -05:00 committed by GitHub
commit 3f40ad9150
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -36,6 +36,7 @@ class Module(ModuleManager.BaseModule):
def video_details(self, video_id): def video_details(self, video_id):
page = self.get_video_page(video_id) page = self.get_video_page(video_id)
try:
if page["items"]: if page["items"]:
item = page["items"][0] item = page["items"][0]
snippet = item["snippet"] snippet = item["snippet"]
@ -76,6 +77,8 @@ class Module(ModuleManager.BaseModule):
video_title, video_duration, video_uploader, video_uploaded_at, video_title, video_duration, video_uploader, video_uploaded_at,
video_views_str, video_opinions), url video_views_str, video_opinions), url
return None return None
except KeyError:
return None
def get_playlist_page(self, playlist_id): def get_playlist_page(self, playlist_id):
self.log.debug("youtube API request: " self.log.debug("youtube API request: "
@ -86,6 +89,7 @@ class Module(ModuleManager.BaseModule):
"key": self.bot.config["google-api-key"]}).json() "key": self.bot.config["google-api-key"]}).json()
def playlist_details(self, playlist_id): def playlist_details(self, playlist_id):
page = self.get_playlist_page(playlist_id) page = self.get_playlist_page(playlist_id)
try:
if page["items"]: if page["items"]:
item = page["items"][0] item = page["items"][0]
snippet = item["snippet"] snippet = item["snippet"]
@ -96,6 +100,8 @@ class Module(ModuleManager.BaseModule):
return "%s - %s (%s %s)" % (snippet["channelTitle"], return "%s - %s (%s %s)" % (snippet["channelTitle"],
snippet["title"], count, "video" if count == 1 else "videos" snippet["title"], count, "video" if count == 1 else "videos"
), URL_PLAYLIST % playlist_id ), URL_PLAYLIST % playlist_id
except KeyError:
return None
def _from_url(self, url): def _from_url(self, url):
parsed = urllib.parse.urlparse(url) parsed = urllib.parse.urlparse(url)
@ -148,6 +154,8 @@ class Module(ModuleManager.BaseModule):
from_url = not url == None from_url = not url == None
try:
if not url: if not url:
safe_setting = event["target"].get_setting("youtube-safesearch", True) safe_setting = event["target"].get_setting("youtube-safesearch", True)
safe = "moderate" if safe_setting else "none" safe = "moderate" if safe_setting else "none"
@ -165,6 +173,8 @@ class Module(ModuleManager.BaseModule):
raise utils.EventError("No videos found") raise utils.EventError("No videos found")
else: else:
raise utils.EventResultsError() raise utils.EventResultsError()
except KeyError:
raise utils.EventError("API error")
if url: if url:
out = self._from_url(url) out = self._from_url(url)