fix(route/bbc): bbc news 404 url issue
This commit is contained in:
parent
4785a0fe0d
commit
efbaf06671
|
|
@ -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';
|
||||
}
|
||||
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 response = await ofetch(linkURL.href, {
|
||||
retryStatusCodes: [403],
|
||||
});
|
||||
|
||||
const $ = load(response);
|
||||
const $ = load(response);
|
||||
|
||||
const path = linkURL.pathname;
|
||||
const path = linkURL.pathname;
|
||||
|
||||
let description;
|
||||
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($);
|
||||
}
|
||||
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,
|
||||
};
|
||||
})
|
||||
)
|
||||
return {
|
||||
title: item.title || '',
|
||||
description: description || '',
|
||||
pubDate: item.pubDate || new Date().toUTCString(),
|
||||
link: item.link,
|
||||
};
|
||||
} catch {
|
||||
return {} as Record<string, any>;
|
||||
}
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
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),
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue