fix: zcool支持用户id和域名前缀 (#3289)

* fix zcool user

* update docs
This commit is contained in:
hoilc
2019-10-18 19:18:15 +08:00
committed by DIYgod
parent 11b3c0ba4d
commit bd74aa8db5
3 changed files with 44 additions and 41 deletions
+6 -2
View File
@@ -48,8 +48,12 @@ pageClass: routes
### 用户作品
<Route author="junbaor" example="/zcool/user/baiyong" path="/zcool/user/:uname" :paramsDesc="['个性域名前缀']">
<Route author="junbaor" example="/zcool/user/baiyong" path="/zcool/user/:uid" :paramsDesc="['个性域名前缀或者用户ID']">
例如: 站酷的个人主页 `https://baiyong.zcool.com.cn` 对应 rss 路径 `/zcool/user/baiyong`
例如:
站酷的个人主页 `https://baiyong.zcool.com.cn` 对应 rss 路径 `/zcool/user/baiyong`
站酷的个人主页 `https://www.zcool.com.cn/u/568339` 对应 rss 路径 `/zcool/user/568339`
</Route>
+1 -1
View File
@@ -1630,7 +1630,7 @@ router.get('/10000link/news/:category?', require('./routes/10000link/news'));
// 站酷
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('/zcool/user/:uid', require('./routes/zcool/user'));
// 第一财经
router.get('/yicai/brief', require('./routes/yicai/brief.js'));
+37 -38
View File
@@ -2,58 +2,57 @@ const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const uname = ctx.params.uname;
let uid = ctx.params.uid;
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();
if (isNaN(uid)) {
const user_response = await got.get(`https://${uid}.zcool.com.cn/`);
const $ = cheerio.load(user_response.data);
uid = $('[data-id]').attr('data-id');
}
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 url = `https://api.zcool.com.cn/v2/api/u/${uid}`;
const result = await Promise.all(
const response = await got.get(url);
const data = response.data.data;
const user = data.content[0].creatorObj;
const list = data.content;
const out = await Promise.all(
list.map(async (item) => {
const link = item.link;
const link = item.pageUrl;
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();
const rssitem = {
title: item.title,
link: link,
author: item.creatorObj.username,
pubDate: new Date(item.createTime * 1),
};
ctx.cache.set(link, JSON.stringify(item));
return Promise.resolve(item);
try {
const item_response = await got.get(link);
const item_element = cheerio.load(item_response.data);
const item_content_wrap = item_element('.work-content-wrap');
const item_content = item_content_wrap.length !== 0 ? item_content_wrap : item_element('.work-show-box');
rssitem.description = item_content.html();
} catch (err) {
return Promise.resolve('');
}
ctx.cache.set(link, JSON.stringify(rssitem));
return Promise.resolve(rssitem);
})
);
ctx.state.data = {
title: '站酷 - ' + author,
link: url,
item: result.reverse(),
title: '站酷 - ' + user.username,
image: user.avatar,
link: user.pageUrl,
item: out.filter((item) => item !== ''),
};
};