Skip to content

Commit

Permalink
fix(route/bbc): bbc news 404 url issue
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudoyu committed Dec 17, 2024
1 parent 4785a0f commit efbaf06
Showing 1 changed file with 47 additions and 42 deletions.
89 changes: 47 additions & 42 deletions lib/routes/bbc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -59,52 +58,58 @@ 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<string, any>;
}
})
)
);

return {
title,
link,
image: 'https://www.bbc.com/favicon.ico',
description: title,
item: items,
item: items.filter((item) => Object.keys(item).length > 0),
};
}

0 comments on commit efbaf06

Please sign in to comment.