Last active
April 1, 2020 06:30
-
-
Save davidlevy/d347c87da1b2d3c94dfcd02341c610b7 to your computer and use it in GitHub Desktop.
Get list of Youtube channel videos
This file contains hidden or 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
#code from https://stackoverflow.com/questions/18953499/youtube-api-to-fetch-all-videos-on-a-channel | |
#1. get an API Key : https://console.developers.google.com/apis/credentials | |
#2. enable Youtube API : https://console.developers.google.com/apis/api/youtube.googleapis.com/ | |
import urllib | |
import json | |
API_KEY = 'YOUR_API_KEY_HERE' | |
CHANNEL_ID = 'YOUR_CHANNEL_ID' | |
def get_all_video_in_channel(channel_id): | |
base_video_url = 'https://www.youtube.com/watch?v=' | |
base_search_url = 'https://www.googleapis.com/youtube/v3/search?' | |
first_url = base_search_url+'key={}&channelId={}&part=snippet,id&order=date&maxResults=25'.format(API_KEY, channel_id) | |
video_links = [] | |
url = first_url | |
while True: | |
inp = urllib.urlopen(url) | |
resp = json.load(inp) | |
try: | |
for i in resp['items']: | |
if i['id']['kind'] == "youtube#video": | |
video_links.append(base_video_url + i['id']['videoId']) | |
except: | |
print resp | |
try: | |
next_page_token = resp['nextPageToken'] | |
url = first_url + '&pageToken={}'.format(next_page_token) | |
except: | |
break | |
return video_links | |
if __name__ =='__main__': | |
links = get_all_video_in_channel(CHANNEL_ID) | |
print("\n".join(links)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment