Add ChannelNotFound exception.

This commit is contained in:
voussoir 2020-07-20 19:03:53 -07:00
parent ff0d01fdda
commit 795aacb044

View file

@ -17,6 +17,9 @@ def int_none(x):
return None return None
return int(x) return int(x)
class ChannelNotFound(Exception):
pass
class VideoNotFound(Exception): class VideoNotFound(Exception):
pass pass
@ -102,14 +105,20 @@ class Youtube:
def get_user_id(self, username): def get_user_id(self, username):
user = self.youtube.channels().list(part='snippet', forUsername=username).execute() user = self.youtube.channels().list(part='snippet', forUsername=username).execute()
if not user.get('items'):
raise ChannelNotFound(f'username: {username}')
return user['items'][0]['id'] return user['items'][0]['id']
def get_user_name(self, uid): def get_user_name(self, uid):
user = self.youtube.channels().list(part='snippet', id=uid).execute() user = self.youtube.channels().list(part='snippet', id=uid).execute()
if not user.get('items'):
raise ChannelNotFound(f'uid: {uid}')
return user['items'][0]['snippet']['title'] return user['items'][0]['snippet']['title']
def get_user_uploads_playlist_id(self, uid): def get_user_uploads_playlist_id(self, uid):
user = self.youtube.channels().list(part='contentDetails', id=uid).execute() user = self.youtube.channels().list(part='contentDetails', id=uid).execute()
if not user.get('items'):
raise ChannelNotFound(f'uid: {uid}')
return user['items'][0]['contentDetails']['relatedPlaylists']['uploads'] return user['items'][0]['contentDetails']['relatedPlaylists']['uploads']
def get_user_videos(self, uid): def get_user_videos(self, uid):