forked from lthero-big/YouthStudyAuto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZheJiangAuto.py
176 lines (151 loc) · 6.23 KB
/
ZheJiangAuto.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
import re
import requests
import json
import yaml
from lxml import etree
from urllib import request
getToken_url = 'https://qczj.h5yunban.com/qczj-youth-learning/cgi-bin/login/we-chat/callback'
getUserInfo_url = 'https://qczj.h5yunban.com/qczj-youth-learning/cgi-bin/user-api/course/last-info'
getClass_url = 'https://qczj.h5yunban.com/qczj-youth-learning/cgi-bin/common-api/course/current'
checkin_url = 'https://qczj.h5yunban.com/qczj-youth-learning/cgi-bin/user-api/course/join'
getPersonalInfo_url = 'https://qczj.h5yunban.com/qczj-youth-learning/cgi-bin/user-api/info'
headers = {
'Content-Type': 'text/plain'
}
def getYmlConfig(yaml_file='config.yml'):
with open(yaml_file, 'r', encoding='utf-8') as f:
file_data = f.read()
return dict(yaml.load(file_data, Loader=yaml.FullLoader))
def getToken(openId):
# 根据openId获得token
try:
token = requests.get(url=getToken_url, params=openId, headers=headers)
Token_raw = token.text
Token = re.findall('[A-Z0-9]{8}[-][A-Z0-9]{4}[-][A-Z0-9]{4}[-][A-Z0-9]{4}[-][A-Z0-9]{12}', Token_raw)[0]
print('获取Token为:' + Token)
accessToken = {
'accessToken': Token
}
return accessToken
except:
print('获取Token失败,请检查openId是否正确')
def getinfo(accessToken):
# 根据accessToken获得用户信息
try:
getUserInfo = requests.get(getUserInfo_url, params=accessToken, headers=headers)
userInfo = getUserInfo.json()
cardNo = userInfo["result"]["cardNo"]
nid = userInfo["result"]["nid"]
getClass = requests.get(getClass_url, params=accessToken, headers=headers)
Class = getClass.json()
classId = Class["result"]["id"]
infos: list = userInfo['result']['nodes']
Faculty = [item['title'] for item in infos]
print('签到课程为:' + classId, '\n您填写的个人信息为:' + cardNo, '\n您的签到所属组织为:' + str(Faculty))
checkinData = {
'course': classId,
'subOrg': None,
'nid': nid,
'cardNo': cardNo
}
return checkinData
except Exception as e:
if "is not subscriptable" in str(e):
print("openid出错,无法获得您的信息")
print(f'获取历史信息失败,请您手动打卡:{e}')
def signup(accessToken, checkinData):
# 根据token和data完成打卡
checkin = requests.post(checkin_url, params=accessToken, data=json.dumps(checkinData), headers=headers)
result = checkin.json()
if result["status"] == 200:
print("签到成功")
return 1
else:
print('出现错误,错误码:' + str(result["status"]))
print('错误信息:' + str(result["message"]))
return result["message"]
def getPersonalInfo(accessToken):
# 获得个人信息
info = requests.get(url=getPersonalInfo_url, params=accessToken, headers=headers).json()
print('当前分数 ', info['result']['score'])
return info['result']
# return info['result']['score']
# 调用接口发送邮件,不提供接口
def sendMail(user,info,resStatus):
msg=resStatus
if resStatus==1:
msg="完成"
# 接收方
receiver = user['user']['mail']
# 内容
content = '{updateTime} 您好,{user}!本周大学习打卡:{msg}。当前分数为{score}分'.format(user=info['nickname'],msg=msg,score=info['score'],updateTime=info['lastUpdTime'])
params = {
'reciever': receiver,
# 邮件标题
'title': f'[{msg}]青年大学习打卡',
# 主要内容
'content': content,
# 内部大标题
'innerTitle': f'青年大学习'
}
requests.post(url='http://邮件接口/', data=params)
print("邮件发送完成")
# 本地发送邮件,需要自行配置
def sendMail(to_email, title, content):
EMAIL_FROM = 'from@runoob.com' # 配置发信地址
EMAIL_HOST_PASSWORD = "aaabbbbb" # 密码
EMAIL_HOST, EMAIL_PORT = 'smtp.XXX.com', 80
# 自定义的回复地址
replyto = EMAIL_FROM
msg = MIMEMultipart('alternative')
msg['Subject'] = title
msg['From'] = '%s <%s>' % ("发送方名称", EMAIL_FROM)
msg['To'] = '%s <%s>' % ("接收方名称", to_email)
msg['Reply-to'] = replyto
msg['Message-id'] = email.utils.make_msgid()
msg['Date'] = email.utils.formatdate()
textplain = MIMEText('{}'.format(content), _subtype='plain', _charset='UTF-8')
msg.attach(textplain)
try:
client = smtplib.SMTP()
client.connect(EMAIL_HOST, EMAIL_PORT)
client.set_debuglevel(0)
client.login(EMAIL_FROM, EMAIL_HOST_PASSWORD)
client.sendmail(EMAIL_FROM, [to_email], msg.as_string())
client.quit()
return True
except Exception as e:
error_msg = '邮件发送异常, {}'.format(str(e))
print(error_msg)
return False
def get_screenshot_url():
"""
获取截屏图片的URL。
:return: 截屏图片的URL。
"""
response = requests.get('https://m.cyol.com/gb/channels/vrGlAKDl/index.html')
response_html = etree.HTML(response.text)
href_list = response_html.xpath('/html/body/section[1]/div/ul//a/@href')
if 'index.html' in href_list[0]:
return href_list[0].replace("index.html", "images/end.jpg")
elif 'm.html' in href_list[0]:
return href_list[0].replace("m.html", "images/end.jpg")
if __name__ == "__main__":
config = getYmlConfig()
for index, eachuser in enumerate(config['users']):
print(eachuser['user']['name'], 'openId为 ', eachuser['user']['openid'])
openid = {
'appid': 'wx56b888a1409a2920',
'openid': eachuser['user']['openid']
}
accesstoken = getToken(openid)
checkindata = getinfo(accesstoken)
if checkindata is not None:
personalInfo=getPersonalInfo(accesstoken)
resStatus=signup(accesstoken, checkindata)
# 需要自行配置接口
# sendMail(eachuser,personalInfo,resStatus)
# 需要自行配置发送邮箱
# sendMail(eachuser['user']['mail'], "邮件标题", "邮件内容")
print('===========================================')
request.urlretrieve(get_screenshot_url(), f'./output.jpg')