Skip to content

Instantly share code, notes, and snippets.

@ahgood
Created November 13, 2019 01:54
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 ahgood/6c6b78938f14a4353d404dfeb2513c2a to your computer and use it in GitHub Desktop.
Save ahgood/6c6b78938f14a4353d404dfeb2513c2a to your computer and use it in GitHub Desktop.
Get all youtube videos ids for specified username
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