import { type Handler } from "aws-lambda"; import { createRestAPIClient } from "masto"; import ScraperReports from "../../utils/scraper-reports"; import RedisClient from "../../libs/redis-client"; import LogLevels from "../../enums/log-levels"; import Emojis from "../../enums/emojis"; import config from "../../config"; import { type IScraperReportsOptions } from "../../interfaces/scaper-reports-options"; export default class SismologiaCL { private readonly _name: string; private readonly _scraperReportsOptions: IScraperReportsOptions private readonly _redisClient: RedisClient; private readonly _mastodonClient: any private readonly _scraperReports: ScraperReports; constructor (name: string, scraperReportsOptions: IScraperReportsOptions) { this._name = name; this._scraperReportsOptions = scraperReportsOptions; this._scraperReports = new ScraperReports(this._name, this._scraperReportsOptions); this._redisClient = new RedisClient(); this._mastodonClient = createRestAPIClient({ url: config.MASTODON_URL, accessToken: config.MASTODON_ACCESS_TOKEN }); } public async run (event?: any, context?: any): Promise { try { const reports = await this._scraperReports.getItems(); if (config.LOG_LEVEL === LogLevels.DEBUG) { console.log(`${this._name} | Reports`, reports); } let totalPublished = 0; const length = reports.length; // Order has to be reversed to appear in the correct order when posting for (let i = length - 1; i >= 0; i--) { const report = reports[i]; const exists = await this._redisClient.retrieve(report.link); if (exists !== null) { continue; } const date = new Date(Date.now()).toLocaleDateString(); let message = `${Emojis.SIREN} Reporte de #sismo` message += `\n\n${Emojis.CALENDAR} ${report.date} (Hora local)`; message += `\n${Emojis.MAGNITUDE} Magnitud: ${report.magnitude} - ${Emojis.DEPTH} Profundidad: ${report.depth}`; message += `\n${Emojis.LOCATION}${report.location.replace(report.date, "").trim()}`; message += `\n${report.link}`; if (message.trim().length === 0) { continue; } const mediaIds: any[] = []; if (report.locationImg !== null && report.locationImg !== undefined) { const media = await this._mastodonClient.v2.media.create({ file: report.locationImg, description: report.location }); mediaIds.push(media.id); } console.log(`\n${this._name} | Sending`, message); await this._mastodonClient.v1.statuses.create({ status: message, mediaIds }); await this._redisClient.store(report.link, date, { EX: 60 * 60 * 168 }); // EX: 1 week totalPublished++; } console.log(`${this._name} | Published ${totalPublished} new reports`); } catch (err: any) { console.log(`${this._name} | An error has occurred\n`) console.error(err.message); if (config.LOG_LEVEL === LogLevels.DEBUG) { if (event !== undefined) { console.debug("\nEvent\n"); console.debug(event); } if (context !== undefined) { console.debug("\nContext\n"); console.debug(context); } } } console.log(`${this._name} | Finished`); } public getHandler (): Handler { return async (event, context) => { await this.run(event, context); } } }