Create separate get_video/get_videos, plural looks better.

Not a big difference, but I didn't like the way
calling get_video([...]) looked.
This commit is contained in:
voussoir 2020-08-12 11:20:43 -07:00
parent 299ea6b579
commit 2da4568fb5

View file

@ -65,8 +65,9 @@ class Youtube:
playlistId=playlist_id, 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']]
videos = self.get_video(video_ids) videos = self.get_videos(video_ids)
videos.sort(key=lambda x: x.published, reverse=True) videos.sort(key=lambda x: x.published, reverse=True)
self.log.debug('Got %d more videos.', len(videos)) self.log.debug('Got %d more videos.', len(videos))
@ -87,9 +88,9 @@ class Youtube:
type='video', type='video',
maxResults=count, maxResults=count,
).execute() ).execute()
videos = []
related = [rel['id']['videoId'] for rel in results['items']] related = [rel['id']['videoId'] for rel in results['items']]
videos = self.get_video(related) videos = self.get_videos(related)
return videos return videos
def get_user_id(self, username): def get_user_id(self, username):
@ -113,16 +114,19 @@ class Youtube:
def get_user_videos(self, uid): def get_user_videos(self, uid):
yield from self.get_playlist_videos(self.get_user_uploads_playlist_id(uid)) yield from self.get_playlist_videos(self.get_user_uploads_playlist_id(uid))
def get_video(self, video_ids): def get_video(self, video_id):
if isinstance(video_ids, str): videos = self.get_videos([video_id])
singular = True
video_ids = [video_ids]
else:
singular = False
if len(videos) == 1:
return videos[0]
elif len(videos) == 0:
raise VideoNotFound(video_id)
def get_videos(self, video_ids):
snippets = [] snippets = []
chunks = helpers.chunk_sequence(video_ids, 50) chunks = helpers.chunk_sequence(video_ids, 50)
for chunk in chunks: for chunk in chunks:
self.log.debug('Requesting batch of %d video ids.', len(chunk))
chunk = ','.join(chunk) chunk = ','.join(chunk)
data = self.youtube.videos().list( data = self.youtube.videos().list(
part='id,contentDetails,snippet,statistics', part='id,contentDetails,snippet,statistics',
@ -130,6 +134,7 @@ class Youtube:
).execute() ).execute()
items = data['items'] items = data['items']
snippets.extend(items) snippets.extend(items)
videos = [] videos = []
broken = [] broken = []
for snippet in snippets: for snippet in snippets:
@ -141,9 +146,4 @@ class Youtube:
if broken: if broken:
# print('broken:', broken) # print('broken:', broken)
pass pass
if singular:
if len(videos) == 1:
return videos[0]
elif len(videos) == 0:
raise VideoNotFound(video_ids[0])
return videos return videos