Skip to content

Instantly share code, notes, and snippets.

@avinashgardas
Created May 16, 2021 19:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avinashgardas/6dd6cfc928263d26d766eb5927327264 to your computer and use it in GitHub Desktop.
Save avinashgardas/6dd6cfc928263d26d766eb5927327264 to your computer and use it in GitHub Desktop.
Fetch YouTube playlists with all playlist-items from a YouTube channel in NodeJS
const express = require("express");
const { getData } = require("./youtube/youtube-channel-videos-with-playlist");
const router = express.Router();
router.get("/youtube/playlists", getData);
module.exports = router;
const { google } = require("googleapis");
const youtube = google.youtube("v3");
let playlists = [];
let playlistsObj = {};
const getPlaylists = async (pageToken = null) => {
const params = {
key: YOUTUBE_TOKEN,
part: "id, snippet",
channelId: YOUTUBE_CHANNEL_ID,
pageToken,
maxResults: 50,
};
try {
const result = await youtube.playlists.list(params);
result.data.items.forEach((item) => {
// create array of playlists
playlists.push({
id: item.id
});
// NEW
const playlist = {
id: item.id,
title: item.snippet.title,
channelId: item.snippet.channelId,
channelTitle: item.snippet.channelTitle,
publishedAt: item.snippet.publishedAt,
thumbnail: {
// default: item.snippet?.thumbnails?.default?.url || "",
// medium: item.snippet?.thumbnails?.medium?.url || "",
high: item.snippet?.thumbnails?.high?.url || "",
// standard: item.snippet?.thumbnails?.standard?.url || "",
maxres: item.snippet?.thumbnails?.maxres?.url || "",
},
playlistItems: {
playlistId: item.id,
length: 0,
data: [],
},
};
playlistsObj[item?.id] = playlist;
});
if (result.data.nextPageToken) {
await getPlaylists(result.data.nextPageToken);
} else {
// console.log(playlists.length);
}
} catch (err) {
console.error(err);
}
return playlists;
};
const getPlaylistItems = async (playlistId, pageToken = null) => {
const params = {
key: YOUTUBE_TOKEN,
channelId: YOUTUBE_CHANNEL_ID,
part: "id, snippet",
playlistId,
pageToken,
maxResults: 50,
};
try {
const result = await youtube.playlistItems.list(params);
let items = [];
result.data.items.forEach((item) => {
items.push({
// playlistId,
id: item.id,
title: item.snippet.title,
thumbnail: {
// default: item.snippet?.thumbnails?.default?.url || "",
// medium: item.snippet?.thumbnails?.medium?.url || "",
high: item.snippet?.thumbnails?.high?.url || "",
// standard: item.snippet?.thumbnails?.standard?.url || "",
maxres: item.snippet?.thumbnails?.maxres?.url || "",
},
url: `https://www.youtube.com/watch?v=${item.snippet.resourceId.videoId}`,
});
});
// NEW
playlistsObj[playlistId].playlistItems.length += parseInt(
result?.data?.items?.length
);
playlistsObj[playlistId].playlistItems.data = [
...playlistsObj[playlistId].playlistItems.data,
...items,
];
if (result.data.nextPageToken) {
await getPlaylistItems(playlistId, result.data.nextPageToken);
} else {
// console.log("-", playlistItems.length);
}
} catch (err) {
console.log(err);
}
};
exports.getData = async (req, res) => {
// get array of playlists
const result = await getPlaylists();
result.forEach(async (playlist) => {
try {
await getPlaylistItems(playlist.id);
} catch (err) {
console.log(err);
}
});
if (result?.error) {
res.send({ data: null, success: false, error: result.message });
} else {
setTimeout(() => {
res.send({
data: playlistsObj,
success: true,
error: null,
});
}, 1000);
}
// reset
playlists = [];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment