Last active
July 31, 2018 10:05
-
-
Save Evolution0/0e7de4198f44518355b511c128eff591 to your computer and use it in GitHub Desktop.
Grab Youtube playlist (Full List)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from apiclient.discovery import build | |
DEVELOPER_KEY = "API KEY HERE" | |
def fetch_all_youtube_videos(playlistId): | |
youtube = build("youtube", "v3", developerKey=DEVELOPER_KEY) | |
res = youtube.playlistItems().list( | |
part="snippet", | |
playlistId=playlistId, | |
maxResults="50" | |
).execute() | |
nextPageToken = res.get('nextPageToken') | |
while ('nextPageToken' in res): | |
nextPage = youtube.playlistItems().list( | |
part="snippet", | |
playlistId=playlistId, | |
maxResults="50", | |
pageToken=nextPageToken | |
).execute() | |
res['items'] = res['items'] + nextPage['items'] | |
if 'nextPageToken' not in nextPage: | |
res.pop('nextPageToken', None) | |
else: | |
nextPageToken = nextPage['nextPageToken'] | |
return res | |
if __name__ == '__main__': | |
# Playlist ID | |
videos = fetch_all_youtube_videos("Playlist ID") | |
videos_c = [] | |
for i in videos['items']: | |
tmp = {} | |
tmp['title'] = i['snippet']['title'] | |
tmp['thumbnail'] = "https://img.youtube.com/vi/" + i['snippet']['resourceId']['videoId'] + "/maxresdefault.jpg" | |
tmp['id'] = i['snippet']['resourceId']['videoId'] | |
videos_c.append(tmp) | |
for item in videos_c: | |
print("Title: {0}\nURL: https://www.youtube.com/watch?v={1}\nThumbnail: {2}\n".format(item['title'], item['id'], item['thumbnail'])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment