forked from Rockytkg/AutoMoGuDingCheckIn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
516 lines (438 loc) · 17.6 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
import logging
import os
import argparse
import random
from datetime import datetime, timedelta
from typing import Dict, List, Any
from util.Api import ApiClient, generate_article, upload
from util.Config import ConfigManager
from util.MessagePush import MessagePusher
# 配置日志
logging.basicConfig(
format="[%(asctime)s] %(name)s %(levelname)s: %(message)s",
level=logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S"
)
logger = logging.getLogger("MainModule")
USER_DIR = os.path.join(os.path.dirname(__file__), "user")
def get_api_client(config: ConfigManager) -> ApiClient:
"""获取配置好的ApiClient实例。
:param config: 配置管理器。
:type config: ConfigManager
:return: ApiClient实例。
:rtype: ApiClient
"""
api_client = ApiClient(config)
if not config.get_value('userInfo.token'):
api_client.login()
if not config.get_value('planInfo.planId'):
api_client.fetch_internship_plan()
else:
logger.info("使用本地数据")
return api_client
def upload_img(api_client: ApiClient, config: ConfigManager, count: int) -> str:
"""上传指定数量的图片
:param api_client: ApiClient实例。
:type api_client: ApiClient
:param config: 配置管理器。
:type config: ConfigManager
:param count: 需要上传的图片数量。
:type count: int
:return: 上传成功的图片链接
:rtype: str
"""
# 检查数量是否大于0
if count <= 0:
return ""
images_dir = os.path.join(os.path.dirname(__file__), "images")
# 获取所有符合条件的图片文件
all_images = [os.path.join(images_dir, f) for f in os.listdir(images_dir) if
f.lower().endswith(('.png', '.jpg', '.jpeg'))]
# 检查可用图片数量
if len(all_images) < count:
return ""
# 随机选择指定数量的图片
images = random.sample(all_images, count)
# 获取上传令牌并上传图片
token = api_client.get_upload_token()
return upload(token, images, config)
def perform_clock_in(api_client: ApiClient, config: ConfigManager) -> Dict[str, Any]:
"""执行打卡操作
:param api_client: ApiClient实例。
:type api_client: ApiClient
:param config: 配置管理器。
:type config: ConfigManager
:return: 执行结果
:rtype: Dict[str, Any]
"""
# try:
# current_time = datetime.now()
# current_hour = current_time.hour
#
# # 判断打卡类型
# if 8 <= current_hour < 12:
# checkin_type = 'START'
# display_type = '上班'
# elif 17 <= current_hour < 20:
# checkin_type = 'END'
# display_type = '下班'
# else:
# logger.info("当前不在打卡时间范围内")
# return {
# "status": "skip",
# "message": "当前不在打卡时间范围内",
# "task_type": "打卡"
# }
try:
current_time = datetime.now()
current_hour = current_time.hour
# 判断打卡类型
if 0 <= current_hour < 17:
checkin_type = 'START'
display_type = '上班'
elif 17 <= current_hour < 23:
checkin_type = 'END'
display_type = '下班'
else:
logger.info("当前不在打卡时间范围内")
return {
"status": "skip",
"message": "当前不在打卡时间范围内",
"task_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 = config.get_value('userInfo.nikeName')
logger.info(f'用户 {user_name} 开始 {display_type} 打卡')
attachments = upload_img(api_client, config, config.get_value("config.clockIn.imageCount"))
# 设置打卡信息
checkin_info = {
'type': checkin_type,
'lastDetailAddress': last_checkin_info.get('address'),
'attachments': attachments or None
}
api_client.submit_clock_in(checkin_info)
logger.info(f'用户 {user_name} {display_type} 打卡成功')
return {
"status": "success",
"message": f"{display_type}打卡成功",
"task_type": "打卡",
"details": {
"姓名": user_name,
"打卡类型": 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]:
"""提交日报
:param api_client: ApiClient实例。
:type api_client: ApiClient
:param config: 配置管理器。
:type config: ConfigManager
:return: 执行结果
:rtype: 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 (17 <= current_time.hour < 20):
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, config, config.get_value("config.reportSettings.daily.imageCount"))
report_info = {
'title': f'第{report_count}天日报',
'content': content,
'attachments': attachments,
'reportType': 'day',
'jobId': job_info.get('jobId'),
'reportTime': current_time.strftime('%Y-%m-%d %H:%M:%S')
}
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]:
"""提交周报
:param config: 配置管理器。
:type config: ConfigManager
:param api_client: ApiClient实例。
:type api_client: ApiClient
:return: 执行结果
:rtype: 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 (17 <= current_time.hour < 20):
logger.info("未到周报提交时间")
return {
"status": "skip",
"message": "未到周报提交时间",
"task_type": "周报提交"
}
try:
# 获取当前周信息
current_week_info = api_client.get_weeks_date()
# 获取历史提交记录
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, config, 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'),
'weeks': current_week_string
}
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]:
"""提交月报
:param config: 配置管理器。
:type config: ConfigManager
:param api_client: ApiClient实例。
:type api_client: ApiClient
:return: 执行结果
:rtype: 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 (17 <= current_time.hour < 20):
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, config, 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'),
}
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:
"""执行所有任务
:param config: 配置管理器
:type 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 = get_api_client(config)
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("任务异常结束\n")
return
logger.info(f"开始执行:{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"执行结束:{config.get_value('userInfo.nikeName')}")
def main(selected_files: list = None) -> None:
"""程序主入口
:param selected_files: 选定的配置文件名(不带路径和后缀)
:type selected_files: list
"""
logger.info("工学云任务开始")
json_files = {f[:-5]: f for f in os.listdir(USER_DIR) if f.endswith('.json')}
if not json_files:
logger.info("打卡文件未配置")
return
if selected_files:
for selected_file in selected_files:
if selected_file in json_files:
run(ConfigManager(os.path.join(USER_DIR, json_files[selected_file])))
else:
logger.error(f"指定的文件 {selected_file}.json 不存在")
else:
for filename in json_files.values():
run(ConfigManager(os.path.join(USER_DIR, filename)))
logger.info("工学云任务结束")
if __name__ == '__main__':
# 读取命令行参数
parser = argparse.ArgumentParser(description="运行工学云任务")
parser.add_argument('--file', type=str, nargs='+', help='指定要执行的配置文件名(不带路径和后缀),可以一次性指定多个')
args = parser.parse_args()
# 执行命令
main(args.file)