Code improvements

- Better error handling
- More output
- Better logic
This commit is contained in:
Logan Cusano
2024-05-22 02:15:56 -04:00
parent 4e71c7b167
commit 429ddd3333
2 changed files with 75 additions and 106 deletions

View File

@@ -22,59 +22,49 @@ export class EmmeliaEmbedBuilder extends EmbedBuilder {
export const sendPost = (post, source, channel) => {
log.DEBUG("Sending post from source: ", post, source);
const postTitle = String(post.title).substring(0, 150);
const postLink = post.link;
let postContent;
if (post.content) {
// Reset the content parameter with the encoded parameter
post.content = parse(post['content:encoded'] ?? post.content);
// Get the post content and trim it to length or add a placeholder if necessary
let postText = String(post.content.text);
if (postText.length >= 3800) postText = `${postText.slice(0, 3800).substring(0, Math.min(postText.length, postText.lastIndexOf(" ")))} [...](${post.link})`;
else if (postText.length === 0) postText = `*This post has no content* [Direct Link](${post.link})`;
let postContent = `*This post has no content* [Direct Link](${post.link})`;
if (post.content || post['content:encoded']) {
const content = post['content:encoded'] ?? post.content;
const parsedContent = parse(content);
let postText = parsedContent.text.trim();
if (postText.length >= 3800) {
postText = `${postText.slice(0, 3800).substring(0, postText.lastIndexOf(" "))} [...](${post.link})`;
} else if (postText.length === 0) {
postText = `*This post has no content* [Direct Link](${post.link})`;
}
postContent = postText;
} else {
postContent = `*This post has no content* [Direct Link](${post.link})`;
}
// Check for embedded youtube videos and add the first four as links
const ytVideos = String(post.content).match(youtubeVideoRegex);
if (ytVideos) {
for (let ytVideo of ytVideos.slice(0, 4)) {
// If the video is an embed, replace the embed to make it watchable
if (ytVideo.includes("embed")) ytVideo = ytVideo.replace("embed/", "watch?v=");
postContent += `\nEmbeded Video from Post: [YouTube](${ytVideo})`;
// Check for embedded YouTube videos and add the first four as links
const ytVideos = content.match(youtubeVideoRegex);
if (ytVideos) {
ytVideos.slice(0, 4).forEach((ytVideo) => {
if (ytVideo.includes("embed")) ytVideo = ytVideo.replace("embed/", "watch?v=");
postContent += `\nEmbedded Video from Post: [YouTube](${ytVideo})`;
});
}
// Extract the first image link if available
const imageLinks = parsedContent.querySelectorAll("a")
.map(link => link.getAttribute("href"))
.filter(href => href && href.match(imageRegex));
if (imageLinks.length > 0) {
post.image = imageLinks[0];
}
}
log.DEBUG("Post content: ", postContent);
const postId = post.postId;
if (!post.pubDate) post.pubDate = Date.now();
const postPubDate = new Date(post.pubDate).toISOString();
const postPubDate = new Date(post.pubDate || Date.now()).toISOString();
const postSourceLink = source.title;
let postImage = post.image ?? undefined;
const postImage = post.image;
if (!postImage) {
if (post.content) {
const linksInPost = post.content.querySelectorAll("a");
if (linksInPost) {
log.DEBUG("Found links in post:", linksInPost);
for (const link of linksInPost) {
// Check to see if this link is a youtube video that was already found, if so skip it
if (ytVideos?.includes(link)) continue;
const images = String(link.getAttribute("href")).match(imageRegex);
log.DEBUG("Images found in post:", images);
if (images) {
postImage = images[0];
}
}
}
}
}
log.DEBUG("Post content: ", postContent);
log.DEBUG("Sending an RSS post to discord", postTitle, postId, postContent);
try {
const rssMessage = new EmmeliaEmbedBuilder()
.setColor(0x0099FF)
@@ -83,20 +73,16 @@ export const sendPost = (post, source, channel) => {
.addFields({ name: 'Source', value: postSourceLink, inline: true })
.addFields({ name: 'Published', value: postPubDate, inline: true });
// 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);
}
// Add the main content if it's present
postContent = postContent.slice(0, 4090).trim();
if (postContent) rssMessage.setDescription(postContent);
const channelResponse = rssMessage;
//const channelResponse = channel.send({ embeds: [rssMessage] });
const channelResponse = channel.send({ embeds: [rssMessage] });
log.DEBUG("Channel send response", channelResponse);
return channelResponse;