Created
November 13, 2019 01:54
-
-
Save ahgood/6c6b78938f14a4353d404dfeb2513c2a to your computer and use it in GitHub Desktop.
Get all youtube videos ids for specified username
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
const request = require("request"); | |
const apiKey = "..."; | |
const youtubeUserName = "..."; | |
let videos = []; | |
let playlistId; | |
console.log("Getting videos..."); | |
// Get playlist ID | |
request( | |
{ | |
method: "GET", | |
url: | |
"https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername=" + | |
youtubeUserName + | |
"&key=" + | |
apiKey | |
}, | |
function(error, response, body) { | |
let data = JSON.parse(body); | |
playlistId = data.items[0].contentDetails.relatedPlaylists.uploads; | |
getVides(); | |
} | |
); | |
function getVides(token) { | |
let nextToken = token === undefined ? "" : "&pageToken=" + token; | |
request( | |
{ | |
method: "GET", | |
url: | |
"https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=" + | |
playlistId + | |
"&key=" + | |
apiKey + | |
nextToken | |
}, | |
function(error, response, body) { | |
let data = JSON.parse(body); | |
data.items.forEach(function(item) { | |
videos.push(item.snippet.resourceId.videoId); | |
}); | |
if (data.nextPageToken !== undefined) { | |
getVides(data.nextPageToken); | |
} else { | |
// Output all videos ids | |
console.log(videos); | |
} | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment