-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchVideos.js
77 lines (66 loc) · 2.45 KB
/
fetchVideos.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const { google } = require('googleapis');
const youtube = google.youtube({
version: 'v3',
auth: process.env.YOUTUBE_DATA_API_KEY // Replace with your API key
});
async function fetchVideosFromPlaylist(playlistId) {
try {
const oneDayAgo = new Date();
oneDayAgo.setDate(oneDayAgo.getDate() - 1); // Set to 24 hours ago
const response = await youtube.playlistItems.list({
playlistId,
part: 'snippet',
maxResults: 5 // You may need to adjust this
});
let videoDetails = await Promise.all(
response.data.items.map(async (item) => {
const videoResponse = await youtube.videos.list({
id: item.snippet.resourceId.videoId,
part: 'contentDetails'
});
return {
...item,
duration: videoResponse.data.items[0].contentDetails.duration,
publishedAt: item.snippet.publishedAt
};
})
);
// Filter by date and duration
videoDetails = videoDetails.filter(video => {
const publishedDate = new Date(video.publishedAt);
const duration = convertDurationToMinutes(video.duration);
return publishedDate >= oneDayAgo && duration >= 0; // condition for minimum time duration of video
});
return videoDetails.map(video => `https://www.youtube.com/watch?v=${video.snippet.resourceId.videoId}`);
} catch (error) {
console.error('Error fetching playlist videos: ', error);
return [];
}
}
async function fetchPlaylistName(playlistId) {
try {
const response = await youtube.playlists.list({
id: playlistId,
part: 'snippet',
maxResults: 1
});
if (response.data.items.length > 0) {
return response.data.items[0].snippet.title;
} else {
console.log(`No playlist found with the ID: ${playlistId}`);
return null;
}
} catch (error) {
console.error(`Error fetching playlist name for ID ${playlistId}: `, error);
return null;
}
}
function convertDurationToMinutes(duration) {
let match = duration.match(/PT(\d+H)?(\d+M)?(\d+S)?/);
let hours = (parseInt(match[1]) || 0);
let minutes = (parseInt(match[2]) || 0);
let seconds = (parseInt(match[3]) || 0);
// Convert everything to minutes
return hours * 60 + minutes + seconds / 60;
}
module.exports = { fetchVideosFromPlaylist,fetchPlaylistName };