Add Youtube.get_related_videos

This commit is contained in:
voussoir 2017-05-21 13:50:17 -07:00
parent 7c7a9b881b
commit 2959673f74

View file

@ -26,6 +26,9 @@ class Video:
best_thumbnail = max(thumbnails, key=lambda x: thumbnails[x]['width'] * thumbnails[x]['height']) best_thumbnail = max(thumbnails, key=lambda x: thumbnails[x]['width'] * thumbnails[x]['height'])
self.thumbnail = thumbnails[best_thumbnail] self.thumbnail = thumbnails[best_thumbnail]
def __str__(self):
return 'Video:%s' % self.id
class Youtube: class Youtube:
def __init__(self, key): def __init__(self, key):
@ -68,6 +71,23 @@ class Youtube:
if page_token is None or count < 50: if page_token is None or count < 50:
break break
def get_related_videos(self, video_id, part='id,snippet', count=50):
if isinstance(video_id, Video):
video_id = video_id.id
results = self.youtube.search().list(
part=part,
relatedToVideoId=video_id,
type='video',
maxResults=count,
).execute()
videos = []
for related in results['items']:
related['id'] = related['id']['videoId']
video = Video(related)
videos.append(video)
return videos
def get_video(self, video_ids): def get_video(self, video_ids):
if isinstance(video_ids, str): if isinstance(video_ids, str):
singular = True singular = True
@ -81,7 +101,7 @@ class Youtube:
chunk = ','.join(chunk) chunk = ','.join(chunk)
data = self.youtube.videos().list(part='snippet', id=chunk).execute() data = self.youtube.videos().list(part='snippet', id=chunk).execute()
items = data['items'] items = data['items']
results += items results.extend(items)
results = [Video(snippet) for snippet in results] results = [Video(snippet) for snippet in results]
if singular: if singular:
if len(results) == 1: if len(results) == 1: