feat: 新增站酷推荐、作品总榜、用户作品页 close #2683 (#2688)

* feat: 新增站酷推荐

* feat: 新增站酷作品总榜单

* 站酷推荐 reverse

* feat: 新增站酷用户作品页

* 调整格式化格式
This commit is contained in:
junbaor
2019-07-26 11:46:50 +08:00
committed by DIYgod
parent 05c79c5139
commit eaf209bec4
5 changed files with 219 additions and 0 deletions

View File

@@ -1145,3 +1145,29 @@ type 为 all 时category 参数不支持 cost 和 free
### 全文
<Route author="HenryQW" example="/zzz" path="/zzz/index"/>
## 站酷
### 推荐
<Route author="junbaor" example="/zcool/recommend/all" path="/zcool/recommend/:type" :paramsDesc="['推荐类型,详见下面的表格']">
推荐类型
| all | home | edit |
| -------- | -------- | -------- |
| 全部推荐 | 首页推荐 | 编辑推荐 |
</Route>
### 作品总榜单
<Route author="junbaor" example="/zcool/top" path="/zcool/top"/>
### 用户作品
<Route author="junbaor" example="/zcool/user/baiyong" path="/zcool/user/:uname" :paramsDesc="['个性域名前缀']">
例如: 站酷的个人主页 `https://baiyong.zcool.com.cn` 对应 rss 路径 `/zcool/user/baiyong`
</Route>

View File

@@ -1562,6 +1562,11 @@ router.get('/10000link/news/:category?', require('./routes/10000link/news'));
// Artand
router.get('/artand/user/work/:uid', require('./routes/artand/user/work'));
// 站酷
router.get('/zcool/recommend/:type', require('./routes/zcool/recommend'));
router.get('/zcool/top', require('./routes/zcool/top'));
router.get('/zcool/user/:uname', require('./routes/zcool/user'));
// 第一财经
router.get('/yicai/brief', require('./routes/yicai/brief.js'));

View File

@@ -0,0 +1,71 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
let typeUrl = '0!0!0!0!0!!!!1!-1!1';
let typeName = '全部推荐';
const type = ctx.params.type;
if (type === 'home') {
typeUrl = '0!0!0!0!0!!!!3!-1!1';
typeName = '首页推荐';
} else if (type === 'edit') {
typeUrl = '0!0!0!0!0!!!!2!-1!1';
typeName = '编辑推荐';
}
const url = 'https://www.zcool.com.cn/discover/' + typeUrl;
const response = await got({ method: 'get', url });
const $ = cheerio.load(response.data);
const list = $('.work-list-box > .card-box')
.map((i, e) => {
const element = $(e);
const title = element
.find('.card-info-title')
.find('a')
.attr('title')
.trim();
const link = element
.find('.card-info-title')
.find('a')
.attr('href')
.trim();
const author = element
.find('.user-avatar')
.find('a')
.attr('title');
return {
title: title,
description: '',
link: link,
author: author,
};
})
.get();
const result = await Promise.all(
list.map(async (item) => {
const link = item.link;
const cache = await ctx.cache.get(link);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const itemReponse = await got.get(link);
const itemElement = cheerio.load(itemReponse.data);
item.description = itemElement('.work-content-wrap').html();
ctx.cache.set(link, JSON.stringify(item));
return Promise.resolve(item);
})
);
ctx.state.data = {
title: '站酷 - ' + typeName,
link: url,
item: result.reverse(),
};
};

58
lib/routes/zcool/top.js Normal file
View File

@@ -0,0 +1,58 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const url = 'https://www.zcool.com.cn/top/index.do';
const response = await got({ method: 'get', url });
const $ = cheerio.load(response.data);
const list = $('.author')
.map((i, e) => {
const element = $(e);
const title = element
.find('.title')
.find('a')
.text();
const link = element
.find('.title')
.find('a')
.attr('href');
const author = element
.find('.nick')
.find('a')
.text();
return {
title: title,
description: '',
link: link,
author: author,
};
})
.get();
const result = await Promise.all(
list.map(async (item) => {
const link = item.link;
const cache = await ctx.cache.get(link);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const itemReponse = await got.get(link);
const itemElement = cheerio.load(itemReponse.data);
item.description = itemElement('.work-content-wrap').html();
ctx.cache.set(link, JSON.stringify(item));
return Promise.resolve(item);
})
);
ctx.state.data = {
title: '站酷 - 作品总榜单',
link: url,
item: result.reverse(),
};
};

59
lib/routes/zcool/user.js Normal file
View File

@@ -0,0 +1,59 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const uname = ctx.params.uname;
const url = 'https://' + uname + '.zcool.com.cn';
const response = await got({ method: 'get', url });
const $ = cheerio.load(response.data);
const author = $('.people-nick-name')
.text()
.trim();
const list = $('.work-list-box > .card-box')
.map((i, e) => {
const element = $(e);
const title = element
.find('.card-info-title')
.find('a')
.attr('title')
.trim();
const link = element
.find('.card-info-title')
.find('a')
.attr('href')
.trim();
return {
title: title,
description: '',
link: link,
author: author,
};
})
.get();
const result = await Promise.all(
list.map(async (item) => {
const link = item.link;
const cache = await ctx.cache.get(link);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const itemReponse = await got.get(link);
const itemElement = cheerio.load(itemReponse.data);
item.description = itemElement('.work-content-wrap').html();
ctx.cache.set(link, JSON.stringify(item));
return Promise.resolve(item);
})
);
ctx.state.data = {
title: '站酷 - ' + author,
link: url,
item: result.reverse(),
};
};