Skip to content

Commit

Permalink
index: code and performance optimizations
Browse files Browse the repository at this point in the history
- switched from `node-fetch` to `undici` since it has 0 dependencies and was made by the Node.js organization.
- made so that `readConfig()` won't do I/O calls every time you call it. the config data should be stored in a global variable called `configData`, `writeConfig()` would also overwrite this.
- other misc code refactors. see changes in commits.
  • Loading branch information
VoltrexKeyva committed Feb 28, 2022
2 parents ea17227 + 94212ca commit 9742050
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
27 changes: 10 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { WebhookClient, MessageEmbed, Formatters } from 'discord.js';
import fetch from 'node-fetch';
import { request } from 'undici';
import cheerio from 'cheerio';
import chalk from 'chalk';
import { readFileSync, writeFileSync } from 'node:fs';
import { setTimeout as setPromisedTimeout } from 'timers/promises';
import { randomInt } from 'node:crypto';

let configData = JSON.parse(readFileSync('./config.json', 'utf-8'));
const { hyperlink, quote, time } = Formatters;

class Feed {
Expand All @@ -23,7 +24,7 @@ class Feed {
{
const mainVideo = htmlData.attr('data-twitpic');

if (mainVideo !== undefined && mainVideo.includes('video'))
if (mainVideo?.includes('video'))
this.video = mainVideo;
else {
const twitterVideo = htmlData
Expand Down Expand Up @@ -62,9 +63,7 @@ class Article {
* @returns {any}
*/
function readConfig(key) {
const data = JSON.parse(readFileSync('./config.json', 'utf-8'));

return key != undefined ? data[key] : data;
return key != undefined ? configData[key] : configData;
}

/**
Expand All @@ -73,11 +72,8 @@ function readConfig(key) {
* @returns {void}
*/
function writeConfig(data) {
const config = readConfig();

for (const [key, value] of Object.entries(data)) config[key] = value;

writeFileSync('./config.json', JSON.stringify(config));
configData = Object.assign(configData, data);
writeFileSync('./config.json', JSON.stringify(configData));
}

const webhookClient = new WebhookClient({ url: readConfig('url') ?? null });
Expand Down Expand Up @@ -151,9 +147,8 @@ while (true) {
);
prettyLog('+', 'Fetching all articles and parsing HTML...');

const $ = await fetch('https://liveuamap.com/')
.then((res) => res.text())
.then((html) => cheerio.load(html));
const liveuamapResponse = await request('https://liveuamap.com/');
const $ = cheerio.load(await liveuamapResponse.body.text());

let latestNews = $('div[id="feedler"]');

Expand All @@ -166,7 +161,6 @@ while (true) {
);

await setPromisedTimeout(5000);

continue;
}

Expand All @@ -177,9 +171,8 @@ while (true) {
if (news.id !== (readConfig('lastId') ?? null)) {
prettyLog('+', 'New article found, checking article...');

const $_ = await fetch(news.extra)
.then((res) => res.text())
.then((html) => cheerio.load(html));
const extraResponse = await request(news.extra);
const $_ = cheerio.load(await extraResponse.body.text());

await sendToWebhook(new Article(news, $_));

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"chalk": "^5.0.0",
"cheerio": "^1.0.0-rc.10",
"discord.js": "^13.6.0",
"node-fetch": "^3.2.0"
"undici": "^4.0.0"
}
}

0 comments on commit 9742050

Please sign in to comment.