feat(route): 商论 (#11659)

* feat(route): 商论

* fix: apply suggestions from code review

* fix: remove language.sort
This commit is contained in:
papersnake 2023-01-20 23:44:48 +08:00 committed by GitHub
parent ddf82784ec
commit 0d7f42e483
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 107 additions and 0 deletions

View File

@ -588,6 +588,10 @@ Language
<RouteEn author="xyqfer" example="/economist/gre-vocabulary" path="/economist/gre-vocabulary" />
### Global Business Review
<Route author="prnake" example="/economist/global-business-review/cn-en" path="/economist/global-business-review/:language?" :paramsDesc="['Language, `en`, `cn`, `tw` are supported, support multiple options, default to cn-en']" radar="1" rssbud="1"/>
### Download
<RouteEn author="nczitzk" example="/economist/download" path="/economist/download" >

View File

@ -494,6 +494,10 @@ Solidot 提供的 feed:
<Route author="xyqfer" example="/economist/gre-vocabulary" path="/economist/gre-vocabulary" radar="1" rssbud="1"/>
### 商论
<Route author="prnake" example="/economist/global-business-review/cn-en" path="/economist/global-business-review/:language?" :paramsDesc="['语言,支持简体 cn、繁体 tw、英文 en ,可选择多个语言,默认为 cn-en']" radar="1" rssbud="1"/>
### 下载
<Route author="nczitzk" example="/economist/download" path="/economist/download" >

View File

@ -0,0 +1,86 @@
const got = require('@/utils/got');
const ALLOW_LANGUAGE = {
en: 'en_GB',
cn: 'zh_CN',
tw: 'zh_TW',
};
const TITLE = {
en: 'The Economist Global Business Review',
cn: '经济学人 · 商论',
tw: '經濟學人 · 商論',
};
const DESCRIPTION = {
en: 'The Economist Global Business Review is a new bilingual digital app from the editors of The Economist Group.',
cn: '《经济学人·商论》是《经济学人》2015年5月推出的旗下中英双语APP萃取《经济学人》在商业、金融、科技等领域的精华文章为中国读者呈现全球视角的深度分析并鼓励中国的读者批判性地思考中国和全球重大议题。',
tw: '《經濟學人·商論》是經濟學人集團官方中英雙語電子APP萃取《經濟學人》在商業、金融、科技等領域的精華文章為中國讀者呈現全球視角的深度分析並鼓勵中國的讀者批判性地思考中國和全球的重大議題。',
};
const parseContent = function (item, article_id, language) {
if (item.type === 'paragraph') {
return parseParagraph(item.data, language);
} else if (item.type === 'image') {
return parseImage(item.data, article_id, language);
} else if (item.type === 'subtitle') {
return parseParagraph(item.data, language, 'h3');
}
};
const parseTitle = function (data, language) {
const content = Object.assign({}, ...data.map((x) => ({ [x.lang]: x.text })));
return language.map((item) => content[ALLOW_LANGUAGE[item]]).join('');
};
const parseParagraph = function (data, language, type = 'p') {
const content = Object.assign({}, ...data.map((x) => ({ [x.lang]: x.text })));
return `<div>${language.map((item) => `<${type}>${content[ALLOW_LANGUAGE[item]]}</${type}>`).join('')}</div>`;
};
const parseImage = function (data, article_id, language) {
const content = Object.assign({}, ...data.map((x) => ({ [x.lang]: x.image_path })));
return `<div><img src="https://businessreviewglobal-cdn.com/article_images/${article_id}/${encodeURIComponent(content[ALLOW_LANGUAGE[language[0]]])}"/></div>`;
};
const getArticleDetail = (article_id, language, ctx) => {
const link = `https://api.hummingbird.businessreview.global/api/article/index?id=${article_id}`;
return ctx.cache.tryGet(`${link}:${language.join('-')}`, async () => {
const response = await got({
method: 'get',
url: link,
});
const data = response.data.body;
let content = '';
content += parseParagraph(data.rubric, language);
content += data.content.map((item) => parseContent(item, article_id, language)).join('');
return content;
});
};
module.exports = async (ctx) => {
const language = (ctx.params.language && ctx.params.language.split('-').filter((item, index, arr) => ALLOW_LANGUAGE[item] && arr.indexOf(item, 0) === index)) || ['cn', 'en'];
const main_language = language[0];
const response = await got({
method: 'get',
url: 'https://api.hummingbird.businessreview.global/api/toc/get_articles',
});
const items = await Promise.all(
response.data.articles.new.slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 10).map(async (item) => ({
title: parseTitle(item.body.title, [main_language]),
description: await getArticleDetail(item.article_id, language, ctx),
category: parseTitle(item.body.fly_title, [main_language]),
link: `https://www.businessreview.global/latest/${item.article_id}`,
pubDate: item.publication_date,
}))
);
ctx.state.data = {
title: TITLE[main_language],
link: 'https://www.businessreview.global',
description: DESCRIPTION[main_language],
item: items,
};
};

View File

@ -2,5 +2,6 @@ module.exports = {
'/download': ['nczitzk'],
'/espresso': ['TonyRL'],
'/gre-vocabulary': ['xyqfer'],
'/global-business-review/:language?': ['prnake'],
'/:endpoint': ['ImSingee'],
};

View File

@ -24,4 +24,15 @@ module.exports = {
},
],
},
'businessreview.global': {
_name: 'The Economist',
'.': [
{
title: '商论',
docs: 'https://docs.rsshub.app/traditional-media.html#the-economist',
source: ['/'],
target: '/economist/global-business-review',
},
],
},
};

View File

@ -2,5 +2,6 @@ module.exports = (router) => {
router.get('/download', require('./download'));
router.get('/espresso', require('./espresso'));
router.get('/gre-vocabulary', require('./gre-vocabulary'));
router.get('/global-business-review/:language?', require('./global-business-review'));
router.get('/:endpoint', require('./full'));
};