diff --git a/youtube_api.py b/youtube_api.py index e90ec2c..0dfbf60 100644 --- a/youtube_api.py +++ b/youtube_api.py @@ -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 diff --git a/yt-unlist-saver.py b/yt-unlist-saver.py index a49c3f9..cae6554 100644 --- a/yt-unlist-saver.py +++ b/yt-unlist-saver.py @@ -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])))