diff --git a/lib/routes/bbc/index.ts b/lib/routes/bbc/index.ts index e56cba3535bfe4..5eff388ef49a13 100644 --- a/lib/routes/bbc/index.ts +++ b/lib/routes/bbc/index.ts @@ -4,11 +4,10 @@ import parser from '@/utils/rss-parser'; import { load } from 'cheerio'; import utils from './utils'; import ofetch from '@/utils/ofetch'; - export const route: Route = { path: '/:site?/:channel?', name: 'News', - maintainers: ['HenryQW', 'DIYgod'], + maintainers: ['HenryQW', 'DIYgod', 'pseudoyu'], handler, example: '/bbc/world-asia', parameters: { @@ -59,45 +58,51 @@ async function handler(ctx) { } const items = await Promise.all( - feed.items.map((item) => - cache.tryGet(item.link, async () => { - const linkURL = new URL(item.link); - if (linkURL.hostname === 'www.bbc.com') { - linkURL.hostname = 'www.bbc.co.uk'; - } - - const response = await ofetch(linkURL.href, { - retryStatusCodes: [403], - }); - - const $ = load(response); - - const path = linkURL.pathname; - - let description; - - switch (true) { - case path.startsWith('/sport'): - description = item.content; - break; - case path.startsWith('/sounds/play'): - description = item.content; - break; - case path.startsWith('/news/live'): - description = item.content; - break; - default: - description = utils.ProcessFeed($); - } - - return { - title: item.title, - description, - pubDate: item.pubDate, - link: item.link, - }; - }) - ) + feed.items + .filter((item) => item && item.link) + .map((item) => + cache.tryGet(item.link, async () => { + try { + const linkURL = new URL(item.link); + if (linkURL.hostname === 'www.bbc.com') { + linkURL.hostname = 'www.bbc.co.uk'; + } + + const response = await ofetch(linkURL.href, { + retryStatusCodes: [403], + }); + + const $ = load(response); + + const path = linkURL.pathname; + + let description; + + switch (true) { + case path.startsWith('/sport'): + description = item.content; + break; + case path.startsWith('/sounds/play'): + description = item.content; + break; + case path.startsWith('/news/live'): + description = item.content; + break; + default: + description = utils.ProcessFeed($); + } + + return { + title: item.title || '', + description: description || '', + pubDate: item.pubDate || new Date().toUTCString(), + link: item.link, + }; + } catch { + return {} as Record; + } + }) + ) ); return { @@ -105,6 +110,6 @@ async function handler(ctx) { link, image: 'https://www.bbc.com/favicon.ico', description: title, - item: items, + item: items.filter((item) => Object.keys(item).length > 0), }; }