feat: 为古诗文网添加翻译、注释和赏析 (#5315)

添加 annotation 字段,用来指定是否添加翻译、注释和赏析到输出的文章中。
This commit is contained in:
Bin Wang 2020-07-31 19:33:31 -04:00 committed by GitHub
parent 1110f3e1a2
commit 7553a06a3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 18 deletions

View File

@ -306,7 +306,13 @@ type 为 all 时category 参数不支持 cost 和 free
### 首页推荐
<Route author="LogicJake" example="/gushiwen/recommend" path="/gushiwen/recommend"/>
<Route author="LogicJake" example="/gushiwen/recommend/zhushang" path="/gushiwen/recommend/:annotation?"/>
`annotation` 字段为添加哪些附加信息。可从以下表格中选择值后按顺序拼接。例如如果需要注释和赏析,则为`zhushang`。
| 翻译 | 注释 | 赏析 |
| ---- | ---- | ----- |
| yi | zhu | shang |
## 国家留学网

View File

@ -1498,7 +1498,7 @@ router.get('/mobdata/report', require('./routes/mobdata/report'));
router.get('/tencent/guyu/channel/:name', require('./routes/tencent/guyu/channel'));
// 古诗文网
router.get('/gushiwen/recommend', require('./routes/gushiwen/recommend'));
router.get('/gushiwen/recommend/:annotation?', require('./routes/gushiwen/recommend'));
// 电商在线
router.get('/imaijia/category/:category', require('./routes/imaijia/category'));

View File

@ -1,29 +1,46 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
const idPattern = /shiwenv_(\w+).aspx/;
async function getContent(link, annotation) {
const id = link.match(idPattern)[1];
const url = `https://www.gushiwen.cn/nocdn/ajaxshiwencont.aspx?id=${id}&value=${annotation}`;
const res = await got.get(url, { headers: { cookie: 'login=flase' } });
return res.data;
}
module.exports = async (ctx) => {
const url = 'https://www.gushiwen.org/';
const res = await got.get(url);
const annotation = ctx.params.annotation;
const $ = cheerio.load(res.data);
const list = $('div.sons');
const out = list
.slice(0, 10)
.map(function () {
const title = $(this).find('b').text();
const link = $(this).find('p a').attr('href');
const description = $(this).find('div.contson').html();
const author = $(this).find('p.source').text();
const item = {
title,
link,
description,
author,
};
const out = await Promise.all(
list
.slice(0, 10)
.map(async function () {
const title = $(this).find('b').text();
const link = $(this).find('p a').attr('href');
return item;
})
.get();
let description = $(this).find('div.contson').html();
if (annotation && link) {
description = await getContent(link, annotation);
}
const author = $(this).find('p.source').text();
const item = {
title,
link,
description,
author,
};
return item;
})
.get()
);
ctx.state.data = {
title: '古诗文推荐',