Deal with empty lists returned by YouTube API

master
Nikola Forró 4 years ago
parent 41cb342e2a
commit 16ab08c7d4

@ -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:

@ -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 = ''

Loading…
Cancel
Save