forked from Jasonliu-0/Newapi-checkin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifier.py
More file actions
334 lines (284 loc) · 12.3 KB
/
Copy pathnotifier.py
File metadata and controls
334 lines (284 loc) · 12.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
# -*- coding: utf-8 -*-
"""
通知推送模块
支持邮件推送和 ServerChan 推送
"""
import os
import json
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from datetime import datetime
from typing import Optional, List, Dict, Any
try:
import requests
except ImportError:
requests = None
def format_quota(quota: int) -> str:
"""格式化额度显示"""
if quota >= 1000000:
return f'{quota / 1000000:.2f}M'
elif quota >= 1000:
return f'{quota / 1000:.2f}K'
else:
return str(quota)
def build_report_text(results: List[Dict[str, Any]], execution_time: str) -> str:
"""构建纯文本报告内容"""
success_list = [r for r in results if r.get('success')]
fail_list = [r for r in results if not r.get('success')]
lines = [
'NewAPI 签到报告',
f'执行时间: {execution_time}',
'',
'-' * 40,
''
]
if success_list:
lines.append(f'✓ 成功 ({len(success_list)}个):')
for r in success_list:
name = r.get('name', '未知账号')
quota = r.get('quota_awarded') or 0
quota_str = f'+{format_quota(quota)}' if quota > 0 else '-'
checkin_count = r.get('checkin_count')
detail = f'已签 {checkin_count} 天' if checkin_count else r.get('message', '成功')
lottery = ' / '.join(r.get('lottery', [])) or '-'
lines.append(f' {name} | {quota_str} | {detail} | {lottery}')
lines.append('')
if fail_list:
lines.append(f'✗ 失败 ({len(fail_list)}个):')
for r in fail_list:
name = r.get('name', '未知账号')
message = r.get('message', '未知错误')
if r.get('session_expired') or 'session' in message.lower() or '认证' in message:
message = f'[Session失效] {message}'
lines.append(f' {name} | {message}')
lines.append('')
lines.append('-' * 40)
total = len(results)
success_count = len(success_list)
fail_count = len(fail_list)
if fail_count == 0:
lines.append(f'汇总: 全部成功 ({success_count}/{total})')
elif success_count == 0:
lines.append(f'汇总: 全部失败 ({fail_count}/{total})')
else:
lines.append(f'汇总: 成功 {success_count},失败 {fail_count}')
expired_accounts = [r for r in fail_list if r.get('session_expired') or
'session' in r.get('message', '').lower() or
'认证' in r.get('message', '')]
if expired_accounts:
lines.append('')
lines.append('注意: 部分账号 Session 已失效,请及时更新 Cookie!')
return '\n'.join(lines)
def build_report_html(results: List[Dict[str, Any]], execution_time: str) -> str:
"""构建 HTML 格式报告"""
success_list = [r for r in results if r.get('success')]
fail_list = [r for r in results if not r.get('success')]
rows = ''
if success_list:
rows += '<tr><td colspan="3" style="background:#f0fff4;font-weight:bold;padding:6px 10px;">'
rows += f'✅ 成功 ({len(success_list)}个)</td></tr>'
for r in success_list:
name = r.get('name', '未知账号')
quota = r.get('quota_awarded') or 0
quota_str = f'+{format_quota(quota)}' if quota > 0 else '-'
checkin_count = r.get('checkin_count')
detail = f'已签 {checkin_count} 天' if checkin_count else r.get('message', '成功')
lottery = ' / '.join(r.get('lottery', [])) or '-'
rows += f'<tr><td style="padding:4px 10px;">{name}</td>'
rows += f'<td style="padding:4px 10px;">{quota_str}</td>'
rows += f'<td style="padding:4px 10px;">{detail}</td>'
rows += f'<td style="padding:4px 10px;">{lottery}</td></tr>'
if fail_list:
rows += '<tr><td colspan="4" style="background:#fff5f5;font-weight:bold;padding:6px 10px;">'
rows += f'❌ 失败 ({len(fail_list)}个)</td></tr>'
for r in fail_list:
name = r.get('name', '未知账号')
message = r.get('message', '未知错误')
if r.get('session_expired') or 'session' in message.lower() or '认证' in message:
message = f'⚠️ {message}'
rows += f'<tr><td style="padding:4px 10px;">{name}</td>'
rows += f'<td colspan="2" style="padding:4px 10px;color:#e53e3e;">{message}</td></tr>'
total = len(results)
success_count = len(success_list)
fail_count = len(fail_list)
if fail_count == 0:
summary = f'✅ 全部成功 ({success_count}/{total})'
elif success_count == 0:
summary = f'❌ 全部失败 ({fail_count}/{total})'
else:
summary = f'成功 {success_count},失败 {fail_count}'
html = f'''<!DOCTYPE html>
<html>
<head><meta charset="utf-8"></head>
<body style="font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif;padding:20px;color:#333;">
<div style="max-width:600px;margin:0 auto;background:#fff;border:1px solid #e2e8f0;border-radius:8px;overflow:hidden;">
<div style="background:#667eea;padding:16px 20px;">
<h2 style="margin:0;color:#fff;font-size:18px;">📋 NewAPI 签到报告</h2>
</div>
<div style="padding:16px 20px;">
<p style="color:#718096;font-size:14px;margin:0 0 12px 0;">执行时间: {execution_time}</p>
<table style="width:100%;border-collapse:collapse;font-size:14px;">
{rows}
</table>
<hr style="border:none;border-top:1px solid #e2e8f0;margin:12px 0;">
<p style="font-size:14px;margin:0;"><strong>{summary}</strong></p>
'''
expired_accounts = [r for r in fail_list if r.get('session_expired') or
'session' in r.get('message', '').lower() or
'认证' in r.get('message', '')]
if expired_accounts:
html += '<p style="color:#e53e3e;font-size:13px;margin:8px 0 0 0;">⚠️ 部分账号 Session 已失效,请及时更新 Cookie!</p>'
html += '''
</div>
</div>
</body>
</html>'''
return html
def send_email_notification(results: List[Dict[str, Any]], execution_time: Optional[str] = None) -> bool:
"""
发送邮件通知
环境变量配置:
EMAIL_SMTP_HOST: SMTP 服务器地址
EMAIL_SMTP_PORT: SMTP 服务器端口 (默认 465)
EMAIL_USER: SMTP 用户名
EMAIL_PASS: SMTP 密码/授权码
EMAIL_TO: 收件人地址
EMAIL_FROM: 发件人地址 (可选,默认与 EMAIL_USER 相同)
"""
host = os.environ.get('EMAIL_SMTP_HOST', '')
port_str = os.environ.get('EMAIL_SMTP_PORT', '465')
port = int(port_str) if port_str else 465
user = os.environ.get('EMAIL_USER', '')
password = os.environ.get('EMAIL_PASS', '')
to_addr = os.environ.get('EMAIL_TO', '')
from_addr = os.environ.get('EMAIL_FROM', user)
if not all([host, user, password, to_addr]):
print('[邮件通知] 未完整配置邮件参数 (EMAIL_SMTP_HOST/USER/PASS/TO),跳过通知')
return False
if not execution_time:
execution_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
success_count = len([r for r in results if r.get('success')])
fail_count = len([r for r in results if not r.get('success')])
# 构建邮件标题
if fail_count == 0:
subject = f'✅ NewAPI 签到成功 ({success_count}个账号)'
elif success_count == 0:
subject = f'❌ NewAPI 签到失败 ({fail_count}个账号)'
else:
subject = f'📋 NewAPI 签到完成 (成功{success_count}/失败{fail_count})'
text_content = build_report_text(results, execution_time)
html_content = build_report_html(results, execution_time)
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = from_addr
msg['To'] = to_addr
msg.attach(MIMEText(text_content, 'plain', 'utf-8'))
msg.attach(MIMEText(html_content, 'html', 'utf-8'))
try:
if port == 465:
server = smtplib.SMTP_SSL(host, port, timeout=30)
else:
server = smtplib.SMTP(host, port, timeout=30)
server.starttls()
server.login(user, password)
server.sendmail(from_addr, [to_addr], msg.as_string())
server.quit()
print(f'[邮件通知] 发送成功 -> {to_addr}')
return True
except Exception as e:
print(f'[邮件通知] 发送失败: {e}')
return False
def send_serverchan_notification(results: List[Dict[str, Any]], execution_time: Optional[str] = None) -> bool:
"""
发送 ServerChan 推送
环境变量配置:
SERVERCHAN_SENDKEY: ServerChan SendKey (https://sctapi.ftqq.com/SENDKEY.send)
"""
send_key = os.environ.get('SERVERCHAN_SENDKEY', '')
if not send_key:
print('[ServerChan] 未配置 SERVERCHAN_SENDKEY,跳过通知')
return False
if requests is None:
print('[ServerChan] 错误: 未安装 requests 库')
return False
if not execution_time:
execution_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
success_count = len([r for r in results if r.get('success')])
fail_count = len([r for r in results if not r.get('success')])
# 构建标题
if fail_count == 0:
title = f'✅ NewAPI 签到成功 ({success_count}个账号)'
elif success_count == 0:
title = f'❌ NewAPI 签到失败 ({fail_count}个账号)'
else:
title = f'📋 NewAPI 签到完成 (成功{success_count}/失败{fail_count})'
# 构建 Markdown 内容
lines = []
# 头部
lines.append(f'**执行时间**: {execution_time}')
lines.append('')
# 成功列表
success_list = [r for r in results if r.get('success')]
if success_list:
lines.append(f'### ✅ 成功 ({len(success_list)}个)')
lines.append('')
lines.append('| 账号 | 奖励 | 结果 | 已签 | 抽奖 |')
lines.append('|------|------|------|------|------|')
for r in success_list:
name = r.get('name', '未知账号')
quota = r.get('quota_awarded') or 0
quota_str = f'+{format_quota(quota)}' if quota > 0 else '-'
message = r.get('message', '成功')
checkin_count = r.get('checkin_count')
days = f'已签 {checkin_count} 天' if checkin_count else '-'
lottery = ' / '.join(r.get('lottery', [])) or '-'
lines.append(f'| {name} | {quota_str} | {message} | {days} | {lottery} |')
lines.append('')
# 失败列表
fail_list_local = [r for r in results if not r.get('success')]
if fail_list_local:
lines.append(f'### ❌ 失败 ({len(fail_list_local)}个)')
lines.append('')
lines.append('| 账号 | 原因 |')
lines.append('|------|------|')
for r in fail_list_local:
name = r.get('name', '未知账号')
message = r.get('message', '未知错误')
if r.get('session_expired') or 'session' in message.lower() or '认证' in message:
message = f'⚠️ {message}'
lines.append(f'| {name} | {message} |')
lines.append('')
# 汇总
total = len(results)
if fail_count == 0:
lines.append(f'**汇总**: 全部成功 ✨ ({success_count}/{total})')
elif success_count == 0:
lines.append(f'**汇总**: 全部失败 ⚠️ ({fail_count}/{total})')
else:
lines.append(f'**汇总**: 成功 {success_count},失败 {fail_count}')
expired_accounts = [r for r in results if not r.get('success') and (
r.get('session_expired') or
'session' in r.get('message', '').lower() or
'认证' in r.get('message', '')
)]
if expired_accounts:
lines.append('')
lines.append('> ⚠️ **注意**: 部分账号 Session 已失效,请及时更新 Cookie!')
content = '\n'.join(lines)
url = f'https://sctapi.ftqq.com/{send_key}.send'
try:
resp = requests.post(url, data={
'title': title,
'desp': content
}, timeout=15)
result = resp.json()
if result.get('code') == 0:
print(f'[ServerChan] 推送成功')
return True
else:
print(f'[ServerChan] 推送失败: {result.get("message", "未知错误")}')
return False
except Exception as e:
print(f'[ServerChan] 推送异常: {e}')
return False