From 6181b388377a6ef91076c7e019e746f47b36c310 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Mon, 4 Nov 2019 17:36:31 +0800 Subject: [PATCH] Feature/weibo oauth (#3299) Co-authored-by: zytomorrow --- docs/install/README.md | 6 +++ docs/social-media.md | 14 +++++++ lib/config.js | 5 +++ lib/router.js | 3 ++ lib/routes/weibo/timeline.js | 78 ++++++++++++++++++++++++++++++++++++ 5 files changed, 106 insertions(+) create mode 100644 lib/routes/weibo/timeline.js diff --git a/docs/install/README.md b/docs/install/README.md index 8f8195c95..0700d7252 100644 --- a/docs/install/README.md +++ b/docs/install/README.md @@ -419,3 +419,9 @@ RSSHub 支持 `memory` 和 `redis` 两种缓存方式 - 吹牛部落 栏目更新 - `CHUINIU_MEMBER`: 吹牛部落登录后的 x-member,获取方式:1. 登陆后点开文章正文 2. 打开控制台 3. 刷新 4. 找到 开头的请求 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` diff --git a/docs/social-media.md b/docs/social-media.md index b41afce4f..e7d7d6e9a 100644 --- a/docs/social-media.md +++ b/docs/social-media.md @@ -576,6 +576,20 @@ pageClass: routes +### 个人时间线 + + + +::: warning 注意 + +需要对应用户打开页面进行授权生成 token 才能生成内容 + +自部署需要申请并配置微博 key,具体见部署文档 + +::: + + + ## 微博绿洲 ### 用户 diff --git a/lib/config.js b/lib/config.js index f5b179e51..f5302a620 100644 --- a/lib/config.js +++ b/lib/config.js @@ -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(); diff --git a/lib/router.js b/lib/router.js index de8306fc7..e6c9f15f3 100644 --- a/lib/router.js +++ b/lib/router.js @@ -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')); diff --git a/lib/routes/weibo/timeline.js b/lib/routes/weibo/timeline.js new file mode 100644 index 000000000..c71623283 --- /dev/null +++ b/lib/routes/weibo/timeline.js @@ -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 += `
`; + }); + 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 = ``; + } + } 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}`); + } +};