-
-
Notifications
You must be signed in to change notification settings - Fork 743
/
Copy pathbuild-newsroom-videos.js
51 lines (41 loc) · 1.63 KB
/
build-newsroom-videos.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
const { writeFileSync } = require('fs-extra');
const { resolve } = require('path');
const fetch = require('node-fetch-2');
async function buildNewsroomVideos(writePath) {
try {
const response = await fetch('https://youtube.googleapis.com/youtube/v3/search?' + new URLSearchParams({
key: process.env.YOUTUBE_TOKEN,
part: 'snippet',
channelId: 'UCIz9zGwDLbrYQcDKVXdOstQ',
eventType: 'completed',
type: 'video',
order: 'Date',
maxResults: 5,
}));
if (!response.ok) {
throw new Error(`HTTP error! with status code: ${response.status}`);
}
const data = await response.json();
console.log(data);
if (!data.items || !Array.isArray(data.items)) {
throw new Error('Invalid data structure received from YouTube API');
}
const videoDataItems = data.items.map((video) => ({
image_url: video.snippet.thumbnails.high.url,
title: video.snippet.title,
description: video.snippet.description,
videoId: video.id.videoId,
}));
const videoData = JSON.stringify(videoDataItems, null, ' ');
console.log('The following are the Newsroom Youtube videos: ', videoData);
writeFileSync(writePath, videoData);
return videoData;
} catch (err) {
throw new Error(`Failed to build newsroom videos: ${err.message}`);
}
}
/* istanbul ignore next */
if (require.main === module) {
buildNewsroomVideos(resolve(__dirname, '../config', 'newsroom_videos.json'))
}
module.exports = { buildNewsroomVideos };