Skip to content

Commit

Permalink
feat: 扫码登录获取移动端的CK更新账号,带sid可减小掉号频率
Browse files Browse the repository at this point in the history
  • Loading branch information
LuluDeer authored and shanmiteko committed Aug 28, 2023
1 parent ddcfcee commit 288851b
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ buvid3亦可不填 使用随机生成值
acount 查看帐号信息
clear 清理动态和关注
update 检查更新
login 扫码登录自动更新CK
help 帮助信息
```

Expand Down
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { login } = require('./login');
const { isMe } = require('./check');
const { clear } = require('./clear');
const { start } = require('./lottery');
Expand All @@ -24,4 +25,4 @@ async function checkCookie(num) {
}


module.exports = { start, isMe, clear, checkCookie, account }
module.exports = { login, start, isMe, clear, checkCookie, account }
65 changes: 65 additions & 0 deletions lib/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const { readFileSync, writeFileSync } = require('fs');
const { log } = require("./utils");
const path = require('path');
const env_path = path.join(path.resolve(__dirname, '..'), "env.js")

/*
* 扫码登陆
* @param {string} num
*/
async function login(num) {
try {
const { pcLogin } = await require('@catlair/blogin');
const loginInfo = await pcLogin();
if (!loginInfo) {
log.error("登录状态","失败/取消");
return;
}
log.info("登录状态","登录成功");
JSON.stringify(loginInfo, null, 2);
const uid = `${loginInfo.mid}`;
const cookie = `${loginInfo.cookie}`;
log.info("账号UID", uid);
log.info("cookie", cookie);
if (replaceCookie(env_path, uid, cookie)) {
log.info("说明",`账号${num}已进行cookie自动更新,如未能生效请手动复制在env.js内替换。路径:${env_path}`);
return;
}
} catch (error) {
if (error?.message?.includes('Cannot find module')) {
log.error('请先运行 yarn add @catlair/blogin');
return;
}
log.error(error);
}
}

/*
* 正则检索uid更新cookie
* @param {string} filePath
* @param {string} uid
* @param {string} oldCK
*/
async function replaceCookie(filePath,uid,oldCK) {
try {
const content = readFileSync(filePath, 'utf-8');
const DedeUserID = `DedeUserID=${uid}`;
const reg = new RegExp(`['"]?COOKIE['"]?:\\s?['"](.*${DedeUserID}.*)['"]`, 'g');
const newCK = content.replaceAll(reg, substring => {
let quote = substring.at(0) || '';
/['"]/.test(quote) || (quote = '');
const quote2 = oldCK.includes("'")
? '"'
: substring.match(/^['"]?COOKIE['"]?:\s?(['"])/)?.[1] || '"';
return `${quote}COOKIE${quote}: ${quote2}${oldCK}${quote2}`;
});
if (content === newCK) return false;
writeFileSync(filePath, newCK);
return true;
} catch (error) {
log.error("替换错误",error);
}
return false;
}

module.exports = { login }
8 changes: 7 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { version: ve, env_file, config_file, log, hasEnv, delay, hasFileOrDir, clearLotteryInfo } = require("./lib/utils");
const {lottery_loop_wait} = require("./lib/data/config");

const metainfo = [
` _ _ _ _____ _ _ `,
Expand Down Expand Up @@ -49,7 +50,7 @@ async function main() {
await global_var.init(COOKIE, NUMBER);

/**引入基础功能 */
const { start, isMe, clear, account, checkCookie } = require("./lib/index");
const { start, isMe, clear, account, checkCookie, login } = require("./lib/index");

log.info('main', '当前为第' + NUMBER + '个账号');
log._cache.length = 0
Expand Down Expand Up @@ -79,6 +80,11 @@ async function main() {
await clear();
}
break;
case 'login':
log.info('登陆', '开始扫码');
loop_wait = lottery_loop_wait;
await login(NUMBER);
break;
case 'help':
return help_msg
case 'account':
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "自动参与B站动态抽奖",
"main": "main.js",
"scripts": {
"login": "node main.js login",
"start": "node main.js start",
"test": "node test/index.js",
"clear": "node main.js clear",
Expand Down Expand Up @@ -46,6 +47,7 @@
"chalk": "^4.1.2",
"http-proxy-agent": "^7.0.0",
"https-proxy-agent": "^7.0.0",
"nodemailer": "^6.7.0"
"nodemailer": "^6.7.0",
"@catlair/blogin": "^0.0.8"
}
}
}
2 changes: 1 addition & 1 deletion script/build/pkg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ for file in "$TARGET_DIR/"*; do
cp $TEMPLATE_CONFIG_FILE "$TMPDIR.d/$CONFIG_FILE"
cp $TEMPLATE_ENV_FILE "$TMPDIR.d/$ENV_FILE"
if [ "$(echo $file | grep '.exe')" ]; then
BATS=("start" "check" "clear" "account" "update")
BATS=("start" "check" "clear" "account" "update" "login")
for item in "${BATS[@]}"; do
create_win_bat "${item}" >"$TMPDIR.d/$item.bat"
done
Expand Down
22 changes: 22 additions & 0 deletions script/docker/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,25 @@ docker image rm -f shanmite/lottery_auto_docker
echo "see you next time!"
EOF
chmod +x remove_all.sh

echo "create login.sh"
cat >login.sh <<EOF
#!$(which env) bash
NAME=shanmite-lottery-login
if [[ -z "\$(docker ps -a | grep \$NAME)" ]]; then
docker run \\
-v $PWD/$ENV_FILE:/lottery/$ENV_FILE \\
-v $PWD/$CONFIG_FILE:/lottery/$CONFIG_FILE \\
--network host \\
--name \$NAME \\
$DOCKER_REPO \\
login
else
echo "container \$NAME already existed"
echo "history logs -> docker logs \$NAME"
echo "close this -> docker stop \$NAME"
echo "login \$NAME"
docker login \$NAME
fi
EOF
chmod +x login.sh
1 change: 1 addition & 0 deletions script/qinglong/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ create start
create check
create clear
create account
create login

cd $NAME
npm i

0 comments on commit 288851b

Please sign in to comment.