modules/youtube.py: add api exception handling

This commit is contained in:
David Schultz 2022-07-11 16:36:32 -05:00
parent 6f6b40b87d
commit 9f33fb4381
No known key found for this signature in database
GPG key ID: F6DED672FFFD5E5E

View file

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