Browse Source

fixed selector LaTercera
added develop flag

Pablo Barrera Yaksic 2 months ago
parent
commit
ed9f3df11e
5 changed files with 38 additions and 14 deletions
  1. 4 0
      .env.example
  2. 3 0
      src/config.ts
  3. 21 5
      src/index.ts
  4. 1 1
      src/portales/latercera/handler.ts
  5. 9 8
      src/portales/portal.ts

+ 4 - 0
.env.example

@@ -25,3 +25,7 @@ MASTODON_KEY_EMOL = ""
 MASTODON_KEY_INTERFERENCIA = ""
 MASTODON_KEY_LATERCERA = ""
 MASTODON_KEY_THECLINIC = ""
+
+# Develop
+DEVELOP = false
+DEV_ACTIVE_PORTALS = "";

+ 3 - 0
src/config.ts

@@ -27,6 +27,9 @@ const config = {
   MASTODON_KEY_INTERFERENCIA: process.env.MASTODON_KEY_INTERFERENCIA ?? "",
   MASTODON_KEY_LATERCERA: process.env.MASTODON_KEY_LATERCERA ?? "",
   MASTODON_KEY_THECLINIC: process.env.MASTODON_KEY_THECLINIC ?? "",
+  // Develop
+  DEVELOP: !(process.env.DEVELOP === "false"),
+  DEV_ACTIVE_PORTALS: process.env.DEV_ACTIVE_PORTALS?.split(";") ?? []
 };
 
 export default config;

+ 21 - 5
src/index.ts

@@ -1,4 +1,6 @@
-import { type Context } from "aws-lambda";
+import { type Context, type Handler } from "aws-lambda";
+
+import config from "./config";
 
 import { handler as chilecultura } from "./portales/chilecultura/handler";
 import { handler as df } from "./portales/df/handler";
@@ -25,12 +27,26 @@ const context: Context = {
   getRemainingTimeInMillis: () => 1
 };
 
-const portals = [chilecultura, df, elciudadano, eldesconcierto, elmostrador, emol, interferencia, latercera, theclinic];
-const cooldown = 10000; // 10 seconds
+const portalsHandlers = {
+  "chilecultura": chilecultura,
+  "df": df,
+  "elciudadano": elciudadano,
+  "eldesconcierto": eldesconcierto,
+  "elmostrador": elmostrador,
+  "emol": emol,
+  "interferencia": interferencia,
+  "latercera": latercera,
+  "theclinic": theclinic,
+};
 
+const activePortalsHandlers: Array<Handler> = Object.keys(portalsHandlers)
+  .filter((portalName) => ! config.DEVELOP || config.DEV_ACTIVE_PORTALS.includes(portalName))
+  .map((portalName) => portalsHandlers[portalName]);
+
+const cooldown = 10000; // 10 seconds
 async function main (): Promise<void> {
-  for (const portal of portals) {
-    await portal(null, context, () => {});
+  for (const portalHandler of activePortalsHandlers) {
+    await portalHandler(null, context, () => {});
     await new Promise((resolve) => setTimeout(resolve, cooldown));
   }
 }

+ 1 - 1
src/portales/latercera/handler.ts

@@ -11,7 +11,7 @@ export const handler: Handler = new Portal(
   config.MASTODON_KEY_LATERCERA,
   {
     url: config.LATERCERA,
-    articlesSelector: "article.card",
+    articlesSelector: "section.top-mainy article.card",
     titleSelector: "div.headline",
     linkSelector: "div.headline a",
     linkPrefix: "https://www.latercera.cl",

+ 9 - 8
src/portales/portal.ts

@@ -48,7 +48,6 @@ export default class Portal {
           continue;
         }
 
-
         if (article.title.includes("Exclusivo suscriptor")) {
           article.title = article.title.replace("Exclusivo suscriptor", "");
         }
@@ -103,13 +102,15 @@ export default class Portal {
 
         console.log(`\n${this._name} | Sending`, message);
 
-        await this._mastodonClient.v1.statuses.create({ status: message, mediaIds });
-        await this._redisClient.store(
-          article.link, 
-          new Date(Date.now()).toLocaleDateString(), 
-          { EX: this._scraperArticlesOptions.cacheExpiration ? this._scraperArticlesOptions.cacheExpiration : 60 * 60 * 24 } // EX: 24 hrs expiration
-        );
-        totalPublished++
+        if (!config.DEVELOP) {
+          await this._mastodonClient.v1.statuses.create({ status: message, mediaIds });
+          await this._redisClient.store(
+            article.link, 
+            new Date(Date.now()).toLocaleDateString(), 
+            { EX: this._scraperArticlesOptions.cacheExpiration ? this._scraperArticlesOptions.cacheExpiration : 60 * 60 * 24 } // EX: 24 hrs expiration
+          );
+          totalPublished++
+        }
       }
       console.log(`${this._name} | Published ${totalPublished} new articles`);
     } catch (err: any) {