Skip to content
Snippets Groups Projects
Commit 73a6c37f authored by Robert Goldmann's avatar Robert Goldmann
Browse files

implemented paging

parent 6793efb3
No related branches found
No related tags found
No related merge requests found
......@@ -32,43 +32,63 @@ class SaveMyPlaylist:
TITLE = 1
VIDEO_ID = 2
ILLEGAL_CHARS = ['NUL', '\',''//', ':', '*', '"', '<', '>', '|']
def __init__(self, apiKey, playlistId):
self.__apiKey = apiKey
self.__playlistId = playlistId
self.__items = self.__fetch_playlist_items()
self.__youtubeApi = googleapiclient.discovery.build(self.API_NAME, self.API_VERSION, developerKey=self.__apiKey)
self.__items = self.__fetch_all_playlist_items()
def __fetch_all_playlist_items(self):
items = []
nextPageToken = 0
while nextPageToken is not None:
pageItems, nextPageToken = self.__fetch_playlist_items(nextPageToken)
items.extend(pageItems)
def __fetch_playlist_items(self):
youtube = googleapiclient.discovery.build(self.API_NAME, self.API_VERSION, developerKey=self.__apiKey)
request = youtube.playlistItems().list(
print('\n>>> Found {} items in playlist\n'.format(len(items)))
return items
def __fetch_playlist_items(self, nextPageToken=None):
if nextPageToken is None or nextPageToken == 0:
request = self.__youtubeApi.playlistItems().list(
part='snippet',
playlistId=self.__playlistId,
maxResults=50,
)
else:
request = self.__youtubeApi.playlistItems().list(
part='snippet',
playlistId=self.__playlistId,
maxResults=50,
pageToken=nextPageToken
)
response = request.execute()
items = []
for item in response['items']:
snippet = item['snippet']
title = snippet['title']
channel = snippet['channelTitle']
videoId = snippet['resourceId']['videoId']
items.append((channel, title, videoId))
print('{} - {} ({})'.format(channel, title, videoId))
print('{} - {} (videoId: {})'.format(channel, title, videoId))
print('\n>>> Found {} items\n'.format(len(items)))
return items
nextPageToken = None
if 'nextPageToken' in response:
nextPageToken = response['nextPageToken']
return items, nextPageToken
def download_items(self, destinationFolder, debug=False):
print('>>> Scanning destination folder...')
downloadedVideos = [f for f in os.listdir(destinationFolder) if
os.path.isfile(os.path.join(destinationFolder, f))]
os.path.isfile(os.path.join(destinationFolder, f)) and f.endswith('.mp4')]
print('>>> Found {} already downloaded videos'.format(len(downloadedVideos)))
print('>>> Found {} videos in destination folder'.format(len(downloadedVideos)))
print('\n>>> Started Downloading...')
newVideos = []
......@@ -108,7 +128,7 @@ class SaveMyPlaylist:
if __name__ == '__main__':
API_KEY = 'AIzaSyD1cQOPzYDRe5pylXEFsQw6u-MaBSOX09Y'
PLAYLIST_ID = 'PL7F-5FkCMt0F86pdKUaiTStIloZYDVfzl'
PLAYLIST_ID = 'PL7F-5FkCMt0HRvF40L2704WhjJZf9G6SB'
DESTINATION_FOLDER = 'C:/Users/ROGO2/Desktop/123'
saveMyPlaylist = SaveMyPlaylist(API_KEY, PLAYLIST_ID)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment