From f9f962e54ec880b1ae3bfe4fe20bfc07499eb395 Mon Sep 17 00:00:00 2001 From: William Moore Date: Mon, 12 Jun 2023 08:06:32 -0500 Subject: [PATCH] Update to parse cleaner --- src/RSS.ts | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/src/RSS.ts b/src/RSS.ts index ed8eae2..5fc4f83 100644 --- a/src/RSS.ts +++ b/src/RSS.ts @@ -36,28 +36,41 @@ export class RSS extends LitElement { }; try { + const allItems: RSSItem[] = []; const response = await fetch(url, options); const result = await response.text(); const rss = (new DOMParser()).parseFromString(result, 'text/xml'); - console.log(rss); - const title = rss.getElementsByTagName('title')[0] ?? null; - const link = rss.getElementsByTagName('link')[0] ?? null; - this.rssTitle = title?.textContent ?? 'Undefined'; - this.rssLink = link?.textContent ?? null; - const items = rss.getElementsByTagName('item'); - const allItems: RSSItem[] = []; - for (let itemsI = 0; itemsI < items.length; itemsI++) { - const item = items.item(itemsI); - const itemTitle = item?.getElementsByTagName('title')[0] ?? null; - const itemLink = item?.getElementsByTagName('link')[0] ?? null; - allItems.push({ - title: itemTitle?.textContent ?? 'Undefined', - link: itemLink?.textContent ?? null, - }); + const channel = rss.getElementsByTagName('channel')[0] ?? null; + + for (let channelI = 0; channelI < channel?.children?.length; channelI++) { + const channelChild = channel?.children?.item(channelI); + + if (channelChild?.tagName === 'title') { + this.rssTitle = channelChild?.textContent ?? 'Undefined'; + } 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); + 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.requestUpdate(); } catch (error) {