forked from Womsxd/MihoyoBBSTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdacapo_main.py
More file actions
518 lines (456 loc) · 20.5 KB
/
Copy pathdacapo_main.py
File metadata and controls
518 lines (456 loc) · 20.5 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
import os
import sys
import json
import yaml
import tempfile
from pathlib import Path
from typing import Dict, Any, List
from configparser import ConfigParser
def setup_logging():
config_dir = Path(__file__).parent / "config"
logging_ini = config_dir / "logging.ini"
logging_example = config_dir / "logging.ini.example"
if not logging_ini.exists() and logging_example.exists():
config_parser = ConfigParser(interpolation=None)
config_parser.read(logging_example, encoding='utf-8')
if config_parser.has_section('formatter_simpleFormatter'):
config_parser.set('formatter_simpleFormatter', 'format', '%(asctime)s - %(levelname)s - %(message)s')
config_parser.set('formatter_simpleFormatter', 'datefmt', '%Y-%m-%d %H:%M:%S')
with open(logging_ini, 'w', encoding='utf-8') as f:
config_parser.write(f)
print(f"已创建日志配置文件: {logging_ini}")
elif not logging_ini.exists():
print("未找到 logging.ini.example,将使用默认日志配置")
setup_logging()
import config
import main
from loghelper import log
class DaCapoAdapter:
"""DaCapo 适配器,负责将 DaCapo 配置转换为原生配置并执行任务"""
def __init__(self, config_path: str):
self.config_path = config_path
self.dacapo_config = self._load_dacapo_config()
def _load_dacapo_config(self) -> Dict[str, Any]:
"""加载 DaCapo 配置文件"""
try:
with open(self.config_path, 'r', encoding='utf-8') as f:
return json.load(f)
except Exception as e:
log.error(f"加载 DaCapo 配置文件失败: {e}")
sys.exit(1)
def _convert_text_to_list(self, text: str) -> List[str]:
"""转换文本为字符串列表"""
return [item.strip() for item in (text or "").split(',') if item.strip()]
def _convert_black_list(self, text: str) -> List[str]:
"""转换黑名单文本为列表"""
return self._convert_text_to_list(text)
def _convert_checkin_list(self, text: str) -> List[int]:
"""转换签到列表文本为整数列表"""
if not text or not text.strip():
return [5, 2]
try:
return [int(item.strip()) for item in text.split(',') if item.strip()]
except ValueError:
log.warning("签到列表格式错误,使用默认值")
return [5, 2]
def _convert_activities_list(self, text: str) -> List[str]:
"""转换活动列表文本为列表"""
return self._convert_text_to_list(text)
def validate_config(self) -> tuple:
"""验证配置的有效性"""
project_config = self.dacapo_config.get("Project", {}).get("General", {})
task_config = self.dacapo_config.get("日常", {}).get("米游社", {})
cookie = project_config.get("账号配置", {}).get("米游社Cookie", "").strip()
if not cookie:
return False, "米游社Cookie不能为空"
checkin_list = task_config.get("米游社BBS", {}).get("签到版块列表", "")
try:
self._convert_checkin_list(checkin_list)
except:
return False, "签到版块列表格式错误,应为用逗号分隔的数字"
retries = task_config.get("国服游戏", {}).get("重试次数", "3")
try:
int(retries)
except ValueError:
return False, "重试次数必须是数字"
return True, "配置验证通过"
def convert_to_native_config(self) -> Dict[str, Any]:
"""将 DaCapo 配置转换为项目原生配置"""
project_config = self.dacapo_config.get("Project", {}).get("General", {})
task_config = self.dacapo_config.get("日常", {}).get("米游社", {})
native_config = {
"enable": True,
"version": 15,
"push": ""
}
account_group = project_config.get("账号配置", {})
native_config["account"] = {
"cookie": account_group.get("米游社Cookie", ""),
"stuid": account_group.get("stuid", ""),
"stoken": account_group.get("stoken", ""),
"mid": account_group.get("mid", "")
}
device_group = project_config.get("设备信息", {})
native_config["device"] = {
"name": device_group.get("设备名称", "Xiaomi MI 6"),
"model": device_group.get("设备型号", "Mi 6"),
"id": device_group.get("设备ID", ""),
"fp": device_group.get("设备指纹", "")
}
bbs_group = task_config.get("米游社BBS", {})
checkin_list_text = bbs_group.get("签到版块列表", "5,2")
native_config["mihoyobbs"] = {
"enable": bbs_group.get("启用米游社签到", True),
"checkin": bbs_group.get("启用版块签到", True),
"checkin_list": self._convert_checkin_list(checkin_list_text),
"read": bbs_group.get("启用看帖", True),
"like": bbs_group.get("启用点赞", True),
"cancel_like": bbs_group.get("启用取消点赞", True),
"share": bbs_group.get("启用分享", True)
}
cn_games_group = task_config.get("国服游戏", {})
native_config["games"] = {
"cn": {
"enable": cn_games_group.get("启用国服签到", True),
"useragent": cn_games_group.get("User Agent",
"Mozilla/5.0 (Linux; Android 12; Unspecified Device) AppleWebKit/537.36 (KHTML, like Gecko) "
"Version/4.0 Chrome/103.0.5060.129 Mobile Safari/537.36"),
"retries": int(cn_games_group.get("重试次数", 3)),
"genshin": {
"checkin": cn_games_group.get("原神签到", True),
"black_list": self._convert_black_list(cn_games_group.get("原神黑名单", ""))
},
"honkai2": {
"checkin": cn_games_group.get("崩坏2签到", False),
"black_list": self._convert_black_list(cn_games_group.get("崩坏2黑名单", ""))
},
"honkai3rd": {
"checkin": cn_games_group.get("崩坏3签到", False),
"black_list": self._convert_black_list(cn_games_group.get("崩坏3黑名单", ""))
},
"tears_of_themis": {
"checkin": cn_games_group.get("未定事件簿签到", False),
"black_list": self._convert_black_list(cn_games_group.get("未定事件簿黑名单", ""))
},
"honkai_sr": {
"checkin": cn_games_group.get("星穹铁道签到", False),
"black_list": self._convert_black_list(cn_games_group.get("星穹铁道黑名单", ""))
},
"zzz": {
"checkin": cn_games_group.get("绝区零签到", False),
"black_list": self._convert_black_list(cn_games_group.get("绝区零黑名单", ""))
}
},
"os": {
"enable": False,
"cookie": "",
"lang": "zh-cn",
"genshin": {"checkin": False, "black_list": []},
"honkai3rd": {"checkin": False, "black_list": []},
"tears_of_themis": {"checkin": False, "black_list": []},
"honkai_sr": {"checkin": False, "black_list": []},
"zzz": {"checkin": False, "black_list": []}
}
}
os_games_group = task_config.get("国际服游戏", {})
if os_games_group.get("启用国际服签到", False):
native_config["games"]["os"] = {
"enable": True,
"cookie": os_games_group.get("国际服Cookie", ""),
"lang": os_games_group.get("语言设置", "zh-cn"),
"genshin": {
"checkin": os_games_group.get("国际服原神签到", False),
"black_list": self._convert_black_list(os_games_group.get("国际服原神黑名单", ""))
},
"honkai3rd": {
"checkin": os_games_group.get("国际服崩坏3签到", False),
"black_list": self._convert_black_list(os_games_group.get("国际服崩坏3黑名单", ""))
},
"tears_of_themis": {
"checkin": os_games_group.get("国际服未定事件簿签到", False),
"black_list": self._convert_black_list(os_games_group.get("国际服未定事件簿黑名单", ""))
},
"honkai_sr": {
"checkin": os_games_group.get("国际服星穹铁道签到", False),
"black_list": self._convert_black_list(os_games_group.get("国际服星穹铁道黑名单", ""))
},
"zzz": {
"checkin": os_games_group.get("国际服绝区零签到", False),
"black_list": self._convert_black_list(os_games_group.get("国际服绝区零黑名单", ""))
}
}
cloud_games_group = task_config.get("云游戏", {})
native_config["cloud_games"] = {
"cn": {
"enable": cloud_games_group.get("启用云游戏签到", False),
"genshin": {
"enable": cloud_games_group.get("启用云原神", False),
"token": cloud_games_group.get("云原神Token", "")
},
"zzz": {
"enable": cloud_games_group.get("启用云绝区零", False),
"token": cloud_games_group.get("云绝区零Token", "")
}
},
"os": {
"enable": cloud_games_group.get("启用国际服云游戏", False),
"lang": cloud_games_group.get("国际服云游戏语言", "zh-cn"),
"genshin": {
"enable": cloud_games_group.get("启用国际服云原神", False),
"token": cloud_games_group.get("国际服云原神Token", "")
}
}
}
native_config["competition"] = {
"enable": False,
"genius_invokation": {
"enable": False,
"account": [],
"checkin": False,
"weekly": False
}
}
web_activity_group = task_config.get("网页活动", {})
activities_text = web_activity_group.get("活动列表", "")
native_config["web_activity"] = {
"enable": web_activity_group.get("启用网页活动", False),
"activities": self._convert_activities_list(activities_text)
}
return native_config
def create_push_config(self, project_config: Dict[str, Any]) -> str:
"""创建推送配置文件"""
push_group = project_config.get("推送设置", {})
if not push_group.get("启用推送", False):
return None
try:
temp_fd, temp_path = tempfile.mkstemp(suffix='.ini', prefix='dacapo_push_')
os.close(temp_fd)
push_config = ConfigParser()
push_service = push_group.get("推送服务", "pushplus")
push_token = push_group.get("推送Token", "")
error_only = push_group.get("仅错误时推送", False)
topic = push_group.get("推送群组", "")
block_keys = push_group.get("屏蔽关键词", "")
push_config.add_section('setting')
push_config.set('setting', 'enable', 'true')
push_config.set('setting', 'push_server', push_service)
push_config.set('setting', 'push_token', push_token)
push_config.set('setting', 'push_block_keys', block_keys)
push_config.set('setting', 'error_push_only', str(error_only).lower())
if topic:
push_config.set('setting', 'topic', topic)
service_configs = {
"telegram": {
"section": "telegram",
"config": {
"api_url": "api.telegram.org",
"bot_token": push_token,
"chat_id": ""
}
},
"wecom": {
"section": "wecom",
"config": {
"wechat_id": "",
"agentid": "",
"secret": push_token,
"touser": "@all"
}
},
"dingrobot": {
"section": "dingrobot",
"config": {
"webhook": push_token,
"secret": ""
}
},
"feishubot": {
"section": "feishubot",
"config": {
"webhook": push_token
}
},
"bark": {
"section": "bark",
"config": {
"api_url": "https://api.day.app",
"token": push_token,
"icon": "genshin"
}
},
"pushdeer": {
"section": "pushdeer",
"config": {
"api_url": "https://api2.pushdeer.com",
"token": push_token
}
},
"gotify": {
"section": "gotify",
"config": {
"api_url": "",
"token": push_token,
"priority": "5"
}
},
"smtp": {
"section": "smtp",
"config": {
"mailhost": "",
"port": "587",
"fromaddr": "",
"toaddr": "",
"username": "",
"password": push_token,
"subject": "米游社签到脚本",
"ssl_enable": "true",
"background": "true"
}
},
"webhook": {
"section": "webhook",
"config": {
"webhook_url": push_token
}
},
"qmsg": {
"section": "qmsg",
"config": {
"key": push_token
}
},
"discord": {
"section": "discord",
"config": {
"webhook": push_token
}
},
"wxpusher": {
"section": "wxpusher",
"config": {
"app_token": push_token,
"uids": "",
"topic_ids": topic
}
},
"serverchan3": {
"section": "serverchan3",
"config": {
"sendkey": push_token,
"tags": ""
}
},
"pushme": {
"section": "pushme",
"config": {
"token": push_token,
"url": "https://push.i-i.me/"
}
},
"cqhttp": {
"section": "cqhttp",
"config": {
"cqhttp_url": "http://127.0.0.1:5000/send_msg",
"cqhttp_qq": "10001",
"cqhttp_group": "10002"
}
}
}
if push_service in service_configs:
service_conf = service_configs[push_service]
push_config.add_section(service_conf["section"])
for key, value in service_conf["config"].items():
if value:
push_config.set(service_conf["section"], key, str(value))
with open(temp_path, 'w', encoding='utf-8') as f:
push_config.write(f)
return temp_path
except Exception as e:
log.error(f"创建推送配置文件失败: {e}")
return None
def create_temp_config(self, native_config: Dict[str, Any]) -> str:
"""创建临时配置文件"""
try:
temp_fd, temp_path = tempfile.mkstemp(suffix='.yaml', prefix='dacapo_config_')
with os.fdopen(temp_fd, 'w', encoding='utf-8') as f:
yaml.dump(native_config, f, default_flow_style=False,
allow_unicode=True, sort_keys=False)
return temp_path
except Exception as e:
log.error(f"创建临时配置文件失败: {e}")
sys.exit(1)
def run_task(self):
"""执行任务"""
temp_push_path = None
temp_config_path = None
try:
log.info("开始执行 DaCapo 适配任务")
log.info("验证配置...")
is_valid, validation_msg = self.validate_config()
if not is_valid:
log.error(f"配置验证失败: {validation_msg}")
return 1
log.info("转换配置...")
native_config = self.convert_to_native_config()
project_config = self.dacapo_config.get("Project", {}).get("General", {})
temp_push_path = self.create_push_config(project_config)
if temp_push_path:
log.info("推送配置已创建")
os.environ["AutoMihoyoBBS_push_path"] = os.path.dirname(temp_push_path)
os.environ["AutoMihoyoBBS_push_name"] = os.path.basename(temp_push_path)
log.info("创建临时配置文件...")
temp_config_path = self.create_temp_config(native_config)
config.config_Path = temp_config_path
log.info("执行米游社签到任务...")
status_code, message = main.main()
if temp_push_path:
log.info("推送执行结果...")
import push
push.push(status_code, message)
log.info("=" * 50)
log.info("任务执行完成!")
log.info(f"状态码: {status_code}")
log.info("执行结果:")
for line in message.split('\n'):
if line.strip():
log.info(line)
log.info("=" * 50)
return status_code
except Exception as e:
log.error(f"执行任务失败: {e}")
import traceback
log.error("异常详情:")
for line in traceback.format_exc().split('\n'):
if line.strip():
log.error(line)
return 1
finally:
# 清理临时配置文件
if temp_config_path and os.path.exists(temp_config_path):
try:
os.unlink(temp_config_path)
log.debug("临时配置文件已清理")
except Exception as cleanup_error:
log.warning(f"清理临时文件失败: {cleanup_error}")
# 清理推送配置文件和环境变量
if temp_push_path and os.path.exists(temp_push_path):
try:
os.unlink(temp_push_path)
except Exception as cleanup_error:
log.warning(f"清理推送配置文件失败: {cleanup_error}")
for env_key in ("AutoMihoyoBBS_push_path", "AutoMihoyoBBS_push_name"):
if env_key in os.environ:
del os.environ[env_key]
def run():
"""主函数"""
if len(sys.argv) < 2:
log.error("用法: python dacapo_main.py <配置文件路径>")
sys.exit(1)
config_path = sys.argv[1]
if not os.path.exists(config_path):
log.error(f"配置文件不存在: {config_path}")
sys.exit(1)
adapter = DaCapoAdapter(config_path)
adapter.run_task()
if __name__ == "__main__":
run()