-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpafd_serve.py
66 lines (55 loc) · 1.88 KB
/
pafd_serve.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
import sys
import time
from io import StringIO
import pymysql
import config
from config import db_host, db_port, db_name, db_user, db_password
from pafd import Pafd
from utils.mail import send_email
MAX_RETRY = 5
def fetch_data():
con = pymysql.connect(host=db_host, port=db_port, database=db_name, user=db_user, password=db_password)
cursor = con.cursor()
cursor.execute('select uid, password, name from student')
data = cursor.fetchall()
con.close()
return data
def serialize(student):
uid = student[0]
domain = 'm.fudan.edu.cn' if int(uid[:2]) > 20 else 'fudan.edu.cn'
email = f'{uid}@{domain}'
return {
'uid': student[0],
'password': student[1],
'name': student[2],
'email': email
}
if __name__ == '__main__':
for datum in fetch_data():
datum = serialize(datum)
uid = datum['uid']
password = datum['password']
name = datum['name']
email = datum['email']
print('[I]', name, '开始提交')
sys.stdout = message = StringIO()
for i in range(MAX_RETRY + 1):
time.sleep(1)
pafd = Pafd(username=uid, password=password)
result = pafd.main()
print(result.get('message'))
if result.get('code') == -1:
print('[W] 提交失败,', end='')
if i < MAX_RETRY:
print(f'重试中...[{i + 1}/{MAX_RETRY}]')
else:
print('达到最大重试次数')
send_email(
'平安复旦提交失败',
f'{name}今天的平安复旦提交失败,原因为:\n{message.getvalue()}\n请手动提交!',
[email, config.email]
)
else:
break
sys.stdout = sys.__stdout__
print(message.getvalue())