Skip to content

Instantly share code, notes, and snippets.

@breakzplatform
Created November 19, 2024 14:00
Show Gist options
  • Save breakzplatform/5a1f6e1d13f04f877f54a546f433cc7d to your computer and use it in GitHub Desktop.
Save breakzplatform/5a1f6e1d13f04f877f54a546f433cc7d to your computer and use it in GitHub Desktop.
async function getYTPlaylistInfo(apiKey, playlistId) {
const baseUrl = "https://www.googleapis.com/youtube/v3/playlistItems";
const results = [];
let nextPageToken = "";
try {
do {
const res = await fetch(
`${baseUrl}?part=snippet&maxResults=50&playlistId=${playlistId}&key=${apiKey}&pageToken=${nextPageToken}`
);
const data = await res.json();
if (data.error) {
console.error(data.error);
return;
}
data.items.forEach(item => {
const title = item.snippet.title;
const description = item.snippet.description;
results.push({ title, description });
});
nextPageToken = data.nextPageToken || "";
} while (nextPageToken);
return results;
} catch (e) {
console.error(e);
}
}
const output = await getYTPlaylistInfo(apiKey, playlistId);
console.log(output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment