Add cache to florida man

This commit is contained in:
Dragon Fire
2024-04-04 17:20:43 -04:00
parent fdc4268f22
commit 3dbaddd9e5
+6 -1
View File
@@ -36,6 +36,8 @@ module.exports = class FloridaManCommand extends Command {
} }
] ]
}); });
this.cache = new Map();
} }
async run(msg, { month, day }) { async run(msg, { month, day }) {
@@ -49,14 +51,17 @@ module.exports = class FloridaManCommand extends Command {
} }
async fetchArticle(month, day) { async fetchArticle(month, day) {
if (this.cache.has(`${month}-${day}`)) return this.cache.get(`${month}-${day}`);
try { try {
const { text } = await request.get(`https://floridamanbirthday.org/${months[month - 1]}-${day}`); const { text } = await request.get(`https://floridamanbirthday.org/${months[month - 1]}-${day}`);
const $ = cheerio.load(text); const $ = cheerio.load(text);
return { const result = {
title: decodeHTML($('p').first().children().first().text()), title: decodeHTML($('p').first().children().first().text()),
firstLine: decodeHTML($('p').eq(1).children().first().text()), firstLine: decodeHTML($('p').eq(1).children().first().text()),
image: `https:${$('img').eq(1).attr('data-lazy-src')}` image: `https:${$('img').eq(1).attr('data-lazy-src')}`
}; };
this.cache.set(`${month}-${day}`, result);
return result;
} catch (err) { } catch (err) {
if (err.status === 404) return null; if (err.status === 404) return null;
throw err; throw err;