From 16ab08c7d409d6d0e870345e7658c14754b7df6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Forr=C3=B3?= Date: Wed, 9 Sep 2020 13:27:32 +0200 Subject: [PATCH] Deal with empty lists returned by YouTube API --- bot.py | 2 ++ youtube.py | 13 +++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/bot.py b/bot.py index d4f6bbe..be7091b 100644 --- a/bot.py +++ b/bot.py @@ -142,6 +142,8 @@ class Bot(discord.Client): video = self.youtube.get_video(video_id) except YouTubeError as e: return + if not video: + return published = entry.get('published') reminder = entry.get('reminder') if published and not reminder: diff --git a/youtube.py b/youtube.py index 4f04ac2..0a3fce6 100644 --- a/youtube.py +++ b/youtube.py @@ -13,7 +13,7 @@ BASE_URL = 'https://www.youtube.com' class YouTube(object): def __init__(self, api_key, channel_id): self.client = googleapiclient.discovery.build('youtube', 'v3', developerKey=api_key) - self.channel = self.get_channel(channel_id) + self.channel = self.get_channel(channel_id) or {} def get_thumbnail_url(self, thumbnails): for key in ('high', 'medium', 'default'): @@ -48,7 +48,10 @@ class YouTube(object): def get_channel(self, channel_id): r = self.client.channels().list(id=channel_id, maxResults=1, part='id,snippet').execute() - channel = r.get('items', [{}]).pop() + channels = r.get('items', []) + if not channels: + return None + channel = channels.pop() return dict( id=channel.get('id', ''), link='{0}/c/{1}'.format(BASE_URL, channel.get('snippet', {}).get('customUrl', '')), @@ -58,8 +61,10 @@ class YouTube(object): def get_video(self, video_id): r = self.client.videos().list(id=video_id, maxResults=1, part='id,snippet,liveStreamingDetails').execute() - video = r.get('items', [{}]).pop() - return self.process_item(video) + videos = r.get('items', []) + if not videos: + return None + return self.process_item(videos.pop()) def get_playlists(self): token = ''