diff --git a/docs/other.md b/docs/other.md index 5fe74634f..f3fc4ac2f 100644 --- a/docs/other.md +++ b/docs/other.md @@ -306,7 +306,13 @@ type 为 all 时,category 参数不支持 cost 和 free ### 首页推荐 - + + +`annotation` 字段为添加哪些附加信息。可从以下表格中选择值后按顺序拼接。例如如果需要注释和赏析,则为`zhushang`。 + +| 翻译 | 注释 | 赏析 | +| ---- | ---- | ----- | +| yi | zhu | shang | ## 国家留学网 diff --git a/lib/router.js b/lib/router.js index 3e9601c1a..a9ae0ff51 100644 --- a/lib/router.js +++ b/lib/router.js @@ -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')); diff --git a/lib/routes/gushiwen/recommend.js b/lib/routes/gushiwen/recommend.js index e48d3905c..d43e69673 100644 --- a/lib/routes/gushiwen/recommend.js +++ b/lib/routes/gushiwen/recommend.js @@ -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: '古诗文推荐',