Created
February 14, 2017 22:30
-
-
Save Evolution0/6f2a7f36aa27fe3a6ae61b1ec6a37740 to your computer and use it in GitHub Desktop.
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 googleapiclient.discovery import build | |
DEVELOPER_KEY = "API_KEY" | |
def fetch_all_youtube_videos(channel_id): | |
youtube = build("youtube", "v3", developerKey=DEVELOPER_KEY) | |
res = youtube.search().list( | |
part="snippet", | |
type="video", | |
channelId=channel_id, | |
maxResults="50" | |
).execute() | |
next_page_token = res['nextPageToken'] | |
while 'nextPageToken' in res: | |
next_page = youtube.search().list( | |
part="snippet", | |
type="video", | |
channelId=channel_id, | |
maxResults="50", | |
pageToken=next_page_token | |
).execute() | |
res['items'] = res['items'] + next_page['items'] | |
if 'nextPageToken' not in next_page: | |
res.pop('nextPageToken', None) | |
else: | |
next_page_token = next_page['nextPageToken'] | |
return res | |
if __name__ == '__main__': | |
videos = fetch_all_youtube_videos("CHANNEL_ID") | |
videos_c = [] | |
for i in videos['items']: | |
tmp = { | |
'title': i['snippet']['title'], | |
'thumbnail': i['snippet']['thumbnails']['high']['url'], | |
'id': i['id']['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