How to process all Playlists under an YouTube account. For example, I would like to read all Playlists under https://www.youtube.com/@adventisthealthcare/playlists and iterate through each Playlist for all the videos.
I have implemented Java wrapper using YouTube Data API v3 to retrieve all videos under a Playlist, but is there an API to fetch Playlists from the above account URL.
I suggest you use Piped's open source API, which accesses content on YouTube. I use it to retrieve the content of a playlist and play it on an audio player written in react JS.
I'll let you have a look at the API documentation:
https://docs.piped.video/docs/api-documentation/
For example on my web app I use the API like this:
import express from 'express';
const instances = [
{ name: 'leptons.xyz', url: 'https://pipedapi.leptons.xyz' },
{ name: 'lunar.icu', url: 'https://piped-api.lunar.icu' },
{ name: 'r4fo.com', url: 'https://pipedapi.r4fo.com' },
{ name: 'ngn.tf', url: 'https://pipedapi.ngn.tf' }
];
export const getPlaylist = async (req: express.Request, res: express.Response) => {
try {
const playlistId: string = req.params.playlistId;
const apiUrl: string = `https://pipedapi.kavin.rocks/playlists/${playlistId}`;
const playlistData = await fetch(apiUrl).then(res => res.json())
res.json(playlistData);
} catch (error) {
console.error('Error fetching playlist data:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
};
And for the stream content I write this:
import express from 'express';
const instances = [
{ name: 'leptons.xyz', url: 'https://pipedapi.leptons.xyz' },
{ name: 'lunar.icu', url: 'https://piped-api.lunar.icu' },
{ name: 'r4fo.com', url: 'https://pipedapi.r4fo.com' },
{ name: 'ngn.tf', url: 'https://pipedapi.ngn.tf' }
];
/**
* @swagger
* components:
* schemas:
* Stream:
* type: object
* properties:
* title:
* type: string
* description:
* type: string
*/
export const getStream = async (req: express.Request, res: express.Response) => {
let streamData: any = null;
const videoId: string = req.params.videoId;
try {
const apiUrl: string = `https://pipedapi.kavin.rocks/streams/${videoId}`;
const response = await Promise.race([
fetch(apiUrl),
new Promise((_, reject) => setTimeout(() => reject(new Error('Timeout')), 5000))
]);
if (isResponse(response)) {
if (response.ok) {
streamData = await response.json();
res.json(streamData);
return;
} else {
throw new Error('API request failed');
}
}
} catch (error) {
console.error('Error fetching stream data:', error);
console.log("fetching with another instance...")
// Retry with another instance if available
for (const instance of instances) {
if (instance.url !== 'https://pipedapi.kavin.rocks') {
try {
const apiUrl: string = `${instance.url}/streams/${videoId}`;
const response = await fetch(apiUrl);
if (isResponse(response) && response.ok) {
streamData = await response.json();
res.json(streamData);
return;
}
} catch (error) {
console.error(`Error fetching from ${instance.name}:`, error);
}
}
}
res.status(500).json({ error: 'Internal Server Error' });
}
};
function isResponse(response: any): response is Response {
return !!response && typeof response === 'object' && 'ok' in response && 'json' in response;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With