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__)
|
self.log = vlogging.getLogger(__name__)
|
||||||
|
|
||||||
def get_playlist_videos(self, playlist_id):
|
def _playlist_paginator(self, playlist_id):
|
||||||
page_token = None
|
page_token = None
|
||||||
while True:
|
while True:
|
||||||
response = self.youtube.playlistItems().list(
|
response = self.youtube.playlistItems().list(
|
||||||
|
@ -65,18 +65,21 @@ class Youtube:
|
||||||
part='contentDetails',
|
part='contentDetails',
|
||||||
playlistId=playlist_id,
|
playlistId=playlist_id,
|
||||||
).execute()
|
).execute()
|
||||||
|
|
||||||
|
yield from response['items']
|
||||||
|
|
||||||
page_token = response.get('nextPageToken', None)
|
page_token = response.get('nextPageToken', None)
|
||||||
|
|
||||||
video_ids = [item['contentDetails']['videoId'] for item in response['items']]
|
|
||||||
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:
|
if page_token is None:
|
||||||
break
|
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)
|
||||||
|
|
||||||
|
yield from videos
|
||||||
|
|
||||||
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
|
||||||
|
|
Loading…
Reference in a new issue