Add method get_playlist_videos.

This commit is contained in:
voussoir 2020-03-17 20:53:10 -07:00
parent 08ae8a9394
commit 1a7662b6bd

View file

@ -61,19 +61,14 @@ class Youtube:
user = self.youtube.channels().list(part='snippet', id=uid).execute() user = self.youtube.channels().list(part='snippet', id=uid).execute()
return user['items'][0]['snippet']['title'] return user['items'][0]['snippet']['title']
def get_user_videos(self, username=None, uid=None): def _iter_playlist(self, playlist_id):
if username:
user = self.youtube.channels().list(part='contentDetails', forUsername=username).execute()
else:
user = self.youtube.channels().list(part='contentDetails', id=uid).execute()
upload_playlist = user['items'][0]['contentDetails']['relatedPlaylists']['uploads']
page_token = None page_token = None
while True: while True:
response = self.youtube.playlistItems().list( response = self.youtube.playlistItems().list(
maxResults=50, maxResults=50,
pageToken=page_token, pageToken=page_token,
part='contentDetails', part='contentDetails',
playlistId=upload_playlist, playlistId=playlist_id,
).execute() ).execute()
page_token = response.get('nextPageToken', None) page_token = response.get('nextPageToken', None)
video_ids = [item['contentDetails']['videoId'] for item in response['items']] video_ids = [item['contentDetails']['videoId'] for item in response['items']]
@ -86,6 +81,17 @@ class Youtube:
if page_token is None: if page_token is None:
break break
def get_playlist_videos(self, playlist_id):
yield from self._iter_playlist(playlist_id)
def get_user_videos(self, username=None, uid=None):
if username:
user = self.youtube.channels().list(part='contentDetails', forUsername=username).execute()
else:
user = self.youtube.channels().list(part='contentDetails', id=uid).execute()
upload_playlist_id = user['items'][0]['contentDetails']['relatedPlaylists']['uploads']
yield from self._iter_playlist(upload_playlist_id)
def get_related_videos(self, video_id, count=50): def get_related_videos(self, video_id, count=50):
if isinstance(video_id, Video): if isinstance(video_id, Video):
video_id = video_id.id video_id = video_id.id