Created
November 19, 2024 14:00
-
-
Save breakzplatform/5a1f6e1d13f04f877f54a546f433cc7d to your computer and use it in GitHub Desktop.
This file contains 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
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