Skip to content

Instantly share code, notes, and snippets.

@pdelteil
Created March 4, 2020 18:56
Show Gist options
  • Save pdelteil/40d45bfd0f3e6ddbe7cc352a973aafbf to your computer and use it in GitHub Desktop.
Save pdelteil/40d45bfd0f3e6ddbe7cc352a973aafbf to your computer and use it in GitHub Desktop.
import urllib.request
import json
import math
import sys
#Get list of videos from Youtube channel
def getVideos(maxResults, uploadId, key,nextPageToken, videosOutput):
videos = []
urld = searchURL+"/playlistItems?part=snippet%2CcontentDetails&maxResults="+maxResults+"&playlistId="+uploadId+"&key="+key+"&pageToken="+nextPageToken
with urllib.request.urlopen(urld) as url:
datad = json.loads(url.read())
totalResults = datad['pageInfo']['totalResults']
for data in datad['items']:
ntitle = data['snippet']['title']
nlink = data['contentDetails']['videoId']
videosOutput.append([nlink,ntitle])
if 'nextPageToken' in datad:
nextPageToken = datad['nextPageToken']
#recursive call
getVideos(maxResults, uploadId, key, nextPageToken, videosOutput)
#API KEY
key = "API_KEY"
videoURL = "http://www.youtube.com/watch?v="
searchURL = "https://www.googleapis.com/youtube/v3"
maxResults= "50"
nextPageToken = ""
ytids = []
with open(sys.argv[1]) as file:
ytids = file.read().splitlines()
file.close()
videos = []
#print(ytids)
for ytid in ytids:
urld = searchURL+"/channels?part=contentDetails&id="+ytid+"&key="+key
with urllib.request.urlopen(urld) as url:
datad = json.loads(url.read())
uploadsdet = datad['items']
uploadId = uploadsdet[0]['contentDetails']['relatedPlaylists']['uploads']
getVideos(maxResults, uploadId, key,nextPageToken, videos)
for link,title in videos:
print(videoURL+link)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment