Created
May 16, 2021 19:18
-
-
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
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 express = require("express"); | |
const { getData } = require("./youtube/youtube-channel-videos-with-playlist"); | |
const router = express.Router(); | |
router.get("/youtube/playlists", getData); | |
module.exports = router; |
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 { 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
Stackoverflow - https://stackoverflow.com/questions/18804904/retrieve-all-videos-from-youtube-playlist-using-youtube-v3-api/67560743#67560743