import { exec } from 'child_process'; import express from 'express'; import cors from 'cors'; const { log } = require('./Modules/logManager'); const { get } = require('./Modules/fetchManager'); const { createCron } = require('./Modules/cronManager'); const { generateHtml } = require('./Modules/htmlManager'); const { subbWebSub, unsubWebSub } = require('./Modules/webSubManager'); const { port, subs, youtube, discord } = require('./config.json'); const app = express(); app.use(express.json()); app.set('trust proxy', 1); app.use(cors({ origin: '*', })); async function main() { const posts = []; for (const sub of subs) { log(`Fetching data from ${sub}...`); const res = await get(`https://www.reddit.com/r/${sub}/top/.json?t=week`); log(`Fetched ${res.data.children.length} posts.`); for (const post of res.data.children) { let tldr = null; if (sub == 'CryptoCurrency' && post.data.link_flair_text == 'GENERAL-NEWS') { const comments = await get(`https://www.reddit.com${post.data.permalink}.json`); for (const comment of comments[1].data.children) { if (comment.data.author == 'coinfeeds-bot') tldr = comment.data.body; } } posts.push({ title: post.data.title, url: `https://www.reddit.com${post.data.permalink}`, linked: post.data.url, description: `${post.data.selftext ? post.data.selftext : 'No text'}`, image: post.data.thumbnail != 'self' ? post.data.thumbnail : null, flair: post.data.link_flair_text ? post.data.link_flair_text : 'No flair', date: new Date(post.data.created_utc * 1000), tldr: tldr, }); } } log('Generating HTML...'); generateHtml(posts); log('Sending webhook...'); discord.webhook ? exec(`curl -X POST -F 'payload_json={"username":"ChromaNews","avatar_url":"${discord.avatar_url}","content":"New weekly news!"}' -F 'file1=@index.html' ${discord.webhook}`) : log('No webhook found.'); } for (const channel of youtube.channels) subbWebSub(youtube.callback, `https://www.youtube.com/feeds/videos.xml?channel_id=${channel}`, 'https://pubsubhubbub.appspot.com/subscribe'); createCron('monday 0 0', main); app.get((youtube.callback).replace(/^.*\/\/[^/]+/, ''), (req, res) => { log('YouTube callback received.'); res.send('ok'); }); app.listen(port, async () => { log(`running on port ${port}`); }); process.on('SIGINT', function() { console.log('Stopping server...'); for (const channel of youtube.channels) unsubWebSub(youtube.callback, `https://www.youtube.com/feeds/videos.xml?channel_id=${channel}`, 'https://pubsubhubbub.appspot.com/subscribe'); process.exit(); });