Skip to content

Commit 0fef620

Browse files
committed
更新[百度贴吧]签到脚本: 优化代码及系统通知
1 parent 2e70ca5 commit 0fef620

3 files changed

Lines changed: 168 additions & 37 deletions

File tree

‎tieba/quanx/README.md‎

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,77 @@
11
# 百度贴吧
22

3-
优化中,暂时请别使用
3+
## 配置
4+
5+
```properties
6+
[MITM]
7+
tieba.baidu.com
8+
9+
[rewrite_local]
10+
^https?:\/\/tieba\.baidu\.com\/?.? url script-response-body tieba.cookie.js
11+
12+
[task_local]
13+
1 0 * * * tieba.js
14+
```
15+
16+
## 说明
17+
18+
1. 先把`tieba.baidu.com`加到`[MITM]`
19+
2. 再把两条远程脚本放到`[Script]`
20+
3. 先在浏览器登录 `(先登录! 先登录! 先登录!)`
21+
4. 再用浏览器访问一下: https://tieba.baidu.com 或者 https://tieba.baidu.com/index/
22+
5. `QuanX`提示: `Cookie [百度贴吧] 写入成功` (如果提示多条写入成功,忽略就好)
23+
6. 最后就可以把第 1 条脚本注释掉了
24+
25+
> 第 1 条脚本是用来获取 cookie 的, 用浏览器访问一次获取 cookie 成功后就可以删掉或注释掉了, 但请确保在`登录成功`后再获取 cookie.
26+
27+
> 第 2 条脚本是签到脚本, 每天`00:00:10`执行一次.
28+
29+
## 常见问题
30+
31+
1. 无法写入 Cookie
32+
33+
- 检查 QuanX 系统通知权限放开了没
34+
- 如果你用的是 Safari, 请尝试在浏览地址栏`手动输入网址`(不要用复制粘贴)
35+
36+
2. 写入 Cookie 成功, 但签到不成功
37+
38+
- 看看是不是在登录前就写入 Cookie 了
39+
- 如果是,请确保在登录成功后,再尝试写入 Cookie
40+
41+
3. 为什么有时成功有时失败
42+
43+
- 很正常,网络问题,哪怕你是手工签到也可能失败(凌晨签到容易拥堵就容易失败)
44+
- 暂时不考虑代码级的重试机制,但咱有配置级的(暴力美学):
45+
46+
- `Surge`配置:
47+
48+
```properties
49+
# 没有什么是一顿饭解决不了的:
50+
cron "10 0 0 * * *" script-path=xxx.js # 每天00:00:10执行一次
51+
# 如果有,那就两顿:
52+
cron "20 0 0 * * *" script-path=xxx.js # 每天00:00:20执行一次
53+
# 实在不行,三顿也能接受:
54+
cron "30 0 0 * * *" script-path=xxx.js # 每天00:00:30执行一次
55+
56+
# 再粗暴点,直接:
57+
cron "* */60 * * * *" script-path=xxx.js # 每60分执行一次
58+
```
59+
60+
- `QuanX`配置:
61+
62+
```properties
63+
[task_local]
64+
1 0 * * * xxx.js # 每天00:01执行一次
65+
2 0 * * * xxx.js # 每天00:02执行一次
66+
3 0 * * * xxx.js # 每天00:03执行一次
67+
68+
*/60 * * * * xxx.js # 每60分执行一次
69+
```
70+
71+
## 感谢
72+
73+
[@NobyDa](https://github.com/NobyDa)
74+
75+
[@lhie1](https://github.com/lhie1)
76+
77+
[@ConnersHua](https://github.com/ConnersHua)

‎tieba/quanx/tieba.cookie.js‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
const cookieName = "百度贴吧";
2-
const cookieKey = "chavy_cookie_tieba";
3-
const cookieVal = $request.headers["Cookie"];
1+
const cookieName = '百度贴吧'
2+
const cookieKey = 'chavy_cookie_tieba'
3+
const cookieVal = $request.headers['Cookie']
44

5-
if (cookieVal) {
6-
let cookie = $prefs.setValueForKey(cookieVal, cookieKey);
5+
if (cookieVal.indexOf('BDUSS') > 0) {
6+
let cookie = $prefs.setValueForKey(cookieVal, cookieKey)
77
if (cookie) {
8-
let msg = `Cookie [${cookieName}] 写入成功!`;
9-
$notify(msg, "", "详见日志");
8+
let msg = `Cookie [${cookieName}] 写入成功!`
9+
$notify(msg, '', '详见日志')
10+
console.log(msg)
11+
console.log(cookieVal)
1012
}
13+
} else {
14+
let msg = `Cookie [${cookieName}] 获取失败!`
15+
let msgDetail = `请确保在已登录状态下获取Cookie`
16+
$notify(msg, '', msgDetail)
17+
console.log(`${msg}, ${msgDetail}`)
18+
console.log(cookieVal)
1119
}
1220

13-
$done({});
21+
$done({})

‎tieba/quanx/tieba.js‎

Lines changed: 77 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,91 @@
1-
const cookieName = "百度贴吧";
2-
const cookieKey = "chavy_cookie_tieba";
3-
const cookieVal = $prefs.valueForKey(cookieKey);
1+
const cookieName = '百度贴吧'
2+
const cookieKey = 'chavy_cookie_tieba'
3+
const cookieVal = $prefs.valueForKey(cookieKey)
44

55
function sign() {
66
let url = {
7-
url: `http://tieba.baidu.com/f/like/mylike`,
7+
url: `https://tieba.baidu.com/mo/q/newmoindex`,
88
headers: {
99
Cookie: cookieVal
1010
}
11-
};
12-
$task.fetch(url).then(response => {
13-
let data = response.body;
14-
let regex = /\/f\?kw=([^{'"}]*)/g;
15-
let cnt = 0;
16-
for (const bar of data.matchAll(regex)) {
17-
cnt += 1;
18-
setTimeout(() => signBar(bar), cnt * 500);
11+
}
12+
13+
$task.fetch(url).then((response) => {
14+
let data = response.body
15+
let result = JSON.parse(data)
16+
let tbs = result.data.tbs
17+
let forums = result.data.like_forum
18+
let signinfo = {
19+
forumCnt: forums.length,
20+
signedCnt: 0,
21+
successCnt: 0,
22+
failedCnt: 0,
23+
skipedCnt: 0
24+
}
25+
26+
let singIndex = 0
27+
for (const bar of forums) {
28+
// 已签
29+
if (bar.is_sign == 1) {
30+
signinfo.signedCnt += 1
31+
signinfo.skipedCnt += 1
32+
console.log(`签到跳过: ${bar.forum_name}, 原因: 重复签到`)
33+
}
34+
// 未签
35+
else {
36+
singIndex += 1
37+
setTimeout(
38+
() =>
39+
signBar(bar, tbs, (response) => {
40+
let data = response.body
41+
let signresult = JSON.parse(data)
42+
if (signresult.no == 0 || signresult.no == 1011) {
43+
signinfo.signedCnt += 1
44+
signinfo.successCnt += 1
45+
console.log(`签到成功: ${bar.forum_name}`)
46+
} else {
47+
signinfo.failedCnt += 1
48+
console.log(`签到失败: ${bar[1]}, 编码: ${signresult.no}, 原因: ${signresult.error}`)
49+
}
50+
}),
51+
singIndex * 100
52+
)
53+
}
1954
}
20-
});
55+
check(forums, signinfo)
56+
})
2157
}
2258

23-
function signBar(bar) {
59+
function signBar(bar, tbs, cb) {
2460
let url = {
25-
url: `http://tieba.baidu.com/sign/add?ie=utf-8&kw=${bar[1]}`,
26-
method: "POST",
27-
headers: { Cookie: cookieVal }
28-
};
29-
$task.fetch(url).then(response => {
30-
let data = response.body;
31-
let result = JSON.parse(data);
32-
if (result.no == 0) {
33-
console.log(`正在签到: ${bar[1]}, 签到成功`);
61+
url: `http://tieba.baidu.com/sign/add`,
62+
method: 'POST',
63+
headers: { Cookie: cookieVal },
64+
body: `ie=utf-8&kw=${bar.forum_name}&tbs=${tbs}`
65+
}
66+
$task.fetch(url).then(cb)
67+
}
68+
69+
function check(forums, signinfo, checkms = 0) {
70+
if (signinfo.forumCnt == signinfo.signedCnt) {
71+
let title = `签到结果: ${cookieName}`
72+
let subTitle = ``
73+
let detail = `今日共签: ${signinfo.signedCnt}, 本次成功: ${signinfo.successCnt}, 失败: ${signinfo.failedCnt}, 跳过: ${signinfo.skipedCnt}`
74+
75+
// 成功数+跳过数=总数 = 全部签到完成
76+
if (signinfo.successCnt + signinfo.skipedCnt == signinfo.signedCnt) {
77+
subTitle = `全部签到完成`
78+
} else {
79+
subTitle = `部分签到完成`
80+
}
81+
console.log(`${title}, ${subTitle}, ${detail}`)
82+
$notify(title, subTitle, detail)
83+
} else {
84+
if (checkms > 5000) {
3485
} else {
35-
console.log(
36-
`正在签到: ${bar[1]}, 错误编码: ${result.no}, 错误原因: ${result.error}`
37-
);
86+
setTimeout(() => check(forums, signinfo, checkms + 100), 100)
3887
}
39-
});
88+
}
4089
}
4190

42-
sign();
91+
sign()

0 commit comments

Comments
 (0)