From a27d432a6dbfbe46bd216d53656b818f3cf87c79 Mon Sep 17 00:00:00 2001 From: BossDoge <66908755+lessmoe@users.noreply.github.com> Date: Wed, 29 Jul 2020 01:11:59 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E6=96=B0=E6=A6=9C(newrank.cn)=20?= =?UTF-8?q?(#5254)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/install/README.md | 5 ++ docs/social-media.md | 18 ++++++ lib/config.js | 4 ++ lib/router.js | 4 ++ lib/routes/newrank/douyin.js | 61 +++++++++++++++++++ lib/routes/newrank/utils.js | 111 +++++++++++++++++++++++++++++++++++ lib/routes/newrank/wechat.js | 36 ++++++++++++ 7 files changed, 239 insertions(+) create mode 100644 lib/routes/newrank/douyin.js create mode 100644 lib/routes/newrank/utils.js create mode 100644 lib/routes/newrank/wechat.js diff --git a/docs/install/README.md b/docs/install/README.md index e748f5b81..318256c33 100644 --- a/docs/install/README.md +++ b/docs/install/README.md @@ -506,3 +506,8 @@ RSSHub 支持使用访问密钥 / 码,白名单和黑名单三种方式进行 - `BTBYR_HOST`: 支持 ipv4 访问的 BTBYR 镜像,默认为原站 `https://bt.byr.cn/`。 - `BTBYR_COOKIE`: 注册用户登录后的 Cookie 值,获取方式:1. 登录后打开网站首页 2. 打开控制台 3. 刷新 4. 找到 请求 5. 找到请求头中的 Cookie + +- 新榜 + + - `NEWRANK_USERNAME`: 用户名 + - `NEWRANK_PASSWORD`: 密码 diff --git a/docs/social-media.md b/docs/social-media.md index 640ebe4a4..57e2c3db3 100644 --- a/docs/social-media.md +++ b/docs/social-media.md @@ -840,6 +840,24 @@ rule +## 新榜 + +::: warning 注意 +部署时需要配置 NEWRANK_USERNAME、NEWRANK_PASSWORD,具体见部署文档 +::: + +### 微信公众号 + + + +### 抖音短视频 + + + +::: warning 注意 +免费版账户抖音每天查询次数20次,如需增加次数可购买新榜会员或等待未来多账户支持 +::: + ## 知乎 ### 收藏夹 diff --git a/lib/config.js b/lib/config.js index 38494dbc7..d4d4df74a 100644 --- a/lib/config.js +++ b/lib/config.js @@ -144,6 +144,10 @@ const calculateValue = () => { host: envs.BTBYR_HOST || 'https://bt.byr.cn/', cookies: envs.BTBYR_COOKIE, }, + newrank: { + username: envs.NEWRANK_USERNAME, + password: envs.NEWRANK_PASSWORD, + }, }; }; calculateValue(); diff --git a/lib/router.js b/lib/router.js index 871ccceb3..9dac95885 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2971,6 +2971,10 @@ router.get('/ngbj/cat/:cat', require('./routes/niaogebiji/cat')); // 东方我乐多丛志 router.get('/touhougarakuta/:language/:type', require('./routes/touhougarakuta')); +// 新榜 +router.get('/newrank/wechat/:wxid', require('./routes/newrank/wechat')); +router.get('/newrank/douyin/:dyid', require('./routes/newrank/douyin')); + // 漫小肆 router.get('/manxiaosi/book/:id', require('./routes/manxiaosi/book')); diff --git a/lib/routes/newrank/douyin.js b/lib/routes/newrank/douyin.js new file mode 100644 index 000000000..6e8c128e9 --- /dev/null +++ b/lib/routes/newrank/douyin.js @@ -0,0 +1,61 @@ +const got = require('@/utils/got'); +const utils = require('./utils'); + +module.exports = async (ctx) => { + const uid = ctx.params.dyid; + const nonce = utils.random_nonce(9); + const url_detail = 'https://xd.newrank.cn/xdnphb/nr/cloud/douyin/detail/aweme?xyz=' + utils.decrypt_douyin_detail_xyz(nonce) + '&nonce=' + nonce; + const cookie = await utils.getCookie(ctx); + const response_detail = await got({ + method: 'post', + url: url_detail, + headers: { + Connection: 'keep-alive', + Cookie: cookie, + 'Content-Type': 'application/json', + }, + data: JSON.stringify({ + create_time_end: '', + create_time_start: '', + date_type: '', + is_promotion: '0', + is_seed: '0', + keyword: '', + size: 20, + sort: 'create_time', + start: 1, + uid: uid, + }), + }); + const url_account = 'https://xd.newrank.cn/xdnphb/nr/cloud/douyin/detail/accountInfoAll?nonce=' + nonce + '&xyz=' + utils.decrypt_douyin_account_xyz(nonce); + const response_account = await got({ + method: 'post', + url: url_account, + headers: { + Connection: 'keep-alive', + Cookie: cookie, + 'Content-Type': 'application/json', + }, + data: JSON.stringify({ + uid: uid, + }), + }); + const name = response_account.data.data.nickname; + const description = response_account.data.data.signature; + const articles = utils.flatten(response_detail.data.data.list); + const items = await Promise.all( + articles.map(async (item) => ({ + title: item.aweme_desc, + description: '', + link: item.share_url, + pubDate: item.create_time, + })) + ); + + ctx.state.data = { + title: name + ' - 抖音', + description: description, + link: 'https://xd.newrank.cn/data/d/account/workList/' + uid, + item: items, + }; +}; diff --git a/lib/routes/newrank/utils.js b/lib/routes/newrank/utils.js new file mode 100644 index 000000000..29427278f --- /dev/null +++ b/lib/routes/newrank/utils.js @@ -0,0 +1,111 @@ +const md5 = require('@/utils/md5'); +const got = require('@/utils/got'); +const config = require('@/config').value; +const newrank_cookie_token = 'newrank_cookie_token'; +const query_count = 'newrank_cookie_count'; +const max_query_count = 30; + +const random_nonce = (count) => { + const arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']; + const shuffled = arr.slice(0); + let i = arr.length, + temp, + index, + str = ''; + const min = i - count; + while (i-- > min) { + index = Math.floor((i + 1) * Math.random()); + temp = shuffled[index]; + str = str + temp; + } + return str; +}; + +const decrypt_login_xyz = (username, decrypt_password, nonce) => { + const str = '/nr/user/login/loginByAccount?AppKey=joker&account=' + username + '&password=' + decrypt_password + '&state=1&nonce=' + nonce; + return md5(str); +}; + +const decrypt_wechat_detail_xyz = (uid, nonce) => { + const str = '/xdnphb/detail/v1/rank/article/lists?AppKey=joker&account=' + uid + '&nonce=' + nonce; + return md5(str); +}; + +const decrypt_douyin_account_xyz = (nonce) => { + const str = '/xdnphb/nr/cloud/douyin/detail/accountInfoAll?AppKey=joker&nonce=' + nonce; + return md5(str); +}; + +const decrypt_douyin_detail_xyz = (nonce) => { + const str = '/xdnphb/nr/cloud/douyin/detail/aweme?AppKey=joker&nonce=' + nonce; + return md5(str); +}; + +const flatten = (arr) => arr.reduce((acc, val) => acc.concat(Array.isArray(val) ? flatten(val) : val), []); + +async function shouldUpdateCookie(ctx, forcedUpdate = false) { + if (forcedUpdate) { + ctx.cache.set(query_count, 0); + } else { + const count = ctx.cache.get(query_count); + if (!count) { + ctx.cache.set(query_count, 1); + } else { + if (count > max_query_count) { + ctx.cache.set(query_count, 0); + await clearCookie(ctx); + } else { + ctx.cache.set(query_count, count + 1); + } + } + } +} + +async function clearCookie(ctx) { + ctx.cache.set(newrank_cookie_token, null); +} + +async function getCookie(ctx) { + // Check if this key should be replace? every 30 times should be fine. + await shouldUpdateCookie(ctx); + let token = await ctx.cache.get(newrank_cookie_token); + const username = config.newrank.username; + const password = md5(md5(config.newrank.password) + 'daddy'); + const nonce = random_nonce(9); + const xyz = decrypt_login_xyz(username, password, nonce); + if (!token) { + const indexResponse = await got({ + method: 'post', + url: 'https://www.newrank.cn/nr/user/login/loginByAccount', + form: { + account: username, + password: password, + state: 1, + nonce: nonce, + xyz: xyz, + }, + }); + const set_cookie = indexResponse.headers['set-cookie']; + if (set_cookie) { + for (const e of set_cookie) { + if (e.indexOf('token') === 0) { + token = e.split(';')[0]; + } + } + } + ctx.cache.set(newrank_cookie_token, token, 600); + // We have acquired new cookie. It may due to cache timeout. + // Force update counter and don't wait it finished. + shouldUpdateCookie(ctx, true); + } + return token; +} + +module.exports = { + getCookie, + random_nonce, + decrypt_wechat_detail_xyz, + decrypt_douyin_account_xyz, + decrypt_douyin_detail_xyz, + flatten, +}; diff --git a/lib/routes/newrank/wechat.js b/lib/routes/newrank/wechat.js new file mode 100644 index 000000000..49afadeca --- /dev/null +++ b/lib/routes/newrank/wechat.js @@ -0,0 +1,36 @@ +const got = require('@/utils/got'); +const utils = require('./utils'); + +module.exports = async (ctx) => { + const uid = ctx.params.wxid; + const nonce = utils.random_nonce(9); + const response = await got({ + method: 'post', + url: 'https://www.newrank.cn/xdnphb/detail/v1/rank/article/lists', + headers: { + Connection: 'keep-alive', + Cookie: await utils.getCookie(ctx), + }, + form: { + account: uid, + nonce: nonce, + xyz: utils.decrypt_wechat_detail_xyz(uid, nonce), + }, + }); + const name = response.data.value.user.name; + const articles = utils.flatten(response.data.value.articles); + const items = await Promise.all( + articles.map(async (item) => ({ + title: item.title, + description: '', + link: item.url, + pubDate: item.publicTime, + })) + ); + + ctx.state.data = { + title: name + ' - 微信公众号', + link: 'https://www.newrank.cn/new/readDetial?account=' + uid, + item: items, + }; +};