Move playlist paginator out to get a clean looking generator.
This commit is contained in:
parent
294ccb77d9
commit
81bd7b9ca2
1 changed files with 12 additions and 9 deletions
|
@ -56,7 +56,7 @@ class Youtube:
|
|||
)
|
||||
self.log = vlogging.getLogger(__name__)
|
||||
|
||||
def get_playlist_videos(self, playlist_id):
|
||||
def _playlist_paginator(self, playlist_id):
|
||||
page_token = None
|
||||
while True:
|
||||
response = self.youtube.playlistItems().list(
|
||||
|
@ -65,17 +65,20 @@ class Youtube:
|
|||
part='contentDetails',
|
||||
playlistId=playlist_id,
|
||||
).execute()
|
||||
page_token = response.get('nextPageToken', None)
|
||||
|
||||
video_ids = [item['contentDetails']['videoId'] for item in response['items']]
|
||||
yield from response['items']
|
||||
|
||||
page_token = response.get('nextPageToken', None)
|
||||
if page_token is None:
|
||||
break
|
||||
|
||||
def get_playlist_videos(self, playlist_id):
|
||||
paginator = self._playlist_paginator(playlist_id)
|
||||
video_ids = (item['contentDetails']['videoId'] for item in paginator)
|
||||
videos = self.get_videos(video_ids)
|
||||
videos.sort(key=lambda x: x.published, reverse=True)
|
||||
|
||||
for video in videos:
|
||||
yield video
|
||||
|
||||
if page_token is None:
|
||||
break
|
||||
yield from videos
|
||||
|
||||
def get_related_videos(self, video_id, count=50):
|
||||
if isinstance(video_id, Video):
|
||||
|
|
Loading…
Reference in a new issue