This repository has been archived by the owner on Jun 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
submain.py
136 lines (121 loc) · 5.49 KB
/
submain.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
import requests
import os
import asyncio
import json
import smtplib
import re
from email.mime.text import MIMEText
from email.utils import formataddr
from .submit import login_submit
from hoshino import logger
# 自动和手动的 全员提交
async def cpdaily_submit(mode):
current_dir = os.path.join(os.path.dirname(__file__), 'config.json')
with open(current_dir, 'r', encoding='UTF-8') as f:
f_data = json.load(f)
manage_dir = os.path.join(os.path.dirname(__file__), 'manage.json')
with open(manage_dir, 'r', encoding='UTF-8') as af:
m_data = json.load(af)
num = len(list(f_data.keys()))
msg_list = []
for username in list(f_data.keys()):
await asyncio.sleep(0.5)
try:
msg_list = await get_msg_list(username, f_data, m_data, mode, msg_list)
except KeyError as e:
if str(e) == "'data'":
msg_list.append(f'{username}前一天未打卡,无法自动打卡,已跳过')
logger.info(f'{username}前一天未打卡,无法自动打卡,已跳过')
continue
else:
return f'出现错误:{e},可能是网站又又又炸了'
logger.info('==所有用户处理结束==')
if not msg_list:
msg = f'全部{mode}成功提交'
elif len(msg_list) == num:
error_info = re.search(r'发生错误:\S*', msg_list[0]).group()
msg = f'警告:\n全员提交失败!\n{error_info}'
else:
msg = f'全部提交完成,其中部分用户提交出现问题:\n' + '\n'.join(msg_list)
return msg
# 单独提交
async def single_submit(username, mode):
current_dir = os.path.join(os.path.dirname(__file__), 'config.json')
with open(current_dir, 'r', encoding='UTF-8') as f:
f_data = json.load(f)
manage_dir = os.path.join(os.path.dirname(__file__), 'manage.json')
with open(manage_dir, 'r', encoding='UTF-8') as af:
m_data = json.load(af)
msg_list = []
try:
msg_list = await get_msg_list(username, f_data, m_data, mode, msg_list)
except KeyError as e:
return f'出现错误:{e},可能是网站又又又炸了'
logger.info(f'=={username}处理结束==')
if not msg_list:
msg = f'{username}成功{mode}提交'
else:
msg = f'提交出现问题:\n' + '\n'.join(msg_list)
return msg
# 提交的过程
async def get_msg_list(username, f_data, m_data, mode, msg_list):
logger.info(f'开始处理用户{username}')
try:
# 登录并提交
flag, error_info = await login_submit(username, f_data[username]['password'], m_data['location'], m_data['region'])
if flag:
if flag == 'success':
info = f'{mode}提交成功'
elif flag == 'have_done':
info = f'{username}已经提交过了'
msg_list.append(info)
elif flag == 'need_self':
info = f'{username}本次请手动填报提交'
msg_list.append(info)
logger.info(info)
emailmsg = f'''
你好:
来自{mode}提交系统的消息:
{info}
'''
await InfoSubmit(username, m_data, emailmsg, f_data[username]['email'], f_data[username]['enable_email'])
else:
logger.info(f'{username}发生错误:{error_info}')
emailmsg = f'''
你好:
来自{mode}提交系统的消息:
{mode}提交失败!
{username}发生错误:{error_info}
'''
await InfoSubmit(username, m_data, emailmsg, f_data[username]['email'], f_data[username]['enable_email'])
msg_list.append(f'{username}发生错误:{error_info}')
except requests.HTTPError:
logger.info(f'{username}发生错误:密码错误')
emailmsg = f'''
你好:
来自{mode}提交系统的消息:
{mode}提交失败!
{username}发生错误:密码错误
'''
await InfoSubmit(username, m_data, emailmsg, f_data[username]['email'], f_data[username]['enable_email'])
msg_list.append(f'{username}发生错误:密码错误')
return msg_list
# 邮件发送
async def InfoSubmit(username, m_data, msg, email, enable_email):
if not enable_email:
logger.info("该用户已关闭邮件提醒服务")
return
my_sender= m_data['account'] # 发件人邮箱账号
my_pass = m_data['emailpassword'] # 发件人邮箱密码
try:
msg=MIMEText(str(msg),'plain','utf-8')
msg['From']=formataddr(["自动提交系统", my_sender]) # 括号里的对应发件人邮箱昵称、发件人邮箱账号
msg['To']=formataddr([username, email]) # 括号里的对应收件人邮箱昵称、收件人邮箱账号
msg['Subject']= '提交结果通知' # 邮件的标题
server=smtplib.SMTP_SSL(m_data['server'], m_data['port']) # 发件人邮箱中的SMTP服务器,端口是对应邮箱的ssl发送端口
server.login(my_sender, my_pass) # 括号中对应的是发件人邮箱账号、邮箱密码
server.sendmail(my_sender, email, msg.as_string()) # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
server.quit() # 关闭连接
logger.info("邮件发送成功")
except Exception:
logger.info("邮件发送失败")