fix(route/cna): Unify full text fetching method (#19346)

This commit is contained in:
Andvari 2025-06-10 23:35:50 +08:00 committed by GitHub
parent 5458b06a85
commit 4c7840f6f7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 27 additions and 46 deletions

View File

@ -1,9 +1,9 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';
import { getFullText } from './utils';
export const route: Route = {
path: '/:id?',
@ -53,29 +53,7 @@ async function handler(ctx) {
pubDate: timezone(parseDate(item.CreateTime), +8),
}));
const items = await Promise.all(
list.map((item) =>
cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = load(detailResponse.data);
content('div.SubscriptionInner').remove();
const topImage = content('.fullPic').html();
item.description = (topImage === null ? '' : topImage) + content('.paragraph').eq(0).html();
item.category = [
...content("meta[property='article:tag']")
.toArray()
.map((e) => e.attribs.content),
content('.active > a').text(),
];
return item;
})
)
);
const items = await Promise.all(list.map((item) => cache.tryGet(item.link, async () => await getFullText(item))));
return {
title: metadata.Title,

23
lib/routes/cna/utils.ts Normal file
View File

@ -0,0 +1,23 @@
import got from '@/utils/got';
import { load } from 'cheerio';
export async function getFullText(item) {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = load(detailResponse.data);
content('div.SubscriptionInner').remove();
content('.gmailNews').remove();
const topImage = content('.fullPic').html();
item.description = (topImage === null ? '' : topImage) + content('.paragraph').eq(0).html();
item.category = [
...content("meta[property='article:tag']")
.toArray()
.map((e) => e.attribs.content),
content('.active > a').text(),
];
return item;
}

View File

@ -4,6 +4,7 @@ import got from '@/utils/got';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';
import { getFullText } from '../utils';
export const route: Route = {
path: '/web/:id?',
@ -45,28 +46,7 @@ async function handler(ctx) {
};
});
const items = await Promise.all(
list.map((item) =>
cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = load(detailResponse.data);
const topImage = content('.fullPic').html();
item.description = (topImage === null ? '' : topImage) + content('.paragraph').eq(0).html();
item.category = [
...content("meta[property='article:tag']")
.toArray()
.map((e) => e.attribs.content),
content('.active > a').text(),
];
return item;
})
)
);
const items = await Promise.all(list.map((item) => cache.tryGet(item.link, async () => await getFullText(item))));
return {
title: $('title').text(),