use this method to get data from youtube this will make it fast
const express = require('express');
const axios = require('axios');
const app = express();
const port = 3000;
// Define the YouTube video ID (the ID from the URL you provided)
const videoId = 'MtR0iiSRCck'; // This is just an example. You can make it dynamic.
// Endpoint to fetch data from YouTube's player API
app.get('/data', async (req, res) => {
try {
// Prepare the request to YouTube's API endpoint
const response = await axios.post(
'https://www.youtube.com/youtubei/v1/player?prettyPrint=false',
{
context: {
client: {
clientName: 'WEB',
clientVersion: '2.20230310.02.00',
hl: 'en',
},
},
videoId: videoId, // Use the dynamic videoId if needed
},
{
headers: {
'Content-Type': 'application/json',
'X-Youtube-Client-Name': '1',
'X-Youtube-Client-Version': '2.20230310.02.00',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
'Accept-Language': 'en-US,en;q=0.9',
}
}
);
// Send the response back to the client
res.json(response.data);
} catch (error) {
// Handle any errors
console.error('Error fetching data from YouTube API:', error);
res.status(500).json({ error: 'Failed to fetch data' });
}
});
// Start the server
app.listen(port, () => {
console.log(Server running at http://localhost:${port});
});
use this method to get data from youtube this will make it fast
const express = require('express');
const axios = require('axios');
const app = express();
const port = 3000;
// Define the YouTube video ID (the ID from the URL you provided)
const videoId = 'MtR0iiSRCck'; // This is just an example. You can make it dynamic.
// Endpoint to fetch data from YouTube's player API
app.get('/data', async (req, res) => {
try {
// Prepare the request to YouTube's API endpoint
const response = await axios.post(
'https://www.youtube.com/youtubei/v1/player?prettyPrint=false',
{
context: {
client: {
clientName: 'WEB',
clientVersion: '2.20230310.02.00',
hl: 'en',
},
},
videoId: videoId, // Use the dynamic videoId if needed
},
{
headers: {
'Content-Type': 'application/json',
'X-Youtube-Client-Name': '1',
'X-Youtube-Client-Version': '2.20230310.02.00',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
'Accept-Language': 'en-US,en;q=0.9',
}
}
);
});
// Start the server
app.listen(port, () => {
console.log(
Server running at http://localhost:${port});});