Feature/weibo oauth (#3299)

Co-authored-by: zytomorrow <z794672847@gmail.com>
This commit is contained in:
DIYgod
2019-11-04 17:36:31 +08:00
committed by GitHub
co-authored by zytomorrow
parent ec1bc57e20
commit 6181b38837
5 changed files with 106 additions and 0 deletions
+6
View File
@@ -419,3 +419,9 @@ RSSHub 支持 `memory` 和 `redis` 两种缓存方式
- 吹牛部落 栏目更新
- `CHUINIU_MEMBER`: 吹牛部落登录后的 x-member,获取方式:1. 登陆后点开文章正文 2. 打开控制台 3. 刷新 4. 找到 <http://api.duanshu.com/h5/content/detail/> 开头的请求 5. 找到请求头中的 x-member
- 微博 个人时间线路由: [申请地址](https://open.weibo.com/connect)
- `WEIBO_APP_KEY`: 微博 App Key
- `WEIBO_APP_SECRET`: 微博 App Secret
- `WEIBO_REDIRECT_URL`: 微博登录授权回调地址,默认为 `RSSHub地址/weibo/timeline/0`,自定义回调地址请确保最后可以转跳到 `RSSHub地址/weibo/timeline/0?code=xxx`
+14
View File
@@ -576,6 +576,20 @@ pageClass: routes
<Route author="zengxs" example="/weibo/super_index/1008084989d223732bf6f02f75ea30efad58a9" path="/weibo/super_index/:id" :paramsDesc="['超话ID']" anticrawler="1" radar="1"/>
### 个人时间线
<Route author="zytomorrow DIYgod" example="/weibo/timeline/3306934123" path="/weibo/timeline/:uid/:feature?" :paramsDesc="['用户的uid', ' 过滤类型ID,0:全部、1:原创、2:图片、3:视频、4:音乐,默认为0。']">
::: warning 注意
需要对应用户打开页面进行授权生成 token 才能生成内容
自部署需要申请并配置微博 key,具体见部署文档
:::
</Route>
## 微博绿洲
### 用户
+5
View File
@@ -95,6 +95,11 @@ const calculateValue = () => {
chuiniu: {
member: envs.CHUINIU_MEMBER,
},
weibo: {
app_key: envs.WEIBO_APP_KEY,
app_secret: envs.WEIBO_APP_SECRET,
redirect_url: envs.WEIBO_REDIRECT_URL,
},
};
};
calculateValue();
+3
View File
@@ -1823,6 +1823,9 @@ router.get('/haimaoba/:id?', require('./routes/haimaoba/comics'));
// 蒲公英
router.get('/pgyer/:app?', require('./routes/pgyer/app'));
// 微博个人时间线
router.get('/weibo/timeline/:uid/:feature?', require('./routes/weibo/timeline'));
// TAPTAP
router.get('/taptap/topic/:id/:label?', require('./routes/taptap/topic'));
router.get('/taptap/changelog/:id', require('./routes/taptap/changelog'));
+78
View File
@@ -0,0 +1,78 @@
const got = require('@/utils/got');
const config = require('@/config').value;
module.exports = async (ctx) => {
const uid = ctx.params.uid;
const feature = ctx.params.feature || 0;
const token = ctx.cache.get('weibotimelineuid' + uid);
if (token) {
const response = await got.get(`https://api.weibo.com/2/statuses/home_timeline.json?access_token=${token}&count=100&feature=${feature}`);
const data = response.data.statuses;
const links = await Promise.all(
data.map(async (item) => {
const url = await ctx.cache.tryGet('weibotimelineurl' + item.user.id + item.id, async () => {
let result = 'https://weibo.com';
try {
await got.get(`http://api.weibo.com/2/statuses/go?access_token=${token}&id=${item.id}&uid=${item.user.id}`, {
hooks: {
beforeRedirect: [
(options) => {
options.followRedirect = false;
result = options.href;
},
],
},
});
} catch (error) {
// nothing
}
return result;
});
return Promise.resolve(url);
})
);
ctx.state.data = {
title: '个人微博时间线',
link: 'https://weibo.com',
item: data.map((item, index) => {
let description = item.text;
item.pic_urls.forEach((pic) => {
description += `<img src="${pic.thumbnail_pic}"><br>`;
});
description += item.source;
return {
description,
link: links[index],
pubDate: item.created_at,
title: `${item.user.name}: ${item.text}`,
author: item.user.name,
};
}),
};
} else if (uid === '0' || ctx.querystring) {
const { app_key = '', redirect_url = ctx.request.origin + '/weibo/timeline/0', app_secret = '' } = config.weibo;
const code = Object.values(ctx.query)[0];
if (code) {
const rep = await got.post(`https://api.weibo.com/oauth2/access_token?client_id=${app_key}&client_secret=${app_secret}&code=${code}&redirect_uri=${redirect_url}&grant_type=authorization_code`);
const token = rep.data.access_token;
const uid = rep.data.uid;
const expires_in = rep.data.expires_in;
await ctx.cache.set('weibotimelineuid' + uid, token, expires_in);
ctx.set({
'Content-Type': 'text/html; charset=UTF-8',
});
ctx.body = `<script>window.location = '/weibo/timeline/${uid}'</script>`;
}
} else {
const { app_key = '', redirect_url = ctx.request.origin + '/weibo/timeline/0' } = config.weibo;
ctx.status = 302;
ctx.redirect(`https://api.weibo.com/oauth2/authorize?client_id=${app_key}&redirect_uri=${redirect_url}`);
}
};