Created
March 4, 2020 18:56
-
-
Save pdelteil/40d45bfd0f3e6ddbe7cc352a973aafbf 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
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