import type { Handler } from "aws-lambda"; import { createRestAPIClient } from "masto"; import "dotenv/config"; import ScraperArticles from "../../utils/scraper-articles"; import RedisClient from "../../libs/redis-client"; import config from "../../config"; import LogLevels from "../../enums/log-levels"; import Emojis from "../../enums/emojis"; export const handler: Handler = async (event, context) => { const name = "El Desconcierto"; try { const redisClient = new RedisClient(); const mastodon = createRestAPIClient({ url: config.MASTODON_URL, accessToken: config.MASTODON_ACCESS_TOKEN }); const scraperArticles = new ScraperArticles(name, { url: config.ELDESCONCIERTO, articlesSelector: "div.the-section__rows figure", titleSelector: "figcaption h2", contentSelector: "figcaption h2", linkSelector: "a", imageSelector: "a img", authorSelector: "", dateSelector: "" }); const articles = await scraperArticles.getArticles(); if (config.LOG_LEVEL === LogLevels.DEBUG) { console.log("Articles", articles); } let totalPublished = 0; const length = articles.length; // Order has to be reversed to appear in the correct order when posting for (let i = length - 1; i >= 0; i--) { const article = articles[i]; const exists = await redisClient.retrieve(article.link); if (exists !== null) { continue; } const date = new Date(Date.now()).toLocaleDateString(); const message = `${Emojis.NEWS} ${article.title} \n${article.link}`; if (message.trim().length === 0) { continue; } console.log("\nSending", message); await mastodon.v1.statuses.create({ status: message }); await redisClient.store(article.link, date, { EX: 60 * 60 * 24 }); // EX: 24 hrs expiration totalPublished++ } console.log(`Published ${totalPublished} new articles`); } catch (err: any) { console.log('An error has occurred\n') console.error(err.message); if (config.LOG_LEVEL === LogLevels.DEBUG) { console.debug("\nEvent\n"); console.debug(event); console.debug("\nContext\n"); console.debug(context); } } return "The End."; };