feat: add 站酷发现 (#5960)

This commit is contained in:
Ethan Shen
2020-10-21 16:20:24 +01:00
committed by GitHub
parent f3fcb80426
commit 6cf6003908
3 changed files with 59 additions and 0 deletions
+10
View File
@@ -159,3 +159,13 @@ Behance 用户主页 URL 获取用户名,如 <https://www.behance.net/mishapet
站酷的个人主页 `https://www.zcool.com.cn/u/568339` 对应 rss 路径 `/zcool/user/568339`
</Route>
### 发现
<Route author="nczitzk" example="/zcool/discovery" path="/zcool/discovery/:query?" :paramsDesc="['查询参数']" radar="1" rssbud="1">
在 [发现页](https://www.zcool.com.cn/discover) 中选择查询参数后会跳转到对应搜索结果页面。此时地址栏 `https://www.zcool.com.cn/discover/` 后的字段即为查询参数。
如:<https://www.zcool.com.cn/discover/0!0!0!0!0!!!!2!-1!1> 查询参数为 `0!0!0!0!0!!!!2!-1!1`
</Route>
+1
View File
@@ -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'));
+48
View File
@@ -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,
};
};