This commit is contained in:
Logan Cusano
2023-03-10 23:14:16 -05:00
parent d19ccd046a
commit 6cccc86d90
3 changed files with 35 additions and 4 deletions

View File

@@ -2,6 +2,9 @@ const { EmbedBuilder } = require('discord.js');
const { DebugBuilder } = require("./utilities/debugBuilder");
const log = new DebugBuilder("server", "libUtils");
const { NodeHtmlMarkdown } = require('node-html-markdown');
const { parse } = require("node-html-parser");
const imageRegex = /(http(s?):)([/|.|\w|\s|-])*((\.(?:jpg|gif|png|webm))|(\/gallery\/(?:[/|.|\w|\s|-])*))/g;
exports.EmmeliaEmbedBuilder = class PostEmbedBuilder extends EmbedBuilder {
constructor() {
@@ -70,14 +73,33 @@ exports.onError = (error) => {
}
exports.sendPost = (post, source, channel, callback) => {
log.DEBUG("Sending post from source: ", post, source);
post.content = parse(post.content);
const postTitle = post.title;
const postLink = post.link;
const postContent = NodeHtmlMarkdown.translate(post.content);
const postContent = NodeHtmlMarkdown.translate(post.content.text);
log.DEBUG("Post content: ", postContent);
const postId = post.postId;
const postPubDate = new Date(post.pubDate).toISOString() ?? new Date().toISOString();
var postSourceLink = new URL(source.link);
postSourceLink = postSourceLink.hostname;
const postImage = post.image ?? undefined;
var postImage = post.image ?? undefined;
if (!postImage){
const linksInPost = post.content.querySelectorAll("a");
if (linksInPost) {
log.DEBUG("Found links in post:", linksInPost);
for (const link of linksInPost) {
const images = String(link.getAttribute("href")).match(imageRegex);
log.DEBUG("Images found in post:", images);
if (images) {
postImage = images[0];
}
}
}
}
log.DEBUG("Sending an RSS post to discord", postTitle, postId)
try{
const rssMessage = new this.EmmeliaEmbedBuilder()
@@ -88,9 +110,16 @@ exports.sendPost = (post, source, channel, callback) => {
.addFields({ name: 'Published', value: postPubDate, inline: true })
.addFields({ name: 'Source', value: postSourceLink, inline: true });
if (postImage) rssMessage.setImage(postImage);
// TODO - If there is more than one image, create a canvas and post the created canvas
if (postImage) {
log.DEBUG("Image from post:", postImage);
rssMessage.setImage(postImage);
}
channel.send({ embeds: [rssMessage] });
//throw new Error("YOU SHALL NOT PASS");
return callback(undefined, true);
}
catch (err){