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') {
const item = items.item(itemsI); this.rssLink = channelChild?.textContent ?? null;
const itemTitle = item?.getElementsByTagName('title')[0] ?? null; } else if (channelChild?.tagName === 'item') {
const itemLink = item?.getElementsByTagName('link')[0] ?? null; const items = channelChild?.children;
allItems.push({ let itemTitle = '';
title: itemTitle?.textContent ?? 'Undefined', let itemLink = null;
link: itemLink?.textContent ?? null,
}); for (let itemsI = 0; itemsI < items?.length; itemsI++) {
const item = items.item(itemsI);
if (item?.tagName === 'title') {
itemTitle = item?.textContent ?? 'Undefined';
} else if (item?.tagName === 'link') {
itemLink = item?.textContent ?? null;
}
}
allItems.push({
title: itemTitle,
link: itemLink,
});
}
} }
this.rssItems = allItems; this.rssItems = allItems;
this.requestUpdate(); this.requestUpdate();
} catch (error) { } catch (error) {