-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
51 lines (42 loc) · 1.47 KB
/
index.js
File metadata and controls
51 lines (42 loc) · 1.47 KB
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 fs = require('fs');
const path = require('path');
const core = require('@actions/core');
async function getLatestVideos(proxyUrl, channelId) {
const url = `${proxyUrl}/get/${channelId}/`;
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
}
async function updateReadme(videos, readmeFilePath) {
const videoLines = videos.map(video =>
`- [${video.title}](https://www.youtube.com/watch?v=${video.videoId})`
);
const readmePath = path.resolve(readmeFilePath);
const content = fs.readFileSync(readmePath, 'utf8');
const startMarker = '<!-- YOUTUBE:START -->';
const endMarker = '<!-- YOUTUBE:END -->';
const startIndex = content.indexOf(startMarker);
const endIndex = content.indexOf(endMarker);
if (startIndex === -1 || endIndex === -1) {
core.setFailed("Markers for YouTube videos not found in README.");
return;
}
const newContent = `${content.slice(0, startIndex + startMarker.length)}
${videoLines.join('\n')}
${content.slice(endIndex)}`;
fs.writeFileSync(readmePath, newContent, 'utf8');
}
(async () => {
try {
const proxyUrl = 'https://latestvid.imgalvin.me';
const channelId = core.getInput('youtube_channel_id');
const readmeFilePath = core.getInput('readme_file_path');
const videos = await getLatestVideos(proxyUrl, channelId);
await updateReadme(videos, readmeFilePath);
} catch (error) {
core.setFailed(error.message);
}
})();