feat(route): add Reuters Graphics (#12748)

This commit is contained in:
Ethan Shen
2023-07-04 00:39:46 +08:00
committed by GitHub
parent 18b06b9c04
commit 413a959cb3
4 changed files with 44 additions and 27 deletions
+4 -4
View File
@@ -390,12 +390,12 @@ Parameters can be obtained from the official website, for instance:
### Category/Topic/Author
<RouteEn author="HenryQW proletarius101 LyleLee" example="/reuters/world/us" path="/reuters/:category/:topic?" :paramsDesc="['find it in the URL, or tables below', 'find it in the URL, or tables below']">
<RouteEn author="HenryQW proletarius101 LyleLee nczitzk" example="/reuters/world/us" path="/reuters/:category/:topic?" :paramsDesc="['find it in the URL, or tables below', 'find it in the URL, or tables below']">
- `:category`:
| World | Business | Legal | Markets | Breakingviews | Technology |
| -------- | ------- | ----- | -------- | ------------- | ---------- |
| world | business | legal | markets | breakingviews | technology |
| World | Business | Legal | Markets | Breakingviews | Technology | Graphics |
| ----- | -------- | ----- | ------- | ------------- | ---------- | -------- |
| world | business | legal | markets | breakingviews | technology | graphics |
- `world/:topic`:
+4 -4
View File
@@ -1524,12 +1524,12 @@ category 对应的关键词有
### 分类 / 话题 / 作者
<Route author="HenryQW proletarius101 LyleLee" example="/reuters/world/us" path="/reuters/:category/:topic?" :paramsDesc="['可在 URL 中找到,或参考下面的表格', '可在 URL 中找到,或参考下面的表格']">
<Route author="HenryQW proletarius101 LyleLee nczitzk" example="/reuters/world/us" path="/reuters/:category/:topic?" :paramsDesc="['可在 URL 中找到,或参考下面的表格', '可在 URL 中找到,或参考下面的表格']">
- `:category`:
| World | Business | Legal | Markets | Breakingviews | Technology |
| ----- | -------- | ----- | ------- | ------------- | ---------- |
| world | business | legal | markets | breakingviews | technology |
| World | Business | Legal | Markets | Breakingviews | Technology | Graphics |
| ----- | -------- | ----- | ------- | ------------- | ---------- | -------- |
| world | business | legal | markets | breakingviews | technology | graphics |
- `world/:topic`:
+35 -18
View File
@@ -7,33 +7,35 @@ const path = require('path');
module.exports = async (ctx) => {
const category = ctx.params.category;
const topic = ctx.params.topic ?? (category === 'authors' ? 'reuters' : '');
const limit = ctx.query.limit ? parseInt(ctx.query.limit) : 20;
const rootUrl = 'https://www.reuters.com';
const currentUrl = topic ? `${rootUrl}/${category}/${topic}/` : `${rootUrl}/${category}/`;
const response = await got(currentUrl);
const $ = cheerio.load(response.data);
let list = $('.media-story-card__body__3tRWy a.media-story-card__heading__eqhp9')
let items = $('.media-story-card__body__3tRWy a.media-story-card__heading__eqhp9, a.svelte-pxbp38, a.svelte-11dknnx, a.svelte-e21rsn')
.slice(0, limit)
.toArray()
.map((item) => {
item = $(item);
item.find('span.visually-hidden__hidden__2qXMW').remove();
return {
title: item.text(),
link: rootUrl + item.prop('href'),
link: new URL(item.prop('href'), rootUrl).href,
};
});
if (!list.length) {
if (!items.length) {
const metadata = $('script#fusion-metadata').html();
const metadataObj = JSON.parse(metadata.match(/Fusion.globalContent=(\{[\s\S]*?});/)[1]);
const articles = metadataObj.arcResult?.articles ?? metadataObj.result?.articles ?? [];
list = articles.map((article) => ({
items = articles.map((article) => ({
title: article.title,
link: rootUrl + article.canonical_url,
}));
}
const items = await Promise.all(
list.map((item) =>
items = await Promise.all(
items.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got(item.link);
const content = cheerio.load(detailResponse.data);
@@ -51,19 +53,34 @@ module.exports = async (ctx) => {
return item;
}
const data = JSON.parse(
content('script#fusion-metadata')
.text()
.match(/Fusion.globalContent=(\{[\s\S]*?});/)[1]
);
const matches = content('script#fusion-metadata')
.text()
.match(/Fusion.globalContent=(\{[\s\S]*?});/);
item.title = data.result.title || item.title;
item.description = art(path.join(__dirname, 'templates/description.art'), {
result: data.result,
});
item.pubDate = parseDate(data.result.display_time);
item.author = data.result.authors.map((author) => author.name).join(', ');
item.category = data.result.taxonomy.keywords;
if (matches) {
const data = JSON.parse(matches[1]);
item.title = data.result.title || item.title;
item.description = art(path.join(__dirname, 'templates/description.art'), {
result: data.result,
});
item.pubDate = parseDate(data.result.display_time);
item.author = data.result.authors.map((author) => author.name).join(', ');
item.category = data.result.taxonomy.keywords;
return item;
}
content('.title').remove();
content('.article-metadata').remove();
item.title = content('meta[property="og:title"]').attr('content');
item.pubDate = parseDate(detailResponse.data.match(/"datePublished":"(.*?)","dateModified/)[1]);
item.author = detailResponse.data
.match(/{"@type":"Person","name":"(.*?)"}/g)
.map((p) => p.match(/"name":"(.*?)"/)[1])
.join(', ');
item.description = content('article').html();
return item;
})
+1 -1
View File
@@ -2,5 +2,5 @@ module.exports = {
'/investigates': ['LyleLee'],
'/reuters/channel/:site/:channel': ['LyleLee'], // deprecated
'/reuters/theWire': ['LyleLee'], // deprecated
'/:category/:topic?': ['LyleLee', 'HenryQW', 'proletarius101', 'black-desk'],
'/:category/:topic?': ['LyleLee', 'HenryQW', 'proletarius101', 'black-desk', 'nczitzk'],
};