-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathmain.py
576 lines (492 loc) · 20 KB
/
main.py
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
import logging
import os
import json
import argparse
import random
from datetime import datetime, timedelta
from typing import Dict, List, Optional, Any
import concurrent.futures
from coreApi.MainLogicApi import ApiClient
from coreApi.AiServiceClient import generate_article
from util.Config import ConfigManager
from util.MessagePush import MessagePusher
from util.HelperFunctions import desensitize_name, is_holiday
from util.FileUploader import upload_img
logging.basicConfig(
format="[%(asctime)s] %(name)s %(levelname)s: %(message)s",
level=logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S",
)
logger = logging.getLogger(__name__)
USER_DIR = os.path.join(os.path.dirname(__file__), "user")
def perform_clock_in(api_client: ApiClient, config: ConfigManager) -> Dict[str, Any]:
"""
执行打卡操作。
Args:
api_client (ApiClient): ApiClient 实例。
config (ConfigManager): 配置管理器。
Returns:
Dict[str, Any]: 执行结果。
"""
try:
current_time = datetime.now()
current_hour = current_time.hour
# 确定打卡类型
if current_hour < 12:
checkin_type = "START"
display_type = "上班"
else:
checkin_type = "END"
display_type = "下班"
# 判断是否为节假日模式并跳过打卡
if config.get_value("config.clockIn.mode") == "holiday" and is_holiday():
if not config.get_value("config.clockIn.specialClockIn"):
return {
"status": "skip",
"message": "今天是休息日,已跳过打卡",
"task_type": "打卡",
}
checkin_type = "HOLIDAY"
display_type = "休息/节假日"
# 判断自定义打卡日期模式并跳过打卡
elif config.get_value("config.clockIn.mode") == "custom":
today = datetime.today().weekday() + 1 # 获取星期几(1-7)
if today not in config.get_value("config.clockIn.customDays"):
if not config.get_value("config.clockIn.specialClockIn"):
return {
"status": "skip",
"message": "今天不在设置打卡时间范围内,已跳过打卡",
"task_type": "打卡",
}
checkin_type = "HOLIDAY"
display_type = "休息/节假日"
last_checkin_info = api_client.get_checkin_info()
# 检查是否已经打过卡
if last_checkin_info and last_checkin_info["type"] == checkin_type:
last_checkin_time = datetime.strptime(
last_checkin_info["createTime"], "%Y-%m-%d %H:%M:%S"
)
if last_checkin_time.date() == current_time.date():
logger.info(f"今日 {display_type} 卡已打,无需重复打卡")
return {
"status": "skip",
"message": f"今日 {display_type} 卡已打,无需重复打卡",
"task_type": "打卡",
}
user_name = desensitize_name(config.get_value("userInfo.nikeName"))
logger.info(f"用户 {user_name} 开始 {display_type} 打卡")
# 打卡图片和备注
attachments = upload_img(
api_client.get_upload_token(),
config.get_value("userInfo.orgJson.snowFlakeId"),
config.get_value("userInfo.userId"),
config.get_value("config.clockIn.imageCount"),
)
description = (
random.choice(config.get_value("config.clockIn.description"))
if config.get_value("config.clockIn.description")
else None
)
# 设置打卡信息
checkin_info = {
"type": checkin_type,
"lastDetailAddress": last_checkin_info.get("address"),
"attachments": attachments or None,
"description": description,
}
api_client.submit_clock_in(checkin_info)
logger.info(f"用户 {user_name} {display_type} 打卡成功")
return {
"status": "success",
"message": f"{display_type}打卡成功",
"task_type": "打卡",
"details": {
"姓名": config.get_value("userInfo.nikeName"),
"打卡类型": display_type,
"打卡时间": current_time.strftime("%Y-%m-%d %H:%M:%S"),
"打卡地点": config.get_value("config.clockIn.location.address"),
},
}
except Exception as e:
logger.error(f"打卡失败: {e}")
return {"status": "fail", "message": f"打卡失败: {str(e)}", "task_type": "打卡"}
def submit_daily_report(api_client: ApiClient, config: ConfigManager) -> Dict[str, Any]:
"""
提交日报。
Args:
api_client (ApiClient): ApiClient 实例。
config (ConfigManager): 配置管理器。
Returns:
Dict[str, Any]: 执行结果。
"""
if not config.get_value("config.reportSettings.daily.enabled"):
logger.info("用户未开启日报提交功能,跳过日报提交任务")
return {
"status": "skip",
"message": "用户未开启日报提交功能",
"task_type": "日报提交",
}
current_time = datetime.now()
if not (current_time.hour >= 12):
logger.info("未到日报提交时间(需12点后)")
return {
"status": "skip",
"message": "未到日报提交时间(需12点后)",
"task_type": "日报提交",
}
try:
# 获取历史提交记录
submitted_reports_info = api_client.get_submitted_reports_info("day")
submitted_reports = submitted_reports_info.get("data", [])
# 检查是否已经提交过今天的日报
if submitted_reports:
last_report = submitted_reports[0]
last_submit_time = datetime.strptime(
last_report["createTime"], "%Y-%m-%d %H:%M:%S"
)
if last_submit_time.date() == current_time.date():
logger.info("今天已经提交过日报,跳过本次提交")
return {
"status": "skip",
"message": "今天已经提交过日报",
"task_type": "日报提交",
}
job_info = api_client.get_job_info()
report_count = submitted_reports_info.get("flag", 0) + 1
content = generate_article(config, f"第{report_count}天日报", job_info)
# 上传图片并获取附件
attachments = upload_img(
api_client.get_upload_token(),
config.get_value("userInfo.orgJson.snowFlakeId"),
config.get_value("userInfo.userId"),
config.get_value("config.reportSettings.daily.imageCount"),
)
report_info = {
"title": f"第{report_count}天日报",
"content": content,
"attachments": attachments,
"reportType": "day",
"jobId": job_info.get("jobId", None),
"reportTime": current_time.strftime("%Y-%m-%d %H:%M:%S"),
"formFieldDtoList": api_client.get_from_info(7),
}
api_client.submit_report(report_info)
logger.info(
f"第{report_count}天日报已提交,提交时间:{current_time.strftime('%Y-%m-%d %H:%M:%S')}"
)
return {
"status": "success",
"message": f"第{report_count}天日报已提交",
"task_type": "日报提交",
"details": {
"日报标题": f"第{report_count}天日报",
"提交时间": current_time.strftime("%Y-%m-%d %H:%M:%S"),
"附件": attachments,
},
"report_content": content,
}
except Exception as e:
logger.error(f"日报提交失败: {e}")
return {
"status": "fail",
"message": f"日报提交失败: {str(e)}",
"task_type": "日报提交",
}
def submit_weekly_report(
config: ConfigManager, api_client: ApiClient
) -> Dict[str, Any]:
"""提交周报
Args:
config (ConfigManager): 配置管理器。
api_client (ApiClient): ApiClient 实例。
Returns:
Dict[str, Any]: 执行结果。
"""
if not config.get_value("config.reportSettings.weekly.enabled"):
logger.info("用户未开启周报提交功能,跳过周报提交任务")
return {
"status": "skip",
"message": "用户未开启周报提交功能",
"task_type": "周报提交",
}
current_time = datetime.now()
submit_day = config.get_value("config.reportSettings.weekly.submitTime")
if current_time.weekday() + 1 != submit_day or not (current_time.hour >= 12):
logger.info("未到周报提交时间")
return {
"status": "skip",
"message": "未到周报提交时间",
"task_type": "周报提交",
}
try:
# 获取当前周信息
current_week_info = api_client.get_weeks_date()[0]
# 获取历史提交记录
submitted_reports_info = api_client.get_submitted_reports_info("week")
submitted_reports = submitted_reports_info.get("data", [])
# 获取当前周数
week = submitted_reports_info.get("flag", 0) + 1
current_week_string = f"第{week}周"
# 检查是否已经提交过本周的周报
if submitted_reports:
last_report = submitted_reports[0]
if last_report.get("weeks") == current_week_string:
logger.info("本周已经提交过周报,跳过本次提交")
return {
"status": "skip",
"message": "本周已经提交过周报",
"task_type": "周报提交",
}
job_info = api_client.get_job_info()
content = generate_article(config, f"第{week}周周报", job_info)
# 上传图片并获取附件
attachments = upload_img(
api_client.get_upload_token(),
config.get_value("userInfo.orgJson.snowFlakeId"),
config.get_value("userInfo.userId"),
config.get_value("config.reportSettings.weekly.imageCount"),
)
report_info = {
"title": f"第{week}周周报",
"content": content,
"attachments": attachments,
"reportType": "week",
"endTime": current_week_info.get("endTime"),
"startTime": current_week_info.get("startTime"),
"jobId": job_info.get("jobId", None),
"weeks": current_week_string,
"formFieldDtoList": api_client.get_from_info(8),
}
api_client.submit_report(report_info)
logger.info(
f"第{week}周周报已提交,开始时间:{current_week_info.get('startTime')},结束时间:{current_week_info.get('endTime')}"
)
return {
"status": "success",
"message": f"第{week}周周报已提交",
"task_type": "周报提交",
"details": {
"周报标题": f"第{week}周周报",
"开始时间": current_week_info.get("startTime"),
"结束时间": current_week_info.get("endTime"),
"附件": attachments,
},
"report_content": content,
}
except Exception as e:
logger.error(f"周报提交失败: {e}")
return {
"status": "fail",
"message": f"周报提交失败: {str(e)}",
"task_type": "周报提交",
}
def submit_monthly_report(
config: ConfigManager, api_client: ApiClient
) -> Dict[str, Any]:
"""提交月报
Args:
config (ConfigManager): 配置管理器。
api_client (ApiClient): ApiClient 实例。
Returns:
Dict[str, Any]: 执行结果。
"""
if not config.get_value("config.reportSettings.monthly.enabled"):
logger.info("用户未开启月报提交功能,跳过月报提交任务")
return {
"status": "skip",
"message": "用户未开启月报提交功能",
"task_type": "月报提交",
}
current_time = datetime.now()
last_day_of_month = (current_time.replace(day=1) + timedelta(days=32)).replace(
day=1
) - timedelta(days=1)
submit_day = config.get_value("config.reportSettings.monthly.submitTime")
if current_time.day != min(submit_day, last_day_of_month.day) or not (
current_time.hour >= 12
):
logger.info("未到月报提交时间")
return {
"status": "skip",
"message": "未到月报提交时间",
"task_type": "月报提交",
}
try:
# 获取当前年月
current_yearmonth = current_time.strftime("%Y-%m")
# 获取历史提交记录
submitted_reports_info = api_client.get_submitted_reports_info("month")
submitted_reports = submitted_reports_info.get("data", [])
# 检查是否已经提交过本月的月报
if submitted_reports:
last_report = submitted_reports[0]
if last_report.get("yearmonth") == current_yearmonth:
logger.info("本月已经提交过月报,跳过本次提交")
return {
"status": "skip",
"message": "本月已经提交过月报",
"task_type": "月报提交",
}
job_info = api_client.get_job_info()
month = submitted_reports_info.get("flag", 0) + 1
content = generate_article(config, f"第{month}月月报", job_info)
# 上传图片并获取附件
attachments = upload_img(
api_client.get_upload_token(),
config.get_value("userInfo.orgJson.snowFlakeId"),
config.get_value("userInfo.userId"),
config.get_value("config.reportSettings.monthly.imageCount"),
)
report_info = {
"title": f"第{month}月月报",
"content": content,
"attachments": attachments,
"yearmonth": current_yearmonth,
"reportType": "month",
"jobId": job_info.get("jobId", None),
"formFieldDtoList": api_client.get_from_info(9),
}
api_client.submit_report(report_info)
logger.info(f"第{month}月月报已提交,提交月份:{current_yearmonth}")
return {
"status": "success",
"message": f"第{month}月月报已提交",
"task_type": "月报提交",
"details": {
"月报标题": f"第{month}月月报",
"提交月份": current_yearmonth,
"附件": attachments,
},
"report_content": content,
}
except Exception as e:
logger.error(f"月报提交失败: {e}")
return {
"status": "fail",
"message": f"月报提交失败: {str(e)}",
"task_type": "月报提交",
}
def run(config: ConfigManager) -> None:
"""
执行所有任务。
Args:
config (ConfigManager): 配置管理器。
"""
results: List[Dict[str, Any]] = []
try:
pusher = MessagePusher(config.get_value("config.pushNotifications"))
except Exception as e:
logger.error(f"获取消息推送客户端失败: {str(e)}")
return
try:
api_client = ApiClient(config)
# 检查是否登录
if not config.get_value("userInfo.token"):
api_client.login()
logger.info("获取用户信息成功")
# 检查用户类型和计划信息
if config.get_value("userInfo.userType") == "teacher":
logger.info("用户身份为教师,跳过计划信息检查")
elif not config.get_value("planInfo.planId"):
api_client.fetch_internship_plan()
logger.info("已获取实习计划信息")
except Exception as e:
error_message = f"获取API客户端失败: {str(e)}"
logger.error(error_message)
results.append(
{"status": "fail", "message": error_message, "task_type": "API客户端初始化"}
)
pusher.push(results)
logger.info("任务异常结束")
return
logger.info(f"开始执行:{desensitize_name(config.get_value('userInfo.nikeName'))}")
try:
results = [
perform_clock_in(api_client, config),
submit_daily_report(api_client, config),
submit_weekly_report(config, api_client),
submit_monthly_report(config, api_client),
]
except Exception as e:
error_message = f"执行任务时发生错误: {str(e)}"
logger.error(error_message)
results.append(
{"status": "fail", "message": error_message, "task_type": "任务执行"}
)
pusher.push(results)
logger.info(f"执行结束:{desensitize_name(config.get_value('userInfo.nikeName'))}")
def execute_tasks(selected_files: Optional[List[str]] = None):
"""
创建并执行任务。
Args:
selected_files (Optional[List[str]]): 指定配置文件列表(不含扩展名),默认为 None。
"""
logger.info("开始执行工学云任务")
# 获取用户目录下的所有 .json 文件(不含后缀)
try:
json_files = [f[:-5] for f in os.listdir(USER_DIR) if f.endswith(".json")]
logger.info(f"发现 {len(json_files)} 个配置文件")
except OSError as e:
logger.error(f"扫描配置文件目录失败: {e}")
json_files = []
# 筛选指定的配置文件
if selected_files:
existing_files = set(selected_files) & set(json_files)
missing_files = set(selected_files) - existing_files
if missing_files:
logger.error(f"以下配置文件未找到: {', '.join(missing_files)}")
json_files = list(existing_files)
# 从环境变量获取配置
try:
user_env = os.getenv("USER", "[]")
user_configs = json.loads(user_env)
if not isinstance(user_configs, list):
raise ValueError("环境变量 USER 必须包含 JSON 数组")
logger.info(f"从环境变量中获取到 {len(user_configs)} 个配置")
except (json.JSONDecodeError, ValueError) as e:
logger.error(f"解析环境变量 USER 失败: {e}")
user_configs = []
# 检查是否存在有效配置
if not json_files and not user_configs:
logger.warning("未找到任何有效配置")
return
# 创建任务列表
tasks = []
def add_task(source, **kwargs):
try:
tasks.append(ConfigManager(**kwargs))
logger.debug(f"已添加来自 {source} 的任务配置")
except Exception as err:
logger.error(f"创建来自 {source} 的任务失败: {err}")
# 处理环境变量中的配置
for config in user_configs:
add_task("环境变量", config=config)
# 处理配置文件
for name in json_files:
file_path = os.path.join(USER_DIR, f"{name}.json")
add_task(f"配置文件 {name}", path=file_path)
if not tasks:
logger.error("没有成功创建任何任务")
return
# 执行任务
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
future_to_task = {executor.submit(run, task): task for task in tasks}
for future in concurrent.futures.as_completed(future_to_task):
task = future_to_task[future]
try:
future.result()
except Exception as e:
logger.error(f"任务 {task} 处理过程中发生错误: {e}")
logger.info("工学云任务执行结束")
if __name__ == "__main__":
# 读取命令行参数
parser = argparse.ArgumentParser(description="运行工学云任务")
parser.add_argument(
"--file",
type=str,
nargs="+",
help="指定要执行的配置文件名(不带路径和后缀),可以一次性指定多个",
)
args = parser.parse_args()
# 执行命令
execute_tasks(args.file)