|
@@ -2,6 +2,7 @@ import { type Handler } from "aws-lambda";
|
|
|
import { createRestAPIClient, mastodon } from "masto";
|
|
import { createRestAPIClient, mastodon } from "masto";
|
|
|
|
|
|
|
|
import ScraperArticles from "../utils/scraper-articles";
|
|
import ScraperArticles from "../utils/scraper-articles";
|
|
|
|
|
+import TagGenerator from "../utils/tag-generator";
|
|
|
import RedisClient from "../libs/redis-client";
|
|
import RedisClient from "../libs/redis-client";
|
|
|
import LogLevels from "../enums/log-levels";
|
|
import LogLevels from "../enums/log-levels";
|
|
|
import Emojis from "../enums/emojis";
|
|
import Emojis from "../enums/emojis";
|
|
@@ -15,12 +16,14 @@ export default class Portal {
|
|
|
private readonly _redisClient: RedisClient;
|
|
private readonly _redisClient: RedisClient;
|
|
|
private readonly _mastodonClient: mastodon.rest.Client
|
|
private readonly _mastodonClient: mastodon.rest.Client
|
|
|
private readonly _scraperArticles: ScraperArticles;
|
|
private readonly _scraperArticles: ScraperArticles;
|
|
|
|
|
+ private readonly _tagGenerator: TagGenerator;
|
|
|
|
|
|
|
|
constructor (name: string, accessToken = config.MASTODON_TEST_ACCESS_TOKEN, scraperArticlesOptions: IScraperArticlesOptions) {
|
|
constructor (name: string, accessToken = config.MASTODON_TEST_ACCESS_TOKEN, scraperArticlesOptions: IScraperArticlesOptions) {
|
|
|
this._name = name;
|
|
this._name = name;
|
|
|
this._scraperArticlesOptions = scraperArticlesOptions;
|
|
this._scraperArticlesOptions = scraperArticlesOptions;
|
|
|
this._scraperArticles = new ScraperArticles(this._name, this._scraperArticlesOptions);
|
|
this._scraperArticles = new ScraperArticles(this._name, this._scraperArticlesOptions);
|
|
|
this._redisClient = new RedisClient();
|
|
this._redisClient = new RedisClient();
|
|
|
|
|
+ this._tagGenerator = new TagGenerator(this._name);
|
|
|
|
|
|
|
|
accessToken = config.DEVELOP ? config.MASTODON_TEST_ACCESS_TOKEN : accessToken;
|
|
accessToken = config.DEVELOP ? config.MASTODON_TEST_ACCESS_TOKEN : accessToken;
|
|
|
this._mastodonClient = createRestAPIClient({ url: config.MASTODON_URL, accessToken });
|
|
this._mastodonClient = createRestAPIClient({ url: config.MASTODON_URL, accessToken });
|
|
@@ -64,18 +67,28 @@ export default class Portal {
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // If the message is more than 400 characters long, its very likely due to the article.content
|
|
|
|
|
- if (message.length > 400) {
|
|
|
|
|
- message = message.substring(0, 397) + "...";
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // Generating hashtags from article content using Ollama
|
|
|
|
|
+ const generatedTags = await this._tagGenerator.generate(article.title, article.content);
|
|
|
|
|
+ const tags = generatedTags.length > 0
|
|
|
|
|
+ ? generatedTags
|
|
|
|
|
+ : (this._scraperArticlesOptions.hashtags ?? []);
|
|
|
|
|
|
|
|
- // Adding hashtags if there is
|
|
|
|
|
- if (this._scraperArticlesOptions.hashtags) {
|
|
|
|
|
- message += `\n${Emojis.TAGS} ${this._scraperArticlesOptions.hashtags?.map((hastag) => `#${hastag}`).join(" ")}\n\n`;
|
|
|
|
|
|
|
+ if (tags.length > 0) {
|
|
|
|
|
+ message += `\n${Emojis.TAGS} ${tags.map((hashtag) => `#${hashtag}`).join(" ")}`;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
message += `\n${Emojis.LINK} ${article.link}`;
|
|
message += `\n${Emojis.LINK} ${article.link}`;
|
|
|
|
|
|
|
|
|
|
+ // Mastodon allows a maximum of 500 characters, but historically this bot limits to 400
|
|
|
|
|
+ if (message.length > 400) {
|
|
|
|
|
+ const linkLine = `\n${Emojis.LINK} ${article.link}`;
|
|
|
|
|
+ const tagsLine = tags.length > 0
|
|
|
|
|
+ ? `\n${Emojis.TAGS} ${tags.map((hashtag) => `#${hashtag}`).join(" ")}`
|
|
|
|
|
+ : "";
|
|
|
|
|
+ const maxContentLength = 400 - linkLine.length - tagsLine.length;
|
|
|
|
|
+ message = `${Emojis.NEWS} ${article.title}\n\n${article.content.substring(0, Math.max(0, maxContentLength - 10))}...${tagsLine}${linkLine}`;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
if (this._name.toUpperCase() == "Chile Cultura".toUpperCase()) {
|
|
if (this._name.toUpperCase() == "Chile Cultura".toUpperCase()) {
|
|
|
// Icons Chilecultura
|
|
// Icons Chilecultura
|
|
|
let regex = new RegExp(/\n(\d{1,2} \w{3})\s*\n?\s*-\s*(\d{1,2} \w{3})/);
|
|
let regex = new RegExp(/\n(\d{1,2} \w{3})\s*\n?\s*-\s*(\d{1,2} \w{3})/);
|