index.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { type Handler } from "aws-lambda";
  2. import { createRestAPIClient } from "masto";
  3. import ScraperReports from "../../utils/scraper-reports";
  4. import RedisClient from "../../libs/redis-client";
  5. import LogLevels from "../../enums/log-levels";
  6. import Emojis from "../../enums/emojis";
  7. import config from "../../config";
  8. import { type IScraperReportsOptions } from "../../interfaces/scaper-reports-options";
  9. export default class SismologiaCL {
  10. private readonly _name: string;
  11. private readonly _scraperReportsOptions: IScraperReportsOptions
  12. private readonly _redisClient: RedisClient;
  13. private readonly _mastodonClient: any
  14. private readonly _scraperReports: ScraperReports;
  15. constructor (name: string, scraperReportsOptions: IScraperReportsOptions) {
  16. this._name = name;
  17. this._scraperReportsOptions = scraperReportsOptions;
  18. this._scraperReports = new ScraperReports(this._name, this._scraperReportsOptions);
  19. this._redisClient = new RedisClient();
  20. this._mastodonClient = createRestAPIClient({
  21. url: config.MASTODON_URL,
  22. accessToken: config.MASTODON_ACCESS_TOKEN
  23. });
  24. }
  25. public async run (event?: any, context?: any): Promise<void> {
  26. try {
  27. const reports = await this._scraperReports.getItems();
  28. if (config.LOG_LEVEL === LogLevels.DEBUG) {
  29. console.log(`${this._name} | Reports`, reports);
  30. }
  31. let totalPublished = 0;
  32. const length = reports.length;
  33. // Order has to be reversed to appear in the correct order when posting
  34. for (let i = length - 1; i >= 0; i--) {
  35. const report = reports[i];
  36. const exists = await this._redisClient.retrieve(report.link);
  37. if (exists !== null) {
  38. continue;
  39. }
  40. const date = new Date(Date.now()).toLocaleDateString();
  41. let message = `${Emojis.SIREN} Reporte de #sismo`
  42. message += `\n\n${Emojis.CALENDAR} ${report.date} (Hora local)`;
  43. message += `\n${Emojis.MAGNITUDE} Magnitud: ${report.magnitude} - ${Emojis.DEPTH} Profundidad: ${report.depth}`;
  44. message += `\n${Emojis.LOCATION}${report.location.replace(report.date, "").trim()}`;
  45. message += `\n${report.link}`;
  46. if (message.trim().length === 0) {
  47. continue;
  48. }
  49. const mediaIds: any[] = [];
  50. if (report.locationImg !== null && report.locationImg !== undefined) {
  51. const media = await this._mastodonClient.v2.media.create({ file: report.locationImg, description: report.location });
  52. mediaIds.push(media.id);
  53. }
  54. console.log(`\n${this._name} | Sending`, message);
  55. await this._mastodonClient.v1.statuses.create({ status: message, mediaIds });
  56. await this._redisClient.store(report.link, date, { EX: 60 * 60 * 168 }); // EX: 1 week
  57. totalPublished++;
  58. }
  59. console.log(`${this._name} | Published ${totalPublished} new reports`);
  60. } catch (err: any) {
  61. console.log(`${this._name} | An error has occurred\n`)
  62. console.error(err.message);
  63. if (config.LOG_LEVEL === LogLevels.DEBUG) {
  64. if (event !== undefined) {
  65. console.debug("\nEvent\n");
  66. console.debug(event);
  67. }
  68. if (context !== undefined) {
  69. console.debug("\nContext\n");
  70. console.debug(context);
  71. }
  72. }
  73. }
  74. console.log(`${this._name} | Finished`);
  75. }
  76. public getHandler (): Handler {
  77. return async (event, context) => {
  78. await this.run(event, context);
  79. }
  80. }
  81. }