diff --git a/libCore.js b/libCore.js index d897d69..f7ef6de 100644 --- a/libCore.js +++ b/libCore.js @@ -103,7 +103,8 @@ exports.updateFeeds = async (client) => { try{ log.DEBUG("Parsed Feed Keys", Object.keys(parsedFeed), parsedFeed?.title); if (parsedFeed?.items){ - for (const post of parsedFeed.items){ + for (const post of parsedFeed.items){ + log.DEBUG("Parsed Source Keys", Object.keys(post), post?.title); //log.VERBOSE("Post from feed: ", post); if (post.title && post.link && post.content && ( post.postId || post.guid || post.id ) && post.pubDate){ post.postId = post.postId ?? post.guid ?? post.id; diff --git a/libUtils.js b/libUtils.js index dd884e0..cc8546b 100644 --- a/libUtils.js +++ b/libUtils.js @@ -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){ diff --git a/package.json b/package.json index d5a0f73..1d3b406 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "http-errors": "~1.6.3", "morgan": "~1.9.1", "node-html-markdown": "~1.3.0", + "node-html-parser": "~6.1.5", "gpt-3-encoder": "~1.1.4", "user-agents": "~1.0.1303" },