Get a list of videos from a playlist

This commit is contained in:
Christian Colglazier 2021-06-28 18:31:19 -04:00
parent f0dbb580b2
commit 1f5eaa231a
2 changed files with 25 additions and 9 deletions

View File

@ -33,4 +33,16 @@ class YouTubeAPI(object):
privacy = status['privacyStatus']
return title, date, channel, channelId, description, tags, privacy
def getPlaylist(self, id, pageToken=''):
if(pageToken):
pageToken = '&pageToken={}'.format(pageToken)
return self.getRequest('playlistItems?part=snippet&maxResults=50&playlistId={}{}'.format(id,
pageToken))
def getPlaylistVideos(self, id):
playlist = self.getPlaylist(id)
items = playlist['items']
while 'nextPageToken' in playlist:
playlist = self.getPlaylist(id, playlist['nextPageToken'])
items.extend(playlist['items'])
return items

View File

@ -3,15 +3,19 @@
from youtube_api import YouTubeAPI
import sys
# Read YouTube API Key
# Read YouTube API Key.
def getYouTubeAPIKey():
return open('youtube.key', 'r').read()
# Print out information about a video.
def printVideoInfo(info):
title, date, channel, channelId, description, tags, privacy = info
print('Title: ' + title)
print('Date: ' + date)
print('Channel: {} ({})'.format(channel, channelId) )
print('Description: ' + description)
print('Tags: ', tags)
print('Privacy: ' + privacy)
api = YouTubeAPI(getYouTubeAPIKey())
title, date, channel, channelId, description, tags, privacy = api.getVideoInfo(sys.argv[1])
print('Title: ' + title)
print('Date: ' + date)
print('Channel: {} ({})'.format(channel, channelId) )
print('Description: ' + description)
print('Tags: ', tags)
print('Privacy: ' + privacy)
print(len(api.getPlaylistVideos(sys.argv[1])))