October 23, 2023
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
export async function getAllVideosInYoutubePlaylist(playlistId, apiKey) {
const { default: axios } = await import("npm:axios");
if (!playlistId) {
console.error("No Playlist ID Provided");
return null;
}
if (!apiKey) {
console.warn("API Key is Invalid");
return null;
}
const getPlaylistVideos = async (pageToken = "") => {
return await axios({
method: "GET",
url: "https://www.googleapis.com/youtube/v3/playlistItems",
params: {
key: apiKey,
part: "contentDetails,id,snippet",
playlistId,
maxResults: 50,
pageToken,
},
});
};
let result = await getPlaylistVideos();
let items = result.data.items;
while (result?.data?.nextPageToken) {
result = await getPlaylistVideos(result.data.nextPageToken);
if (result?.data?.items) {
items = items.concat(result.data.items);
}
}
const { length } = items;
console.log(
length
? `Successfully fetched ${length} video${length === 1 ? "" : "s"}`
: "No videos found",
);
return items;
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023