-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathTieBa.js
315 lines (298 loc) · 10.1 KB
/
TieBa.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*********************************
百度贴吧签到脚本
脚本原作者: @sazs34
平台兼容: QuantumultX, Surge, Loon
更新日期: 2024/06/01
获取Cookie说明:
打开百度贴吧App后(AppStore中国区, 非内部版),点击"我的", 如通知成功获取cookie则可以使用该脚本.
*********************************
Surge(iOS 5.9.0+/macOS 5.5.0+)模块:
https://raw.githubusercontent.com/NobyDa/Script/master/Surge/Module/TieBaDailyBonus.sgmodule
*********************************
QuantumultX 任务仓库(Gallery)订阅:
https://raw.githubusercontent.com/NobyDa/Script/master/NobyDa_BoxJs.json
工具&分析->HTTP请求->右上角添加任务仓库->选择百度贴吧签到脚本添加定时任务和附加组件
*********************************
Loon 脚本订阅(非插件):
https://raw.githubusercontent.com/NobyDa/Script/master/Loon/Loon_Daily_bonus.plugin
添加后请按需启用脚本
*********************************/
var $nobyda = nobyda();
var cookieVal = $nobyda.read("CookieTB");
var useParallel = 0; //0自动切换,1串行,2并行(当贴吧数量大于30个以后,并行可能会导致QX崩溃,所以您可以自动切换)
var singleNotifyCount = 20; //想签到几个汇总到一个通知里,这里就填几个(比如我有13个要签到的,这里填了5,就会分三次消息通知过去)
var process = {
total: 0,
result: [
// {
// bar:'',
// level:0,
// exp:0,
// errorCode:0,
// errorMsg:''
// }
]
};
var url_fetch_sign = {
url: "https://tieba.baidu.com/mo/q/newmoindex",
headers: {
"Content-Type": "application/octet-stream",
Referer: "https://tieba.baidu.com/index/tbwise/forum",
Cookie: cookieVal,
"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 12_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16A366"
}
};
var url_fetch_add = {
url: "https://tieba.baidu.com/sign/add",
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Cookie: cookieVal,
"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 10_1_1 like Mac OS X; zh-CN) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/14B100 UCBrowser/10.7.5.650 Mobile"
},
body: ""
};
if ($nobyda.isRequest) {
GetCookie()
} else {
signTieBa()
}
function signTieBa() {
useParallel = $nobyda.read("BDTB_DailyBonus_Mode") || useParallel
singleNotifyCount = $nobyda.read("BDTB_DailyBonus_notify") || singleNotifyCount
if (!cookieVal) {
$nobyda.notify("贴吧签到", "签到失败", "未获取到cookie");
return $nobyda.done()
}
$nobyda.get(url_fetch_sign, function(error, response, data) {
if (error) {
$nobyda.notify("贴吧签到", "签到失败", "未获取到签到列表");
$nobyda.done()
} else {
// $nobyda.notify("贴吧签到", "贴吧列表", response.body);
var body = JSON.parse(data);
var isSuccessResponse = body && body.no == 0 && body.error == "success" && body.data.tbs;
if (!isSuccessResponse) {
$nobyda.notify("贴吧签到", "签到失败", (body && body.error) ? body.error : "接口数据获取失败");
return $nobyda.done()
}
process.total = body.data.like_forum.length;
if (body.data.like_forum && body.data.like_forum.length > 0) {
if (useParallel == 1 || (useParallel == 0 && body.data.like_forum.length >= 30)) {
signBars(body.data.like_forum, body.data.tbs, 0);
} else {
for (const bar of body.data.like_forum) {
signBar(bar, body.data.tbs);
}
}
} else {
$nobyda.notify("贴吧签到", "签到失败", "请确认您有关注的贴吧");
return $nobyda.done()
}
}
})
}
function signBar(bar, tbs) {
if (bar.is_sign == 1) { //已签到的,直接不请求接口了
process.result.push({
bar: `${bar.forum_name}`,
level: bar.user_level,
exp: bar.user_exp,
errorCode: 9999,
errorMsg: "已签到"
});
checkIsAllProcessed();
} else {
url_fetch_add.body = `tbs=${tbs}&kw=${bar.forum_name}&ie=utf-8`;
$nobyda.post(url_fetch_add, function(error, response, data) {
if (error) {
process.result.push({
bar: bar.forum_name,
errorCode: 999,
errorMsg: '接口错误'
});
checkIsAllProcessed();
} else {
try {
var addResult = JSON.parse(data);
if (addResult.no == 0) {
process.result.push({
bar: bar.forum_name,
errorCode: 0,
errorMsg: `获得${addResult.data.uinfo.cont_sign_num}积分,第${addResult.data.uinfo.user_sign_rank}个签到`
});
} else {
process.result.push({
bar: bar.forum_name,
errorCode: addResult.no,
errorMsg: addResult.error
});
}
} catch (e) {
$nobyda.notify("贴吧签到", "贴吧签到数据处理异常", JSON.stringify(e));
$nobyda.done()
}
checkIsAllProcessed();
}
})
}
}
function signBars(bars, tbs, index) {
//$nobyda.notify("贴吧签到", `进度${index}/${bars.length}`, "");
if (index >= bars.length) {
//$nobyda.notify("贴吧签到", "签到已满", `${process.result.length}`);
checkIsAllProcessed();
} else {
var bar = bars[index];
if (bar.is_sign == 1) { //已签到的,直接不请求接口了
process.result.push({
bar: `${bar.forum_name}`,
level: bar.user_level,
exp: bar.user_exp,
errorCode: 9999,
errorMsg: "已签到"
});
signBars(bars, tbs, ++index);
} else {
url_fetch_add.body = `tbs=${tbs}&kw=${bar.forum_name}&ie=utf-8`;
$nobyda.post(url_fetch_add, function(error, response, data) {
if (error) {
process.result.push({
bar: bar.forum_name,
errorCode: 999,
errorMsg: '接口错误'
});
signBars(bars, tbs, ++index);
} else {
try {
var addResult = JSON.parse(data);
if (addResult.no == 0) {
process.result.push({
bar: bar.forum_name,
errorCode: 0,
errorMsg: `获得${addResult.data.uinfo.cont_sign_num}积分,第${addResult.data.uinfo.user_sign_rank}个签到`
});
} else {
process.result.push({
bar: bar.forum_name,
errorCode: addResult.no,
errorMsg: addResult.error
});
}
} catch (e) {
$nobyda.notify("贴吧签到", "贴吧签到数据处理异常", JSON.stringify(e));
$nobyda.done()
}
signBars(bars, tbs, ++index)
}
})
}
}
}
function checkIsAllProcessed() {
//$nobyda.notify("贴吧签到", `最终进度${process.result.length}/${process.total}`, "");
if (process.result.length != process.total) return;
for (var i = 0; i < Math.ceil(process.total / singleNotifyCount); i++) {
var notify = "";
var spliceArr = process.result.splice(0, singleNotifyCount);
var notifySuccessCount = 0;
for (const res of spliceArr) {
if (res.errorCode == 0 || res.errorCode == 9999) {
notifySuccessCount++;
}
if (res.errorCode == 9999) {
notify += `【${res.bar}】已经签到,当前等级${res.level},经验${res.exp}
`;
} else {
notify += `【${res.bar}】${res.errorCode==0?'签到成功':'签到失败'},${res.errorCode==0?res.errorMsg:('原因:'+res.errorMsg)}
`;
}
}
$nobyda.notify("贴吧签到", `签到${spliceArr.length}个,成功${notifySuccessCount}个`, notify);
$nobyda.done()
}
}
function GetCookie() {
let headerCookie = $request.headers["Cookie"] || $request.headers["cookie"];
if (headerCookie && headerCookie.includes('BDUSS=')) {
if (!cookieVal) {
$nobyda.notify("写入百度贴吧Cookie成功 🎉", "", "");
} else {
console.log(`写入百度贴吧Cookie成功 🎉`);
}
$nobyda.write(headerCookie, "CookieTB")
} else {
console.log(`写入Cookie失败, BDUSS值缺失. `);
}
return $nobyda.done();
}
function nobyda() {
const isRequest = typeof $request != "undefined"
const isSurge = typeof $httpClient != "undefined"
const isQuanX = typeof $task != "undefined"
const notify = (title, subtitle, message) => {
if (isQuanX) $notify(title, subtitle, message)
if (isSurge) $notification.post(title, subtitle, message)
}
const write = (value, key) => {
if (isQuanX) return $prefs.setValueForKey(value, key)
if (isSurge) return $persistentStore.write(value, key)
}
const read = (key) => {
if (isQuanX) return $prefs.valueForKey(key)
if (isSurge) return $persistentStore.read(key)
}
const adapterStatus = (response) => {
if (response) {
if (response.status) {
response["statusCode"] = response.status
} else if (response.statusCode) {
response["status"] = response.statusCode
}
}
return response
}
const get = (options, callback) => {
if (isQuanX) {
if (typeof options == "string") options = {
url: options
}
options["method"] = "GET"
$task.fetch(options).then(response => {
callback(null, adapterStatus(response), response.body)
}, reason => callback(reason.error, null, null))
}
if (isSurge) $httpClient.get(options, (error, response, body) => {
callback(error, adapterStatus(response), body)
})
}
const post = (options, callback) => {
if (isQuanX) {
if (typeof options == "string") options = {
url: options
}
options["method"] = "POST"
$task.fetch(options).then(response => {
callback(null, adapterStatus(response), response.body)
}, reason => callback(reason.error, null, null))
}
if (isSurge) {
$httpClient.post(options, (error, response, body) => {
callback(error, adapterStatus(response), body)
})
}
}
const done = (value = {}) => {
if (isQuanX) return $done(value)
if (isSurge) isRequest ? $done(value) : $done()
}
return {
isRequest,
notify,
write,
read,
get,
post,
done
}
};