fix(route): ap news filter (#10872)

This commit is contained in:
Tony 2022-09-23 21:37:08 +08:00 committed by GitHub
parent 0f2dd302c5
commit 9b9f0c4ad0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 18 deletions

View File

@ -7,8 +7,8 @@ const HOME_PAGE = 'https://apnews.com';
module.exports = async (ctx) => {
const { topic = 'trending-news' } = ctx.params;
const urlLink = `${HOME_PAGE}/hub/${topic}`;
const response = await got(urlLink);
const url = `${HOME_PAGE}/hub/${topic}`;
const response = await got(url);
const $ = cheerio.load(response.data);
const data = JSON.parse(
$('script')
@ -18,26 +18,28 @@ module.exports = async (ctx) => {
);
const meta = data.hub.data[`/${topic}`];
const items = meta.cards.map((item) => {
const description = cheerio.load(item.contents[0].storyHTML, null, false);
description('.ad-placeholder').remove();
return {
title: item.contents[0].headline,
description: art(path.join(__dirname, 'templates/description.art'), {
media: item.contents[0].media,
description: description.html(),
}),
link: `${HOME_PAGE}/article/${item.contents[0].canonicalUrl}-${item.contents[0].shortId}`,
author: item.contents[0].bylines ? item.contents[0].bylines.slice(3) : null,
pubDate: parseDate(item.contents[0].published),
category: item.contents[0].tagObjs.map((tag) => tag.name),
};
});
const items = meta.cards
.filter((c) => c.id.startsWith('urn:publicid:ap.org:'))
.map((item) => {
const description = cheerio.load(item.contents[0].storyHTML, null, false);
description('.ad-placeholder').remove();
return {
title: item.contents[0].headline,
description: art(path.join(__dirname, 'templates/description.art'), {
media: item.contents[0].media,
description: description.html(),
}),
link: `${HOME_PAGE}/article/${item.contents[0].canonicalUrl}-${item.contents[0].shortId}`,
author: item.contents[0].bylines ? item.contents[0].bylines.slice(3) : null,
pubDate: parseDate(item.contents[0].published),
category: item.contents[0].tagObjs.map((tag) => tag.name),
};
});
ctx.state.data = {
title: meta.tagObjs[0].seoTitle,
description: meta.tagObjs[0].seoDescription,
link: urlLink,
link: url,
item: items,
language: $('html').attr('lang'),
};