Attempted Bugfix for logan/Emmelia-Link-Flayer-Rewrite#2
This commit is contained in:
@@ -103,7 +103,8 @@ exports.updateFeeds = async (client) => {
|
|||||||
try{
|
try{
|
||||||
log.DEBUG("Parsed Feed Keys", Object.keys(parsedFeed), parsedFeed?.title);
|
log.DEBUG("Parsed Feed Keys", Object.keys(parsedFeed), parsedFeed?.title);
|
||||||
if (parsedFeed?.items){
|
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);
|
//log.VERBOSE("Post from feed: ", post);
|
||||||
if (post.title && post.link && post.content && ( post.postId || post.guid || post.id ) && post.pubDate){
|
if (post.title && post.link && post.content && ( post.postId || post.guid || post.id ) && post.pubDate){
|
||||||
post.postId = post.postId ?? post.guid ?? post.id;
|
post.postId = post.postId ?? post.guid ?? post.id;
|
||||||
|
|||||||
35
libUtils.js
35
libUtils.js
@@ -2,6 +2,9 @@ const { EmbedBuilder } = require('discord.js');
|
|||||||
const { DebugBuilder } = require("./utilities/debugBuilder");
|
const { DebugBuilder } = require("./utilities/debugBuilder");
|
||||||
const log = new DebugBuilder("server", "libUtils");
|
const log = new DebugBuilder("server", "libUtils");
|
||||||
const { NodeHtmlMarkdown } = require('node-html-markdown');
|
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 {
|
exports.EmmeliaEmbedBuilder = class PostEmbedBuilder extends EmbedBuilder {
|
||||||
constructor() {
|
constructor() {
|
||||||
@@ -70,14 +73,33 @@ exports.onError = (error) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
exports.sendPost = (post, source, channel, callback) => {
|
exports.sendPost = (post, source, channel, callback) => {
|
||||||
|
log.DEBUG("Sending post from source: ", post, source);
|
||||||
|
post.content = parse(post.content);
|
||||||
const postTitle = post.title;
|
const postTitle = post.title;
|
||||||
const postLink = post.link;
|
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 postId = post.postId;
|
||||||
const postPubDate = new Date(post.pubDate).toISOString() ?? new Date().toISOString();
|
const postPubDate = new Date(post.pubDate).toISOString() ?? new Date().toISOString();
|
||||||
var postSourceLink = new URL(source.link);
|
var postSourceLink = new URL(source.link);
|
||||||
postSourceLink = postSourceLink.hostname;
|
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)
|
log.DEBUG("Sending an RSS post to discord", postTitle, postId)
|
||||||
try{
|
try{
|
||||||
const rssMessage = new this.EmmeliaEmbedBuilder()
|
const rssMessage = new this.EmmeliaEmbedBuilder()
|
||||||
@@ -88,9 +110,16 @@ exports.sendPost = (post, source, channel, callback) => {
|
|||||||
.addFields({ name: 'Published', value: postPubDate, inline: true })
|
.addFields({ name: 'Published', value: postPubDate, inline: true })
|
||||||
.addFields({ name: 'Source', value: postSourceLink, 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] });
|
channel.send({ embeds: [rssMessage] });
|
||||||
|
|
||||||
|
//throw new Error("YOU SHALL NOT PASS");
|
||||||
|
|
||||||
return callback(undefined, true);
|
return callback(undefined, true);
|
||||||
}
|
}
|
||||||
catch (err){
|
catch (err){
|
||||||
|
|||||||
@@ -25,6 +25,7 @@
|
|||||||
"http-errors": "~1.6.3",
|
"http-errors": "~1.6.3",
|
||||||
"morgan": "~1.9.1",
|
"morgan": "~1.9.1",
|
||||||
"node-html-markdown": "~1.3.0",
|
"node-html-markdown": "~1.3.0",
|
||||||
|
"node-html-parser": "~6.1.5",
|
||||||
"gpt-3-encoder": "~1.1.4",
|
"gpt-3-encoder": "~1.1.4",
|
||||||
"user-agents": "~1.0.1303"
|
"user-agents": "~1.0.1303"
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user