From a86815b29658ee9727a704c98cd0fece89c17906 Mon Sep 17 00:00:00 2001 From: Allen Kinzalow Date: Fri, 1 Mar 2024 19:25:47 -0500 Subject: [PATCH] [#82] Gracefully handle news post category fetching failures --- .changeset/famous-windows-grin.md | 5 +++++ .../news/sections/newsHeader/newsHeader.ts | 9 +++------ .../news/sections/newsHeader/newsHeader.utils.ts | 15 +++++++++++++++ 3 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 .changeset/famous-windows-grin.md diff --git a/.changeset/famous-windows-grin.md b/.changeset/famous-windows-grin.md new file mode 100644 index 0000000..4304112 --- /dev/null +++ b/.changeset/famous-windows-grin.md @@ -0,0 +1,5 @@ +--- +"osrs-web-scraper": patch +--- + +Gracefully handle news post category fetching failures diff --git a/src/scrapers/news/sections/newsHeader/newsHeader.ts b/src/scrapers/news/sections/newsHeader/newsHeader.ts index 67ecf17..c2802a6 100644 --- a/src/scrapers/news/sections/newsHeader/newsHeader.ts +++ b/src/scrapers/news/sections/newsHeader/newsHeader.ts @@ -12,6 +12,7 @@ import Parser from "rss-parser"; import { NEWS_RSS_LINK, + getLatestRSSCateogry, getNewsCategory, getNewsUrlIdentifier, } from "./newsHeader.utils"; @@ -24,12 +25,8 @@ import { NewsSection } from "../types"; const newsHeader: NewsSection = { format: async (html, url, title) => { - const rss = await new Parser().parseURL(NEWS_RSS_LINK); - const urlIdentifier = getNewsUrlIdentifier(url); - const item = rss.items.find( - (item) => getNewsUrlIdentifier(item.link) === urlIdentifier - ); - const category = getNewsCategory(item?.categories?.[0] ?? ""); + const rssCategory = await getLatestRSSCateogry(url); + const category = getNewsCategory(rssCategory ?? ""); const headerRoot = parse(html); diff --git a/src/scrapers/news/sections/newsHeader/newsHeader.utils.ts b/src/scrapers/news/sections/newsHeader/newsHeader.utils.ts index c068970..fbd0eaa 100644 --- a/src/scrapers/news/sections/newsHeader/newsHeader.utils.ts +++ b/src/scrapers/news/sections/newsHeader/newsHeader.utils.ts @@ -1,3 +1,5 @@ +import Parser from "rss-parser"; + export const NEWS_RSS_LINK = "https://secure.runescape.com/m=news/a=1/latest_news.rss?oldschool=true"; @@ -51,6 +53,19 @@ const updateCategories: { [key: string]: NewsCategory } = { "Your Feedback": "yourfeedback", }; +export const getLatestRSSCateogry = async (url: string) => { + try { + const rss = await new Parser().parseURL(NEWS_RSS_LINK); + const urlIdentifier = getNewsUrlIdentifier(url); + const item = rss?.items?.find( + (item) => getNewsUrlIdentifier(item?.link) === urlIdentifier + ); + return item?.categories?.[0]; + } catch (error) { + return undefined; + } +}; + export const getNewsCategory = (rawCategory: string) => updateCategories[rawCategory] ?? "game";