Skip to content

Instantly share code, notes, and snippets.

@Evolution0
Created February 14, 2017 22:30
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/6f2a7f36aa27fe3a6ae61b1ec6a37740 to your computer and use it in GitHub Desktop.
Save Evolution0/6f2a7f36aa27fe3a6ae61b1ec6a37740 to your computer and use it in GitHub Desktop.
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