|
| 1 | +import Parser from 'rss-parser'; |
| 2 | + |
| 3 | +const WEBHOOK_URL = process.env.WEBHOOK_URL; |
| 4 | +const CHANNEL_ID = 'UCGxzWLH1_1ABKKfSiy2WIAw'; |
| 5 | +const RSS_URL = `https://www.youtube.com/feeds/videos.xml?channel_id=${CHANNEL_ID}`; |
| 6 | +const LAST_ID_FILE = process.env.LAST_ID_FILE || 'last_video_id.txt'; |
| 7 | + |
| 8 | +async function main() { |
| 9 | + const parser = new Parser(); |
| 10 | + const response = await fetch(RSS_URL); |
| 11 | + const feedText = await response.text(); |
| 12 | + const feed = await parser.parseString(feedText); |
| 13 | + if (!feed.items || feed.items.length === 0) { |
| 14 | + console.log('No entries found in RSS feed'); |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + const latestVideo = feed.items[0]; |
| 19 | + const latestId = latestVideo.id?.split(':').pop(); |
| 20 | + const videoUrl = latestVideo.link; |
| 21 | + const videoTitle = latestVideo.title; |
| 22 | + console.log(`Latest video: ${videoTitle} (ID: ${latestId})`); |
| 23 | + |
| 24 | + let lastId = ''; |
| 25 | + try { |
| 26 | + lastId = await Bun.file(LAST_ID_FILE).text(); |
| 27 | + } catch {} |
| 28 | + if (lastId === latestId) { |
| 29 | + console.log(`No new video (last ID: ${lastId})`); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + const message = { |
| 34 | + content: `**${videoTitle}**\nNew video uploaded!\n${videoUrl}` |
| 35 | + }; |
| 36 | + const res = await fetch(WEBHOOK_URL!, { |
| 37 | + method: 'POST', |
| 38 | + headers: { 'Content-Type': 'application/json' }, |
| 39 | + body: JSON.stringify(message) |
| 40 | + }); |
| 41 | + console.log(`Discord response: ${res.status}`); |
| 42 | + |
| 43 | + if (res.status === 204) { |
| 44 | + await Bun.write(LAST_ID_FILE, latestId!); |
| 45 | + console.log('Notification sent successfully!'); |
| 46 | + } else { |
| 47 | + const errorText = await res.text(); |
| 48 | + console.log(`Failed to send notification: ${errorText}`); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +main() |
| 53 | + |
0 commit comments