Skip to content

Instantly share code, notes, and snippets.

@Evolution0
Created January 21, 2017 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Evolution0/aaa3b64063d0c0325f519225c470c500 to your computer and use it in GitHub Desktop.
Save Evolution0/aaa3b64063d0c0325f519225c470c500 to your computer and use it in GitHub Desktop.
from apiclient.discovery import build
DEVELOPER_KEY = "DEVELOPER_KEY"
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