|
@@ -0,0 +1,79 @@
|
|
|
+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 = "Interferencia";
|
|
|
+ const baseDomain = "https://interferencia.cl"
|
|
|
+ try {
|
|
|
+ const redisClient = new RedisClient();
|
|
|
+ const mastodon = createRestAPIClient({
|
|
|
+ url: config.MASTODON_URL,
|
|
|
+ accessToken: config.MASTODON_ACCESS_TOKEN
|
|
|
+ });
|
|
|
+ const scraperArticles = new ScraperArticles(name, {
|
|
|
+ url: config.INTERFERENCIA,
|
|
|
+ articlesSelector: "div.row.container:eq(6) div.row div.col-md-4, div.row.container:eq(6) div.row div.col-md-6, div.row.container:eq(6) div.row div.col-md-8",
|
|
|
+ titleSelector: "div.views-field-title",
|
|
|
+ contentSelector: "div.views-field-field-subhead div.field-content p",
|
|
|
+ linkSelector: "div.views-field-title a",
|
|
|
+ imageSelector: "",
|
|
|
+ authorSelector: "div.views-field-field-cover-authors div.field-content",
|
|
|
+ dateSelector: "div.views-field-created span.field-content"
|
|
|
+ });
|
|
|
+
|
|
|
+ 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();
|
|
|
+ let message = `${Emojis.NEWS} ${article.title}.\n\n${article.content}\n${baseDomain}${article.link}`;
|
|
|
+
|
|
|
+ if (message.trim().length === 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (message.length > 400) {
|
|
|
+ message = `${Emojis.NEWS} ${article.title}.\n\n${article.content}`.substring(0, 397) + "...";
|
|
|
+ message = `${message}\n${baseDomain}${article.link}`;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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.";
|
|
|
+};
|