Skip to content

Instantly share code, notes, and snippets.

@davidlevy
Last active April 1, 2020 06: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 davidlevy/d347c87da1b2d3c94dfcd02341c610b7 to your computer and use it in GitHub Desktop.
Save davidlevy/d347c87da1b2d3c94dfcd02341c610b7 to your computer and use it in GitHub Desktop.
Get list of Youtube channel videos
#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