forked from NobyDa/Script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTieBa.js
353 lines (330 loc) · 11.2 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
百度贴吧签到脚本
脚本修改自: https://github.com/sazs34/TaskConfig
兼容: QuantumultX, Surge4, Loon
获取Cookie说明:
打开百度贴吧App后(AppStore中国区, 非内部版),点击"我的", 如通知成功获取cookie, 则可以使用此签到脚本.
获取Cookie后, 请将Cookie脚本禁用并移除主机名,以免产生不必要的MITM.
脚本将在每天上午9:00执行, 您可以修改执行时间。
************************
Surge 4.2.0+ 脚本配置:
************************
[Script]
贴吧签到 = type=cron,cronexp=0 9 * * *,script-path=https://raw.githubusercontent.com/NobyDa/Script/master/BDTieBa-DailyBonus/TieBa.js
贴吧获取Cookie = type=http-request,pattern=https?:\/\/(c\.tieba\.baidu\.com|180\.97\.\d+\.\d+)\/c\/s\/#,script-path=https://raw.githubusercontent.com/NobyDa/Script/master/BDTieBa-DailyBonus/TieBa.js
[MITM]
hostname= c.tieba.baidu.com
************************
QuantumultX 本地脚本配置:
************************
[task_local]
# 贴吧签到
0 9 * * * TieBa.js
[rewrite_local]
# 获取Cookie
https?:\/\/(c\.tieba\.baidu\.com|180\.97\.\d+\.\d+)\/c\/s\/# url script-request-header TieBa.js
[mitm]
hostname= c.tieba.baidu.com
************************
Loon 2.1.0+ 脚本配置:
************************
[Script]
# 贴吧签到
cron "0 9 * * *" script-path=https://raw.githubusercontent.com/NobyDa/Script/master/BDTieBa-DailyBonus/TieBa.js
# 获取Cookie
http-request https?:\/\/(c\.tieba\.baidu\.com|180\.97\.\d+\.\d+)\/c\/s\/# script-path=https://raw.githubusercontent.com/NobyDa/Script/master/BDTieBa-DailyBonus/TieBa.js
[Mitm]
hostname= c.tieba.baidu.com
*/
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() {
var headerCookie = $request.headers["Cookie"];
if (headerCookie) {
if ($nobyda.read("CookieTB") != undefined) {
if ($nobyda.read("CookieTB") != headerCookie) {
if (headerCookie.indexOf("BDUSS") != -1) {
var cookie = $nobyda.write(headerCookie, "CookieTB");
if (!cookie) {
$nobyda.notify("更新贴吧Cookie失败‼️", "", "");
} else {
$nobyda.notify("更新贴吧Cookie成功 🎉", "", "");
}
}
}
} else {
if (headerCookie.indexOf("BDUSS") != -1) {
var cookie = $nobyda.write(headerCookie, "CookieTB");
if (!cookie) {
$nobyda.notify("首次写入贴吧Cookie失败‼️", "", "");
} else {
$nobyda.notify("首次写入贴吧Cookie成功 🎉", "", "");
}
}
}
}
$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
}
};