Public
Back
Version 71
5/31/2023
const leadgenerator = (async () => {
// npm package import
const { default: DOMParser } = await import("npm:dom-parser");
// Function to get the first 2 paragraphs of a random article from RSS feeds
async function getRandomArticleParagraphs(feedUrls) {
const randomFeedUrl = feedUrls[Math.floor(Math.random() * feedUrls.length)];
const response = await fetch(randomFeedUrl); // Use fetch to make an HTTP request to the RSS feed
const xmlString = await response.text(); // Get the XML response as a string
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, "text/xml");
const items = xmlDoc.getElementsByTagName("item"); // Assuming the article entries are enclosed in <item> tags
if (items.length > 0) {
const randomItem = items[Math.floor(Math.random() * items.length)];
const title = randomItem.getElementsByTagName("title")[0].textContent; // Get the title of the random article
const content =
unescapeEntities(
randomItem.getElementsByTagName("content")[0].textContent,
)
.slice(0, 100) + "..."; // Get the description of the random article
console.log(content);
}
else {
console.log("No articles found in the RSS feed.");
}
return content;
}
function unescapeEntities(text) {
const entities = {
"&": "&",
"<": "<",
">": ">",
""": '"',
"'": "'",
"’": "'",
// Add more HTML entities if needed
};
Updated: October 23, 2023