fix(route): AP News获取方式调整 (#12812)

* Resort to DOM parsing.

* Update topics.js

* Update topics.js

* Remove unused code.

* Fix cache error.

* Remove "Other News".

* Update lib/v2/apnews/topics.js

---------
This commit is contained in:
Andvari 2023-07-17 23:35:47 +08:00 committed by GitHub
parent 220c82cba2
commit bd2f00fea4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 30 deletions

View File

@ -1,9 +1,5 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const timezone = require('@/utils/timezone');
const { art } = require('@/utils/render');
const path = require('path');
const HOME_PAGE = 'https://apnews.com';
module.exports = async (ctx) => {
@ -11,35 +7,33 @@ module.exports = async (ctx) => {
const url = `${HOME_PAGE}/hub/${topic}`;
const response = await got(url);
const $ = cheerio.load(response.data);
const data = JSON.parse(
$('script')
.not('[src], [type]')
.text()
.match(/window\['titanium-state'\] = (.*)\nwindow\['titanium-cacheConfig'\]/)[1]
);
const meta = data.hub.data[`/${topic}`];
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: timezone(parseDate(item.contents[0].published), 0),
category: item.contents[0].tagObjs.map((tag) => tag.name),
};
});
const items = await Promise.all(
$('.PagePromo-content')
.get()
.map((e) => ({
title: $(e).find('span.PagePromoContentIcons-text').text(),
link: $(e).find('a').attr('href'),
}))
.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const { data: response } = await got(item.link);
const $ = cheerio.load(response);
$('div.Enhancement').remove();
return Object.assign(item, {
pubDate: new Date($("meta[property='article:published_time']").attr('content')),
updated: new Date($("meta[property='article:modified_time']").attr('content')),
description: $('div.RichTextStoryBody').html(),
category: $("meta[property='article:section']").attr('content'),
guid: $("meta[name='brightspot.contentId']").attr('content'),
});
})
)
);
ctx.state.data = {
title: meta.tagObjs[0].seoTitle,
description: meta.tagObjs[0].seoDescription,
title: $('title').text(),
description: $("meta[property='og:description']").text(),
link: url,
item: items,
language: $('html').attr('lang'),