Skip to content

Instantly share code, notes, and snippets.

@huffmanks
Last active June 3, 2024 14:33
Show Gist options
  • Save huffmanks/11442e23625b73a3405932ec50d962da to your computer and use it in GitHub Desktop.
Save huffmanks/11442e23625b73a3405932ec50d962da to your computer and use it in GitHub Desktop.
function parseRSS(xmlString) {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, "application/xml");
const channel = xmlDoc.querySelector("channel");
const items = channel?.querySelectorAll("item");
if (!items) return;
const rssArr = [];
items?.forEach((item) => {
const itemObject = {};
itemObject.title = item.querySelector("title").textContent;
itemObject.guid = item.querySelector("guid").textContent;
itemObject.author = item.querySelector("author").textContent;
itemObject.eventDate = item.querySelector("eventDate").textContent;
itemObject.eventTime = item.querySelector("eventTime").textContent;
itemObject.description = item.querySelector("description").textContent;
itemObject.formattedDate = item.querySelector("formattedDate").textContent;
itemObject.utcOffset = item.querySelector("utcOffset").textContent;
rssArr.push(itemObject);
});
return rssArr;
}
async function fetchRSS(rssUrl) {
try {
const response = await fetch(rssUrl);
const xmlString = await response.text();
const rssData = parseRSS(xmlString);
console.log("rssData__", rssData);
return rssData;
} catch (error) {
console.error("Failed to fetch and parse RSS feed:", error);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment