diff --git a/SaveMyPlaylist.py b/SaveMyPlaylist.py index 871c7c80fee8448cfccbb48c77ea6f59c08d2c99..6ea4c2225ef7031580365aa65ed1e15b4633b4af 100644 --- a/SaveMyPlaylist.py +++ b/SaveMyPlaylist.py @@ -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 = [] - def __fetch_playlist_items(self): - youtube = googleapiclient.discovery.build(self.API_NAME, self.API_VERSION, developerKey=self.__apiKey) - request = youtube.playlistItems().list( - part='snippet', - playlistId=self.__playlistId, - maxResults=50, - ) + nextPageToken = 0 + while nextPageToken is not None: + pageItems, nextPageToken = self.__fetch_playlist_items(nextPageToken) + items.extend(pageItems) + + 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)