-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathweibo.js
414 lines (328 loc) · 12.4 KB
/
weibo.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
* @Author: Hellager
* @Date: 2022-06-23 11:29:02
* @LastEditTime: 2022-06-23 16:15:12
* @LastEditors: Hellager
*/
const axios = require('axios');
const dayjs = require('dayjs');
const qs = require('qs');
const dotenv = require("dotenv");
const notifier = require('./sendNotify.js');
dotenv.config();
/** dependencies **/
class logger {
constructor () {
}
info(msg) {
const current_time = dayjs().format('YYYY-MM-DD HH:mm:ss');
console.log(`${current_time} info ${msg}`);
}
warning(msg) {
const current_time = dayjs().format('YYYY-MM-DD HH:mm:ss');
console.log(`${current_time} warning ${msg}`);
}
error(msg) {
const current_time = dayjs().format('YYYY-MM-DD HH:mm:ss');
console.log(`${current_time} error ${msg}`);
}
}
log = new logger();
var sharedArrayBuffer_for_sleep = new SharedArrayBuffer( 4 ) ;
var sharedArray_for_sleep = new Int32Array( sharedArrayBuffer_for_sleep ) ;
var sleep = function( n ) {
Atomics.wait( sharedArray_for_sleep , 0 , 0 , n * 1000 ) ;
}
/*************** */
let COOKIE_WEIBO = '';
let CURRENT_COOKIE = '';
if (process.env.COOKIE_WEIBO) {
COOKIE_WEIBO = process.env.COOKIE_WEIBO
}
const error_msg_list = [];
const request_headers = {
'Accept': '*/*',
'Host': 'api.weibo.cn',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-Hans-CN;q=1, en-CN;q=0.9',
'User-Agent': 'WeiboOverseas/4.7.8 (iPhone; iOS 15.5; Scale/3.00)',
}
function random_range(min, max) {
switch (arguments.length) {
case 1:
return parseInt(Math.random() * min + 1, 10);
break;
case 2:
return parseInt(Math.random() * (max - min + 1) + min, 10);
break;
default:
return 0;
break;
}
}
function form_params(data, type) {
let params = {}
if (data === '') {
log.error(`数据为空, 类型为 ${type}`)
error_msg_list.push(`数据为空, 类型为 ${type}`);
return false;
}
if (type === 'cookie') {
data.split('&').forEach((item, index) => {
const split_item = item.split('=');
params[split_item[0]] = split_item[1];
})
return params;
} else if (type === 'sign_action') {
let cookie = form_params(CURRENT_COOKIE, 'cookie');
let request_url = data.slice(data.indexOf('request_url=') + 'request_url='.length, data.indexOf('%26container_id'));
const abort_property = ['containerid', 'extparam', 'page', 'v_f'];
const cookie_order = ['aid', 'c', 'from', 'gsid', 'i', 'lang', 'request_url', 's', 'ua', 'v_p'];
abort_property.forEach(item => {
Reflect.deleteProperty(cookie, item);
});
cookie.request_url = request_url;
cookie_order.forEach(item => {
params[item] = cookie[item];
})
return params;
} else if (type === 'profile') {
let cookie = {}
data.split('&').forEach((item, index) => {
const split_item = item.split('=');
cookie[split_item[0]] = split_item[1];
})
const abort_property = ['containerid', 'extparam', 'page', 'v_f'];
const cookie_order = ['aid', 'c', 'from', 'gsid', 'i', 'lang', 's', 'ua', 'v_p'];
abort_property.forEach(item => {
Reflect.deleteProperty(cookie, item);
});
cookie_order.forEach(item => {
params[item] = cookie[item];
})
return qs.stringify(params);
} else {
try {
data.split('&').forEach((item, index) => {
const split_item = item.split('=');
params[split_item[0]] = split_item[1];
})
}
catch (err){
log.error(err);
}
return params;
}
}
async function get_username(cookie) {
const request_username_headers = {
'Accept': '*/*',
'Host': 'api.weibo.cn',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-Hans-CN;q=1, en-CN;q=0.9',
'User-Agent': 'WeiboOverseas/4.7.8 (iPhone; iOS 15.5; Scale/3.00)',
'Content-type': 'application/x-www-form-urlencoded',
};
const base_url = 'https://api.weibo.cn/2/users/update';
// const request_params = form_params(cookie, 'profile');
let username = '';
await axios({
method: 'post',
url: base_url,
data: cookie,
headers: request_username_headers,
timeout: 3000,
}).then(res => {
const request_res = res.data;
if (request_res.errno != undefined) {
log.error(`获取用户名失败, 错误提示:${request_res.errmsg}`)
error_msg_list.push(`获取用户名失败, 错误提示:${request_res.errmsg}`);
}
username = request_res.name;
}).catch(err => {
log.error(`获取用户名失败, 错误提示:${err}`)
error_msg_list.push(`获取用户名失败, 错误提示:${err}`);
})
log.info(`当前微博用户: ${username}`);
return username;
}
async function get_follow_list(cookie) {
log.info('开始获取超话列表');
let page = '1'
let since_id = ''
let follow_list = []
let data = form_params(cookie, 'cookie');
const base_url = 'https://api.weibo.cn/2/cardlist';
let res_obj = {};
while (true) {
sleep(random_range(10, 15));
data['v_f'] = page;
data['since_id'] = since_id;
const isSuccess = await axios({
method: 'get',
url: base_url,
headers: request_headers,
params: data,
timeout: 3000,
})
.then(res => {
res_obj = res.data;
if (res_obj.errno != undefined) {
log.info(`获取超话列表第 ${page} 页数据失败, 错误提示:${res_obj.errmsg}`)
error_msg_list.push(`获取超话列表第 ${page} 页数据失败, 错误提示:${res_obj.errmsg}`);
return;
}
let card_group = res_obj.cards[0].card_group;
let card_list_info = res_obj.cardlistInfo;
since_id = card_list_info.since_id;
try {
page = JSON.parse(since_id).page.toString();
} catch {
page = (parseInt(page) + 1).toString();
}
for (const item of card_group) {
if (item.card_type === '8') {
const page_index = item.itemid.slice(item.itemid.indexOf('follow_super_follow_') + 'follow_super_follow_'.length, item.itemid.length);
const topic = {
title: item.title_sub,
level: item.desc1.slice(item.desc1.indexOf('.') + 1, item.desc1.length),
sign_status: item.buttons[0].name,
sign_action: item.buttons[0].name === '已签' ? '' : item.buttons[0].params.action,
since_id: since_id,
page: page_index.split('_')[0],
}
follow_list.push(topic);
}
}
log.info(`获取超话列表第 ${page-1} 页数据成功`);
})
.catch(err => {
log.error(`获取超话列表第 ${page} 页数据失败. 错误提示:${err}`)
error_msg_list.push(`获取超话列表第 ${page} 页数据失败, 错误提示:${err}`);
})
if (since_id === '' || isSuccess === false || since_id === '0') {
break;
}
}
return follow_list;
}
async function sign_topic(topic_list) {
log.info('开始执行签到');
const base_url = 'https://api.weibo.cn/2/page/button';
const list_length = topic_list.length;
for (let i = 0; i < topic_list.length; i++) {
const item = topic_list[i];
const index = i + 1;
if (item.sign_status === '已签') {
log.info(`超话 ${item.title} 已签到 ${index} / ${list_length}`);
} else {
let sign_params = form_params(item.sign_action, 'sign_action');
sleep(random_range(15, 30));
await axios({
method: 'get',
url: base_url,
headers: request_headers,
params: sign_params,
timeout: 3000
}).then(res => {
let res_obj = res.data;
if (res_obj.errno != undefined) {
log.error(`超话 ${item.title} 签到失败 ${index} / ${list_length}, 错误提示:${res_obj.errmsg}`);
error_msg_list.push(`超话 ${item.title} 签到失败 ${index} / ${list_length}, 错误提示:${res_obj.errmsg}`);
} else {
if (res_obj.msg === '已签到') {
item.sign_status = '已签';
log.info(`超话 ${item.title} 签到成功 ${index} / ${list_length}`);
}
}
})
.catch(err => {
log.error(`超话 ${item.title} 签到失败 ${index} / ${list_length}, 错误提示:${err}`);
error_msg_list.push(`超话 ${item.title} 签到失败 ${index} / ${list_length}, 错误提示:${err}`);
})
}
}
log.info('当前用户列表签到执行完成');
return topic_list;
}
async function send_notify(res) {
let total_success = 0;
let total_failed = 0;
let content = ``;
for (let i = 0; i < res.length; i++) {
const item = res[i];
const name = item.name;
const data = item.data;
let total_topic = data.length;
let signed_topic = 0;
let failed_topic_list = [];
data.forEach(item => item.sign_status === '已签' ? signed_topic++ : failed_topic_list.push(item.title));
let failed_content = ''
if (failed_topic_list.length != 0) {
total_failed++;
let start_with = ' 签到❌ \n';
failed_topic_list.forEach(item => {
start_with = start_with + ' ' + item + '\n';
})
failed_content = start_with;
} else {
total_success++;
}
content = content.concat(`${name}\n 签到✅ ${signed_topic} / ${total_topic} \n`);
content = failed_topic_list.length === 0 ? content.concat('\n\n') : content.concat(failed_content + '\n\n');
}
let title = `微博超话签到 ✔ ${total_success} ✖️ ${total_failed}`;
notifier.sendNotify(title, content, {}, '');
return;
}
async function send_err_msg(msg) {
if (msg.length === 0) return;
let content = '';
for (let i = 0; i < msg.length; i++) {
content = content.concat(msg[i] + '\n');
};
let title = '微博超话签到脚本执行错误通知';
notifier.sendNotify(title, content, {}, '');
return;
}
async function main_handler() {
log.info('微博超话签到脚本开始执行');
let account_list = [];
if (COOKIE_WEIBO.indexOf(';') === -1) {
account_list.push(COOKIE_WEIBO);
} else {
try {
account_list = COOKIE_WEIBO.split(';');
}
catch(err) {
log.error('获取 Cookie 失败');
error_msg_list.push(`获取 Cookie 失败`);
return;
}
}
if (account_list.length === 0) {
log.error('未获得任何 Cookie 信息');
error_msg_list.push(`未获得任何 Cookie 信息`);
return;
}
let task_res = []
for (let i = 0; i < account_list.length; i++) {
let account_cookie = account_list[i];
if (account_cookie.includes('cardlist?')) {
account_cookie = account_cookie.slice(account_cookie.indexOf('cardlist?') + 'cardlist?'.length, account_cookie.length);
}
CURRENT_COOKIE = account_cookie;
const username = await get_username(account_cookie);
const follow_list = await get_follow_list(account_cookie);
if (follow_list.length === 0) {
log.warning('无待签到列表');
error_msg_list.push(`无待签到列表`);
return;
}
const sign_result = await sign_topic(follow_list);
task_res.push({name: username, data: sign_result});
}
const res = await send_notify(task_res);
const err_res = await send_err_msg(error_msg_list);
}
main_handler();