Files
chromanews/index.js
2024-02-21 18:47:21 +01:00

42 lines
1.6 KiB
JavaScript

import { exec } from 'child_process';
const { log } = require('./Modules/logManager');
const { get } = require('./Modules/fetchManager');
const { createCron } = require('./Modules/cronManager');
const { generateHtml } = require('./Modules/htmlManager');
const { subs, discord } = require('./config.json');
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.');
}
main();
createCron('monday 0 0', main);