Update to parse cleaner

This commit is contained in:
William Moore 2023-06-12 08:06:32 -05:00
parent 768d262f34
commit f9f962e54e

View File

@ -36,28 +36,41 @@ export class RSS extends LitElement {
}; };
try { try {
const allItems: RSSItem[] = [];
const response = await fetch(url, options); const response = await fetch(url, options);
const result = await response.text(); const result = await response.text();
const rss = (new DOMParser()).parseFromString(result, 'text/xml'); const rss = (new DOMParser()).parseFromString(result, 'text/xml');
console.log(rss); const channel = rss.getElementsByTagName('channel')[0] ?? null;
const title = rss.getElementsByTagName('title')[0] ?? null;
const link = rss.getElementsByTagName('link')[0] ?? null; for (let channelI = 0; channelI < channel?.children?.length; channelI++) {
this.rssTitle = title?.textContent ?? 'Undefined'; const channelChild = channel?.children?.item(channelI);
this.rssLink = link?.textContent ?? null;
const items = rss.getElementsByTagName('item'); if (channelChild?.tagName === 'title') {
const allItems: RSSItem[] = []; this.rssTitle = channelChild?.textContent ?? 'Undefined';
for (let itemsI = 0; itemsI < items.length; itemsI++) { } else if (channelChild?.tagName === 'link') {
this.rssLink = channelChild?.textContent ?? null;
} else if (channelChild?.tagName === 'item') {
const items = channelChild?.children;
let itemTitle = '';
let itemLink = null;
for (let itemsI = 0; itemsI < items?.length; itemsI++) {
const item = items.item(itemsI); const item = items.item(itemsI);
const itemTitle = item?.getElementsByTagName('title')[0] ?? null; if (item?.tagName === 'title') {
const itemLink = item?.getElementsByTagName('link')[0] ?? null; itemTitle = item?.textContent ?? 'Undefined';
} else if (item?.tagName === 'link') {
itemLink = item?.textContent ?? null;
}
}
allItems.push({ allItems.push({
title: itemTitle?.textContent ?? 'Undefined', title: itemTitle,
link: itemLink?.textContent ?? null, link: itemLink,
}); });
} }
}
this.rssItems = allItems; this.rssItems = allItems;
this.requestUpdate(); this.requestUpdate();
} catch (error) { } catch (error) {