From 7553a06a3dd5a8ef03989d6f8800bc6c6d0896b0 Mon Sep 17 00:00:00 2001 From: Bin Wang Date: Fri, 31 Jul 2020 19:33:31 -0400 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=BA=E5=8F=A4=E8=AF=97=E6=96=87?= =?UTF-8?q?=E7=BD=91=E6=B7=BB=E5=8A=A0=E7=BF=BB=E8=AF=91=E3=80=81=E6=B3=A8?= =?UTF-8?q?=E9=87=8A=E5=92=8C=E8=B5=8F=E6=9E=90=20(#5315)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 annotation 字段,用来指定是否添加翻译、注释和赏析到输出的文章中。 --- docs/other.md | 8 +++++- lib/router.js | 2 +- lib/routes/gushiwen/recommend.js | 49 +++++++++++++++++++++----------- 3 files changed, 41 insertions(+), 18 deletions(-) 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: '古诗文推荐',