fix(route): The New York Times 新闻简报 (#10012)

This commit is contained in:
Ethan Shen
2022-06-23 00:24:07 +08:00
committed by GitHub
parent 4cf6fa4f3f
commit d32803b147
4 changed files with 71 additions and 42 deletions
+2 -2
View File
@@ -1358,9 +1358,9 @@ category 对应的关键词有
### 新闻简报
<Route author="yueyericardo" example="/nytimes/daily_briefing_chinese" path="/nytimes/daily_briefing_chinese">
<Route author="yueyericardo nczitzk" example="/nytimes/daily_briefing_chinese" path="/nytimes/daily_briefing_chinese">
网站地址:<https://www.nytimes.com/zh-hans/series/daily-briefing-chinese/>
网站地址:<https://www.nytimes.com/zh-hans/series/daily-briefing-chinese>
</Route>
+64 -39
View File
@@ -1,61 +1,86 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const { art } = require('@/utils/render');
const path = require('path');
module.exports = async (ctx) => {
const url = 'https://www.nytimes.com/zh-hans/series/daily-briefing-chinese';
const rootUrl = 'https://www.nytimes.com';
const currentUrl = `${rootUrl}/zh-hans/series/daily-briefing-chinese`;
const response = await got({
method: 'get',
url,
url: currentUrl,
});
const data = response.data;
const $ = cheerio.load(data);
const post = $('.css-13mho3u .css-ye6x8s')
.map((index, elem) => {
const $item = $(elem);
const $link = $item.find('a');
const $title = $item.find('h2');
return {
title: $title.text(),
link: 'https://www.nytimes.com' + $link.attr('href'),
};
})
.get();
const listData = JSON.parse(response.data.match(/<script>window\.__preloadedData = {"initialState":(.*),"config"/)[1]);
const items = await Promise.all(
post.map((item) =>
let items = [];
Object.keys(listData).forEach((key) => {
if (/^Article:/.test(key) && listData[key].url) {
const item = listData[key];
items.push({
link: item.url,
pubDate: parseDate(item.firstPublished),
});
}
});
items = await Promise.all(
items.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const response = await got.get(item.link);
const data = response.data;
const arr = data.match(/"url":"https:\\u002F\\u002Fstatic(\S*)"articleLarge"/g);
const imageLink = arr.map((e) => JSON.parse(`{${e}}`).url);
const $ = cheerio.load(data);
$('.css-pncxxs.etfikam0').remove();
// remove 'Credit...'
$('.css-1ly73wi.e1tej78p0').remove();
$('.css-1xdhyk6.erfvjey0').each((i, e) => $(e).replaceWith(`<img src="${imageLink[i]}">`));
$('.css-1l44abu.ewdxa0s0').each((i, e) => {
const figureNote = $(e)
.children()
.map((i, e) => $(e).text())
.get();
$(e).replaceWith(`<p><small>${figureNote.join(' ')}</small><p>`);
const detailResponse = await got({
method: 'get',
url: item.link,
});
const date = $('time').attr('datetime');
item.description = $('.meteredContent.css-1r7ky0e').html();
item.pubDate = parseDate(date);
const content = cheerio.load(detailResponse.data);
const itemData = JSON.parse(detailResponse.data.match(/<script>window\.__preloadedData = {"initialState":(.*),"config"/)[1]);
// to remove ads
content('.StoryBodyCompanionColumn').last().find('p').last().remove();
const images = [];
Object.keys(itemData).forEach((key) => {
if (/^Image:/.test(key) && itemData[key].__typename === 'Image') {
let cropKey = '';
const image = itemData[key];
Object.keys(image).forEach((c) => {
if (/^crops/.test(c)) {
cropKey = c;
return;
}
});
images.push({
caption: image.legacyHtmlCaption,
url: itemData[itemData[image[cropKey][0].id].renditions[0].id].url,
});
}
});
let i = 0;
content('figure').each(function () {
content(this).html(
art(path.join(__dirname, 'templates/image.art'), {
url: images[i].url,
caption: images[i++].caption,
})
);
});
item.title = content('meta[property="og:title"]').attr('content');
item.author = content('meta[name="byl"]').attr('content').replace(/By /, '');
item.description = content('section[name="articleBody"]').html();
return item;
})
)
);
ctx.state.data = {
title: '纽约时报中文网|新闻简报',
link: url,
title: '新闻简报 - The New York Times',
link: currentUrl,
item: items,
};
};
+1 -1
View File
@@ -1,6 +1,6 @@
module.exports = {
'/author/:byline': ['kevinschaul'],
'/book/:category?': ['melvinto'],
'/daily_briefing_chinese': ['yueyericardo'],
'/daily_briefing_chinese': ['yueyericardo', 'nczitzk'],
'/:lang?': ['HenryQW'],
};
+4
View File
@@ -0,0 +1,4 @@
<figure>
<img src="{{ url }}">
<figcaption>{{ caption }}</figcaption>
</figure>