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) video = self.youtube.get_video(video_id)
except YouTubeError as e: except YouTubeError as e:
return return
if not video:
return
published = entry.get('published') published = entry.get('published')
reminder = entry.get('reminder') reminder = entry.get('reminder')
if published and not reminder: if published and not reminder:

@ -13,7 +13,7 @@ BASE_URL = 'https://www.youtube.com'
class YouTube(object): class YouTube(object):
def __init__(self, api_key, channel_id): def __init__(self, api_key, channel_id):
self.client = googleapiclient.discovery.build('youtube', 'v3', developerKey=api_key) 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): def get_thumbnail_url(self, thumbnails):
for key in ('high', 'medium', 'default'): for key in ('high', 'medium', 'default'):
@ -48,7 +48,10 @@ class YouTube(object):
def get_channel(self, channel_id): def get_channel(self, channel_id):
r = self.client.channels().list(id=channel_id, maxResults=1, part='id,snippet').execute() 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( return dict(
id=channel.get('id', ''), id=channel.get('id', ''),
link='{0}/c/{1}'.format(BASE_URL, channel.get('snippet', {}).get('customUrl', '')), 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): def get_video(self, video_id):
r = self.client.videos().list(id=video_id, maxResults=1, part='id,snippet,liveStreamingDetails').execute() r = self.client.videos().list(id=video_id, maxResults=1, part='id,snippet,liveStreamingDetails').execute()
video = r.get('items', [{}]).pop() videos = r.get('items', [])
return self.process_item(video) if not videos:
return None
return self.process_item(videos.pop())
def get_playlists(self): def get_playlists(self):
token = '' token = ''

Loading…
Cancel
Save