forked from Jasonliu-0/Newapi-checkin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckin.py
More file actions
852 lines (734 loc) · 33.3 KB
/
Copy pathcheckin.py
File metadata and controls
852 lines (734 loc) · 33.3 KB
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
NewAPI 自动签到脚本
支持多账号签到,通过 GitHub Actions 定时执行
"""
import os
import sys
import json
import requests
from datetime import datetime
from typing import Optional
try:
from cf_bypass import detect_cloudflare_block, CloudflareBypasser
CF_BYPASS_AVAILABLE = True
except ImportError:
CF_BYPASS_AVAILABLE = False
detect_cloudflare_block = None
CloudflareBypasser = None
try:
from dingtalk_notifier import send_checkin_notification
except ImportError:
send_checkin_notification = None
try:
from notifier import send_email_notification, send_serverchan_notification
except ImportError:
send_email_notification = None
send_serverchan_notification = None
try:
from lottery import run_for_account as lottery_run_for_account
from lottery import run_gwent_for_account
except ImportError:
lottery_run_for_account = None
run_gwent_for_account = None
class NewAPICheckin:
"""NewAPI 签到类"""
@staticmethod
def _mask_url(url: str) -> str:
"""
脱敏 URL,隐藏域名细节
例如: https://api.example.com -> https://api.***.**
"""
try:
from urllib.parse import urlparse
parsed = urlparse(url)
domain_parts = parsed.netloc.split('.')
if len(domain_parts) >= 2:
# 保留第一部分和最后一部分,中间用 *** 代替
masked_domain = f"{domain_parts[0]}.***." + '.'.join(domain_parts[-1:])
else:
masked_domain = '***'
return f"{parsed.scheme}://{masked_domain}"
except Exception:
return 'https://***'
@staticmethod
def _mask_user_id(user_id: str) -> str:
"""
脱敏用户ID
例如: 1429 -> ****
"""
return '****'
def __init__(self, base_url: str, session_cookie: str, user_id: str = None, cf_clearance: str = None,
login_username: str = None, login_password: str = None):
self.base_url = base_url.rstrip('/')
self.session_cookie = session_cookie
self.original_cf_clearance = cf_clearance
self.cf_bypassed = False
self.login_username = login_username
self.login_password = login_password
self.session = requests.Session()
self.session.cookies.set('session', session_cookie)
if cf_clearance:
self.session.cookies.set('cf_clearance', cf_clearance)
self.session.headers.update({
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'Cache-Control': 'no-store',
'Pragma': 'no-cache',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
})
self.user_id = user_id
if user_id:
self.session.headers.update({'new-api-user': str(user_id)})
def _try_login(self) -> bool:
"""
尝试使用配置的账号密码重新登录,获取新的 session
登录接口:POST /api/user/login
请求体:{"username": x, "password": x}
成功时 cookie 中包含新的 session_id
Returns:
True 如果登录成功,False 否则
"""
if not self.login_username or not self.login_password:
return False
print(f' [登录] 尝试重新登录...')
try:
resp = self.session.post(
f'{self.base_url}/api/user/login',
json={'username': self.login_username, 'password': self.login_password},
timeout=30
)
if resp.status_code == 429:
print(f' [登录] 请求过多 (429),跳过登录')
return False
try:
data = resp.json()
except json.JSONDecodeError:
content_preview = resp.text[:100] if resp.text else '(空响应)'
print(f' [登录] 响应格式错误 (HTTP {resp.status_code}): {content_preview}')
return False
if resp.status_code == 200 and data.get('success'):
# 从响应 cookie 中获取新的 session_id
new_session = None
for cookie in resp.cookies:
if cookie.name == 'session' or cookie.name == 'session_id':
new_session = cookie.value
break
if new_session:
self.session_cookie = new_session
self.session.cookies.set('session', new_session)
print(f' [登录] 登录成功,已更新 session')
return True
else:
# 可能 session cookie 在 set-cookie 头中
set_cookie = resp.headers.get('Set-Cookie', '')
import re
match = re.search(r'(?:session|session_id)=([^;]+)', set_cookie)
if match:
new_session = match.group(1)
self.session_cookie = new_session
self.session.cookies.set('session', new_session)
print(f' [登录] 登录成功,已更新 session')
return True
print(f' [登录] 登录成功但未获取到新的 session cookie')
return False
else:
msg = data.get('message', '未知错误')
print(f' [登录] 登录失败: {msg}')
return False
except Exception as e:
print(f' [登录] 登录请求异常: {e}')
return False
def get_user_info(self, verbose: bool = False) -> Optional[dict]:
"""
获取用户信息
自动设置 new-api-user 请求头
Args:
verbose: 是否显示详细调试信息
"""
try:
resp = self.session.get(f'{self.base_url}/api/user/self', timeout=30)
if verbose:
print(f' [调试] HTTP 状态码: {resp.status_code}')
print(f' [调试] 响应内容预览: {resp.text[:200]}...')
# 检查认证失败
if resp.status_code == 401:
print(f'[错误] 认证失败 (401): Session 可能已过期')
if verbose:
print(f' [调试] 完整响应: {resp.text[:500]}')
# 尝试自动登录
if self._try_login():
print(f' [登录] 重试获取用户信息...')
resp = self.session.get(f'{self.base_url}/api/user/self', timeout=30)
if resp.status_code == 200:
try:
data = resp.json()
if data.get('success'):
user_data = data.get('data')
if user_data and 'id' in user_data:
self.user_id = user_data['id']
self.session.headers.update({
'new-api-user': str(self.user_id)
})
return user_data
except Exception:
pass
print(f' [登录] 重试后仍无法获取用户信息')
return None
else:
if self.login_username and self.login_password:
print(' [登录] 尝试登录不成功')
else:
print(' [登录] 未配置账号密码,无法自动重新登录')
return None
# 尝试解析 JSON
try:
data = resp.json()
except json.JSONDecodeError as e:
# 检测是否是 Cloudflare 拦截
if detect_cloudflare_block:
is_blocked, reason = detect_cloudflare_block(resp.status_code, resp.text)
if is_blocked:
print(f'[CF] 获取用户信息时检测到 Cloudflare 拦截: {reason}')
print(f'[CF] 该站点需要 CF 绕过才能访问')
return None
print(f'[错误] 响应格式错误 (HTTP {resp.status_code}): 无法解析 JSON')
if verbose:
print(f' [调试] 原始响应: {resp.text[:500]}')
return None
if verbose:
print(f' [调试] success 字段: {data.get("success")}')
print(f' [调试] message 字段: {data.get("message")}')
if resp.status_code == 200:
if data.get('success'):
user_data = data.get('data')
# 保存用户ID并设置到请求头
if user_data and 'id' in user_data:
self.user_id = user_data['id']
self.session.headers.update({
'new-api-user': str(self.user_id)
})
return user_data
else:
if verbose:
print(f' [调试] API 返回失败: {data.get("message", "未知错误")}')
else:
print(f'[错误] HTTP {resp.status_code}: {data.get("message", "未知错误")}')
return None
except requests.exceptions.Timeout:
print(f'[错误] 请求超时')
return None
except requests.exceptions.RequestException as e:
print(f'[错误] 网络请求失败: {e}')
return None
except Exception as e:
print(f'[错误] 未知错误: {e}')
if verbose:
import traceback
traceback.print_exc()
return None
def checkin(self) -> dict:
"""
执行签到
流程(借鉴 Chrome 扩展 background.js:115-248):
1. requests 直连签到(快速模式)
2. CF 拦截检测 → Playwright 获取 cookie 后重新签到
3. 仍然失败 → Playwright 浏览器内直接签到(终极回退)
Returns:
签到结果字典
"""
result = {
'success': False,
'message': '',
'checkin_date': None,
'quota_awarded': None
}
try:
resp = self.session.post(f'{self.base_url}/api/user/checkin', timeout=30)
if resp.status_code == 401:
result['message'] = '认证失败: Session 可能已过期,请重新获取'
# 尝试自动登录
if self._try_login():
print(f' [登录] 重试签到...')
resp = self.session.post(f'{self.base_url}/api/user/checkin', timeout=30)
if resp.status_code == 200:
try:
data = resp.json()
if data.get('success'):
result['success'] = True
result['message'] = data.get('message', '签到成功')
checkin_data = data.get('data', {})
result['checkin_date'] = checkin_data.get('checkin_date')
result['quota_awarded'] = checkin_data.get('quota_awarded')
return result
except Exception:
pass
result['message'] = '重新登录后签到失败'
return result
else:
if self.login_username and self.login_password:
result['message'] = '尝试登录不成功'
return result
try:
data = resp.json()
except json.JSONDecodeError:
if detect_cloudflare_block:
is_blocked, reason = detect_cloudflare_block(resp.status_code, resp.text)
if is_blocked:
print(f'[CF] 检测到 Cloudflare 拦截: {reason}')
return self._cf_bypass_checkin()
content_preview = resp.text[:200] if resp.text else '(空响应)'
result['message'] = f'响应格式错误 (HTTP {resp.status_code}): {content_preview}'
return result
if detect_cloudflare_block and resp.status_code in (403, 503):
is_blocked, reason = detect_cloudflare_block(resp.status_code, json.dumps(data))
if is_blocked:
print(f'[CF] 检测到 Cloudflare 拦截: {reason}')
return self._cf_bypass_checkin()
if resp.status_code == 200:
if data.get('success'):
result['success'] = True
result['message'] = data.get('message', '签到成功')
checkin_data = data.get('data', {})
result['checkin_date'] = checkin_data.get('checkin_date')
result['quota_awarded'] = checkin_data.get('quota_awarded')
else:
message = data.get('message', '签到失败')
already_keywords = ['已签到', '已经签到', 'already', '重复签到']
already_checked_in = any(k in message for k in already_keywords)
if already_checked_in:
result['success'] = True
result['message'] = message
else:
result['message'] = message
else:
result['message'] = f'HTTP {resp.status_code}: {data.get("message", "未知错误")}'
except requests.exceptions.Timeout:
result['message'] = '请求超时'
except requests.exceptions.RequestException as e:
result['message'] = f'网络请求失败: {e}'
except Exception as e:
result['message'] = f'未知错误: {e}'
return result
def _cf_bypass_checkin(self) -> dict:
"""
CF 绕过签到流程
在同一个 Playwright 会话中完成 CF 绕过 + 签到,
不拆分 cookie 提取和 requests 重试(因为 cf_clearance 绑定浏览器指纹)
"""
result = {
'success': False,
'message': '',
'checkin_date': None,
'quota_awarded': None
}
if not CF_BYPASS_AVAILABLE or not CloudflareBypasser:
result['message'] = 'Cloudflare 拦截: 需安装 Playwright 才能自动绕过 (pip install playwright && playwright install chromium)'
return result
bypasser = CloudflareBypasser(self.base_url, self.session_cookie, self.user_id)
if not bypasser.is_available():
result['message'] = 'Cloudflare 拦截: Playwright 未正确安装'
return result
print('[CF] 开始 Playwright 绕过流程...')
browser_result = bypasser.bypass_and_checkin()
if not browser_result:
result['message'] = 'Cloudflare 绕过失败: 无法通过 CF 验证'
return result
self.cf_bypassed = True
if browser_result.get('error'):
result['message'] = f'CF 绕过后签到失败: {browser_result["error"]}'
return result
if browser_result.get('alreadyCheckedIn'):
result['success'] = True
result['message'] = browser_result.get('message', '今日已签到 (CF绕过)')
elif browser_result.get('success'):
result['success'] = True
result['message'] = browser_result.get('message', '签到成功 (CF绕过)')
data = browser_result.get('data', {})
if isinstance(data, dict):
checkin_data = data.get('data', data)
result['checkin_date'] = checkin_data.get('checkin_date')
result['quota_awarded'] = checkin_data.get('quota_awarded')
else:
result['message'] = browser_result.get('message', 'CF 绕过后签到失败')
return result
def get_checkin_history(self, month: str = None) -> Optional[dict]:
"""
获取签到历史
Args:
month: 月份,格式 YYYY-MM,默认当前月
"""
if month is None:
month = datetime.now().strftime('%Y-%m')
try:
resp = self.session.get(
f'{self.base_url}/api/user/checkin',
params={'month': month},
timeout=30
)
if resp.status_code == 200:
data = resp.json()
if data.get('success'):
return data.get('data')
return None
except Exception as e:
print(f'[错误] 获取签到历史失败: {e}')
return None
def parse_accounts(accounts_str: str) -> list:
"""
解析账号配置
支持格式:
1. 单账号: BASE_URL#SESSION_COOKIE
2. 多账号: BASE_URL1#SESSION1,BASE_URL2#SESSION2
3. JSON格式: [{"url": "...", "session": "..."}]
"""
accounts = []
if not accounts_str:
return accounts
# 尝试 JSON 格式
try:
data = json.loads(accounts_str)
if isinstance(data, list):
for item in data:
if isinstance(item, dict) and 'url' in item and 'session' in item:
account = {
'url': item['url'],
'session': item['session'],
'name': item.get('name', '')
}
# 如果提供了 user_id,添加到账号信息中
if 'user_id' in item:
account['user_id'] = item['user_id']
# 如果提供了 cf_clearance,添加到账号信息中
if 'cf_clearance' in item:
account['cf_clearance'] = item['cf_clearance']
# 如果提供了登录账号密码,添加到账号信息中
if 'login_username' in item:
account['login_username'] = item['login_username']
if 'login_password' in item:
account['login_password'] = item['login_password']
accounts.append(account)
return accounts
except json.JSONDecodeError:
pass
# 简单格式: URL#SESSION,URL#SESSION
for part in accounts_str.split(','):
part = part.strip()
if '#' in part:
url, session = part.split('#', 1)
accounts.append({
'url': url.strip(),
'session': session.strip(),
'name': ''
})
return accounts
def load_config_from_cloud(config_url: str, config_auth: str = None) -> Optional[str]:
"""
从云端(WebDAV)加载配置
支持:
- 坚果云 WebDAV
- 群晖 NAS WebDAV
- NextCloud WebDAV
- 任何支持 WebDAV/直接链接的云存储
Args:
config_url: 配置文件 URL (WebDAV 或直接下载链接)
config_auth: 认证信息,格式:
- Basic Auth: "username:password"
- Token Auth: "token:your_token"
"""
try:
headers = {}
if config_auth:
if config_auth.startswith('token:'):
headers['Authorization'] = 'Bearer ' + config_auth[6:]
elif ':' in config_auth:
import base64 as b64mod
credentials = b64mod.b64encode(config_auth.encode('utf-8')).decode('utf-8')
headers['Authorization'] = 'Basic ' + credentials
print(f'[云端] 正在从云端加载配置: {NewAPICheckin._mask_url(config_url)}')
resp = requests.get(config_url, headers=headers, timeout=30)
if resp.status_code == 401:
print('[云端] 认证失败: 请检查 CONFIG_AUTH 配置')
return None
elif resp.status_code == 404:
print('[云端] 配置文件不存在: 请先通过配置生成器保存到云端')
return None
elif resp.status_code != 200:
print(f'[云端] 加载失败: HTTP {resp.status_code}')
return None
data = resp.json()
if isinstance(data, list):
accounts_str = json.dumps(data)
print(f'[云端] 成功加载 {len(data)} 个账号配置')
return accounts_str
elif isinstance(data, dict) and 'accounts' in data:
accounts = data['accounts']
accounts_str = json.dumps(accounts)
print(f'[云端] 成功加载 {len(accounts)} 个账号配置')
if data.get('dingtalk'):
dt = data['dingtalk']
if dt.get('webhook') and not os.environ.get('DINGTALK_WEBHOOK'):
os.environ['DINGTALK_WEBHOOK'] = dt['webhook']
if dt.get('secret') and not os.environ.get('DINGTALK_SECRET'):
os.environ['DINGTALK_SECRET'] = dt['secret']
if dt.get('webhook'):
print('[云端] 已从云端加载钉钉通知配置')
if data.get('email'):
em = data['email']
if em.get('smtp_host') and not os.environ.get('EMAIL_SMTP_HOST'):
os.environ['EMAIL_SMTP_HOST'] = em['smtp_host']
if em.get('smtp_port') and not os.environ.get('EMAIL_SMTP_PORT'):
os.environ['EMAIL_SMTP_PORT'] = str(em['smtp_port'])
if em.get('user') and not os.environ.get('EMAIL_USER'):
os.environ['EMAIL_USER'] = em['user']
if em.get('pass') and not os.environ.get('EMAIL_PASS'):
os.environ['EMAIL_PASS'] = em['pass']
if em.get('to') and not os.environ.get('EMAIL_TO'):
os.environ['EMAIL_TO'] = em['to']
if em.get('from_addr') and not os.environ.get('EMAIL_FROM'):
os.environ['EMAIL_FROM'] = em['from_addr']
if em.get('smtp_host'):
print('[云端] 已从云端加载邮件通知配置')
if data.get('serverchan'):
sc = data['serverchan']
if sc.get('sendkey') and not os.environ.get('SERVERCHAN_SENDKEY'):
os.environ['SERVERCHAN_SENDKEY'] = sc['sendkey']
if sc.get('sendkey'):
print('[云端] 已从云端加载 ServerChan 通知配置')
return accounts_str
else:
print('[云端] 配置格式错误: 无法解析账号列表')
return None
except json.JSONDecodeError:
print('[云端] 配置文件不是有效的 JSON 格式')
return None
except requests.exceptions.Timeout:
print('[云端] 请求超时')
return None
except requests.exceptions.RequestException as e:
print(f'[云端] 网络请求失败: {e}')
return None
except Exception as e:
print(f'[云端] 加载失败: {e}')
return None
def load_env_file():
"""加载脚本同目录下的 .env 文件到环境变量"""
env_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), '.env')
if os.path.isfile(env_file):
with open(env_file, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if not line or line.startswith('#') or '=' not in line:
continue
key, _, value = line.partition('=')
key = key.strip()
value = value.strip()
if key and value:
os.environ.setdefault(key, value)
def main():
"""主函数"""
load_env_file()
import pytz
beijing_tz = pytz.timezone('Asia/Shanghai')
execution_time = datetime.now(beijing_tz).strftime("%Y-%m-%d %H:%M:%S")
print('=' * 50)
print('NewAPI 自动签到')
print(f'执行时间: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}')
print('=' * 50)
config_url = os.environ.get('CONFIG_URL', '')
config_auth = os.environ.get('CONFIG_AUTH', '')
accounts_str = ''
if config_url:
accounts_str = load_config_from_cloud(config_url, config_auth) or ''
if not accounts_str:
accounts_str = os.environ.get('NEWAPI_ACCOUNTS', '')
from_env_file = False
if not accounts_str:
env_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), '.env')
if os.path.isfile(env_file):
with open(env_file, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if line.startswith('NEWAPI_ACCOUNTS='):
accounts_str = line[len('NEWAPI_ACCOUNTS='):]
from_env_file = True
break
if not accounts_str:
print('[错误] 未配置账号信息')
print('请设置 CONFIG_URL(云端配置)或 NEWAPI_ACCOUNTS(本地配置)环境变量')
sys.exit(1)
accounts = parse_accounts(accounts_str)
if not accounts:
print('[错误] 账号配置解析失败')
sys.exit(1)
print(f'共 {len(accounts)} 个账号待签到\n')
success_count = 0
fail_count = 0
checkin_results = []
session_updated = False
for i, account in enumerate(accounts, 1):
url = account['url']
session_cookie = account['session']
user_id = account.get('user_id')
cf_clearance = account.get('cf_clearance')
login_username = account.get('login_username')
login_password = account.get('login_password')
name = account.get('name') or f'账号{i}'
print(f'[{i}/{len(accounts)}] {name}')
print(f' 站点: {NewAPICheckin._mask_url(url)}')
if user_id:
print(f' 用户ID: {NewAPICheckin._mask_user_id(user_id)}')
client = NewAPICheckin(url, session_cookie, user_id, cf_clearance, login_username, login_password)
# 获取用户信息(可能触发自动登录)
user_info = client.get_user_info()
if client.session_cookie != session_cookie:
account['session'] = client.session_cookie
session_updated = True
if user_info:
username = user_info.get('username', '未知')
# 用户名也脱敏,只显示前3个字符
masked_username = username[:3] + '***' if len(username) > 3 else '***'
print(f' 用户: {masked_username}')
else:
print(' 用户: 获取失败(可能 session 已过期)')
# 执行签到
result = client.checkin()
if client.session_cookie != session_cookie:
account['session'] = client.session_cookie
session_updated = True
checkin_count = 0 # 默认值,避免历史接口失败时未定义
if result['success']:
success_count += 1
print(f' 结果: ✅ {result["message"]}')
# 显示签到日期
if result['checkin_date']:
print(f' 日期: {result["checkin_date"]}')
# 显示获得的额度(格式化显示)
if result['quota_awarded']:
quota = result['quota_awarded']
# 格式化额度显示
if quota >= 1000000:
quota_str = f'{quota / 1000000:.2f}M'
elif quota >= 1000:
quota_str = f'{quota / 1000:.2f}K'
else:
quota_str = str(quota)
print(f' 奖励: +{quota_str} 额度 ({quota:,} tokens)')
# 获取本月签到统计
history = client.get_checkin_history()
if history and history.get('stats'):
stats = history['stats']
checkin_count = stats.get('checkin_count', 0)
total_quota = stats.get('total_quota', 0)
if total_quota >= 1000000:
total_str = f'{total_quota / 1000000:.2f}M'
elif total_quota >= 1000:
total_str = f'{total_quota / 1000:.2f}K'
else:
total_str = str(total_quota)
print(f' 统计: 本月已签 {checkin_count} 天,累计 {total_str} 额度')
# 抽奖(仅 lanxiu.cc 本地运行,GitHub Actions 跳过 — 绑定映射无法持久化)
lottery_items = []
if 'lanxiu.cc' in url and lottery_run_for_account and not os.environ.get('GITHUB_ACTIONS'):
display_name = (user_info or {}).get('username') or account.get('login_username')
if display_name:
for rnd in range(2):
prize, err = lottery_run_for_account(
client.session, url, display_name)
if err:
lottery_items.append(f'⏭️ {err}')
print(f' 抽奖: ⏭️ {err}')
break
if prize:
q = prize.get('quota_awarded', 0)
qs = f'{q/1000000:.2f}M' if q >= 1000000 else f'{q/1000:.2f}K' if q >= 1000 else str(q)
line = f'🎉 {prize["prize_name"]} +{qs}'
lottery_items.append(line)
print(f' 抽奖: {line}')
if prize.get('remaining_times', 0) <= 0:
break
# 维云翻卡(本地和 GitHub Actions 都运行,最多 3 次)
if 'vsllm.com' in url and run_gwent_for_account:
for rnd in range(3):
prize, err = run_gwent_for_account(client.session, url)
if err:
lottery_items.append(f'⏭️ {err}')
print(f' 翻卡: ⏭️ {err}')
break
if prize:
q = prize.get('quota_awarded', 0)
qs = f'{q/1000000:.2f}M' if q >= 1000000 else f'{q/1000:.2f}K' if q >= 1000 else str(q)
line = f'🎉 第{rnd+1}次 {prize["prize_name"]} +{qs}'
lottery_items.append(line)
print(f' 翻卡: {line}')
# 收集结果用于钉钉通知
account_result = {
'name': name,
'success': True,
'message': result['message'],
'quota_awarded': result.get('quota_awarded'),
'checkin_count': checkin_count,
'lottery': lottery_items
}
checkin_results.append(account_result)
else:
fail_count += 1
print(f' 结果: ❌ {result["message"]}')
# 收集结果用于钉钉通知
message = result.get('message', '')
account_result = {
'name': name,
'success': False,
'message': message,
'session_expired': 'session' in message.lower() or '认证' in message
}
checkin_results.append(account_result)
print()
# 汇总
print('=' * 50)
print(f'签到完成: 成功 {success_count}, 失败 {fail_count}')
print('=' * 50)
# 发送钉钉通知
if send_checkin_notification:
print('正在发送钉钉通知...')
send_checkin_notification(checkin_results, execution_time)
elif os.environ.get('DINGTALK_WEBHOOK'):
print('[警告] 已配置 DINGTALK_WEBHOOK 但无法导入通知模块')
# 发送邮件通知
if send_email_notification and os.environ.get('EMAIL_SMTP_HOST'):
print('正在发送邮件通知...')
send_email_notification(checkin_results, execution_time)
elif os.environ.get('EMAIL_SMTP_HOST'):
print('[警告] 已配置邮件参数但无法导入通知模块')
# 发送 ServerChan 通知
if send_serverchan_notification:
print('正在发送 ServerChan 通知...')
send_serverchan_notification(checkin_results, execution_time)
elif os.environ.get('SERVERCHAN_SENDKEY'):
print('[警告] 已配置 SERVERCHAN_SENDKEY 但无法导入通知模块')
# 回写 .env(仅限从 .env 加载且 session 有更新的本地运行)
if from_env_file and session_updated and not os.environ.get('GITHUB_ACTIONS'):
env_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), '.env')
print('\n[Session] 检测到 session 已更新,正在回写 .env...')
new_accounts_str = json.dumps(accounts, ensure_ascii=False)
with open(env_file, 'r', encoding='utf-8') as f:
content = f.read()
marker = 'NEWAPI_ACCOUNTS='
idx = content.find(marker)
if idx != -1:
line_end = content.find('\n', idx)
if line_end == -1:
line_end = len(content)
new_line = marker + new_accounts_str
new_content = content[:idx] + new_line + content[line_end:]
with open(env_file, 'w', encoding='utf-8') as f:
f.write(new_content)
print('[Session] .env 已更新')
# 如果全部失败则返回错误码
if fail_count == len(accounts):
sys.exit(1)
if __name__ == '__main__':
main()
# === DINGTALK NOTIFICATION PATCH ===
# This section was added to send DingTalk notifications