Jelajahi Sumber

Merge branch 'develop' of Mastodon/bots into main

Pablo Yaksic 1 Minggu lalu
induk
melakukan
28da0f6f64
2 mengubah file dengan 39 tambahan dan 14 penghapusan
  1. 1 1
      package.json
  2. 38 13
      src/agents/video-streams/index.ts

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "bot-noticias",
-  "version": "0.0.31",
+  "version": "0.0.32",
   "description": "Bot que busca noticias y las replica en mastodon.cl",
   "main": "dist/index.js",
   "scripts": {

+ 38 - 13
src/agents/video-streams/index.ts

@@ -9,6 +9,7 @@ import LogLevels from "../../enums/log-levels";
 import config from "../../config";
 
 interface IStreamConfig {
+  id: number;
   location: string;
   url: string;
   hashtags: string[];
@@ -21,6 +22,7 @@ export default class VideoStreams {
   private readonly _name: string = "Video Streams";
   private readonly _mastodonClient: mastodon.rest.Client;
   private readonly _scraper: Scraper;
+  private readonly _defaultLocation: string = "Transmisión en vivo";
 
   constructor(accessToken = config.MASTODON_TEST_ACCESS_TOKEN) {
     accessToken = config.DEVELOP ? config.MASTODON_TEST_ACCESS_TOKEN : accessToken;
@@ -40,16 +42,31 @@ export default class VideoStreams {
         throw new Error("VIDEO_STREAMS must be a JSON array");
       }
 
-      return parsed.map((item: any) => ({
-        location: String(item.location ?? "").trim(),
-        url: String(item.url ?? "").trim(),
-        hashtags: Array.isArray(item.hashtags)
-          ? item.hashtags.map((h: unknown) => String(h).trim()).filter((h: string) => h !== "")
-          : [],
-        screenshotSelector: item.screenshotSelector !== undefined ? String(item.screenshotSelector).trim() : undefined,
-        clickSelector: item.clickSelector !== undefined ? String(item.clickSelector).trim() : undefined,
-        screenshotDelay: item.screenshotDelay !== undefined ? Number(item.screenshotDelay) : undefined
-      })).filter((stream) => stream.location !== "" && stream.url !== "");
+      let id = 1;
+      const streams: IStreamConfig[] = [];
+
+      parsed.forEach((item: any) => {
+        const location = String(item.location ?? "").trim();
+        const url = String(item.url ?? "").trim();
+
+        if (url === "") {
+          return;
+        }
+
+        streams.push({
+          id: id++,
+          location,
+          url,
+          hashtags: Array.isArray(item.hashtags)
+            ? item.hashtags.map((h: unknown) => String(h).trim()).filter((h: string) => h !== "")
+            : [],
+          screenshotSelector: item.screenshotSelector !== undefined ? String(item.screenshotSelector).trim() : undefined,
+          clickSelector: item.clickSelector !== undefined ? String(item.clickSelector).trim() : undefined,
+          screenshotDelay: item.screenshotDelay !== undefined ? Number(item.screenshotDelay) : undefined
+        });
+      });
+
+      return streams;
     } catch (err: any) {
       console.error(`${this._name} | Error parsing VIDEO_STREAMS: ${err.message}`);
       throw err;
@@ -61,12 +78,20 @@ export default class VideoStreams {
     return new File([buffer], fileName, { type: "image/png" });
   }
 
+  private generateUniqueTag(id: number): string {
+    return `EnVivoCL_${String(id).padStart(3, "0")}`;
+  }
+
   private buildMessage(stream: IStreamConfig): string {
-    let message = `${Emojis.VIDEO_CAMERA} Transmisión en vivo desde ${stream.location}`;
+    const location = stream.location !== "" ? stream.location : this._defaultLocation;
+    const uniqueTag = this.generateUniqueTag(stream.id);
+    const tags = [uniqueTag, ...stream.hashtags];
+
+    let message = `${Emojis.VIDEO_CAMERA} Transmisión en vivo desde ${location}`;
     message += `\n\n${Emojis.LINK} ${stream.url}`;
 
-    if (stream.hashtags.length > 0) {
-      message += `\n\n${stream.hashtags.map((hashtag) => `#${hashtag}`).join(" ")}`;
+    if (tags.length > 0) {
+      message += `\n\n${tags.map((hashtag) => `#${hashtag}`).join(" ")}`;
     }
 
     return message;