From 6cf60039086de52bb2f3c92c582a60a3846b2e4f Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Wed, 21 Oct 2020 23:20:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E7=AB=99=E9=85=B7=E5=8F=91?= =?UTF-8?q?=E7=8E=B0=20(#5960)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/design.md | 10 ++++++++ lib/router.js | 1 + lib/routes/zcool/discovery.js | 48 +++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 lib/routes/zcool/discovery.js diff --git a/docs/design.md b/docs/design.md index ff4cc2204..c125f2096 100644 --- a/docs/design.md +++ b/docs/design.md @@ -159,3 +159,13 @@ Behance 用户主页 URL 获取用户名,如 + +### 发现 + + + +在 [发现页](https://www.zcool.com.cn/discover) 中选择查询参数后会跳转到对应搜索结果页面。此时地址栏 `https://www.zcool.com.cn/discover/` 后的字段即为查询参数。 + +如: 查询参数为 `0!0!0!0!0!!!!2!-1!1` + + diff --git a/lib/router.js b/lib/router.js index 72dda85b4..fe5f0d7d9 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1913,6 +1913,7 @@ router.get('/zcool/recommend/:type', require('./routes/zcool/recommend')); router.get('/zcool/top/:type', require('./routes/zcool/top')); router.get('/zcool/top', require('./routes/zcool/top')); // 兼容老版本 router.get('/zcool/user/:uid', require('./routes/zcool/user')); +router.get('/zcool/discovery/:query?', require('./routes/zcool/discovery')); // 第一财经 router.get('/yicai/brief', require('./routes/yicai/brief.js')); diff --git a/lib/routes/zcool/discovery.js b/lib/routes/zcool/discovery.js new file mode 100644 index 000000000..f0f05fb55 --- /dev/null +++ b/lib/routes/zcool/discovery.js @@ -0,0 +1,48 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const query = ctx.params.query || '0!0!0!0!0!!!!2!-1!1'; + + const rootUrl = `https://www.zcool.com.cn/discover/${query}`; + const response = await got({ + method: 'get', + url: rootUrl, + }); + const $ = cheerio.load(response.data); + + const list = $('.work-list-box > .card-box') + .map((_, item) => { + item = $(item); + return { + link: item.find('.card-info-title a[title]').attr('href'), + author: item.find('.user-avatar').find('a').attr('title'), + }; + }) + .get(); + + const items = await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + const content = cheerio.load(detailResponse.data); + + item.title = content('h2').text(); + item.pubDate = new Date(content('.title-time').attr('title').split('创建时间:')[1]).toUTCString(); + item.description = item.link.indexOf('article') < 0 ? content('.work-content-wrap').html() : content('.article-content-wraper').html(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: `站酷 - ${query}`, + link: rootUrl, + item: items, + }; +};