-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathAutoSign.py
203 lines (177 loc) · 8.46 KB
/
AutoSign.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
import requests
from requests.sessions import session
from lxml import etree
import base64
import re
from itertools import combinations
import time
import os
from login import Login
import json
global currClass
currClass = 0
def login(username, password):
global session
session = requests.session()
if os.path.exists(os.path.dirname(os.path.realpath(__file__)) + "/cookies.json"):
with open(os.path.dirname(os.path.realpath(__file__)) + "/cookies.json", "r") as f:
# json.loads(f.read())
session.cookies.update(json.loads(f.read()))
print("cookies存在,使用cookies")
return
url = 'http://passport2.chaoxing.com/fanyalogin'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0',
'Referer': r'http://passport2.chaoxing.com/login?fid=&newversion=true&refer=http%3A%2F%2Fi.chaoxing.com'
}
my_login = Login(username, password)
my_login.get_information()
data = {
'fid': -1,
'uname': my_login.username,
'password': my_login.password,
'refer': r'http%253A%252F%252Fi.chaoxing.com',
't': True,
'forbidotherlogin': 0
}
# session=requests.session()
res = session.post(url, headers=headers, data=data)
with open(os.path.dirname(os.path.realpath(__file__)) + "/cookies.json", "w") as f:
f.write(json.dumps(res.cookies.get_dict()))
# print(res.cookies)
def getclass():
url = 'http://mooc1-2.chaoxing.com/visit/courses'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0',
'Referer': r'http://i.chaoxing.com/'
}
res = session.get(url, headers=headers)
# print(res.status_code)
if res.status_code == 200:
class_HTML = etree.HTML(res.text)
# print("处理成功,您当前已开启的课程如下:")
i = 0
global course_dict
course_dict = {}
for class_item in class_HTML.xpath("/html/body/div/div[2]/div[3]/ul/li[@class='courseItem curFile']"):
# courseid=class_item.xpath("./input[@name='courseId']/@value")[0]
# classid=class_item.xpath("./input[@name='classId']/@value")[0]
try:
class_item_name = class_item.xpath("./div[2]/h3/a/@title")[0]
# 等待开课的课程由于尚未对应链接,所有缺少a标签。
i += 1
# print(class_item_name)
course_dict[i] = [class_item_name,
"https://mooc1-2.chaoxing.com{}".format(class_item.xpath("./div[1]/a[1]/@href")[0])]
except Exception as e:
print(e)
# print("———————————————————————————————————")
else:
print("error:课程处理失败")
def qiandao(url: str, address: str, sleepTime: int, SENDKEY: str):
url = 'https://mobilelearn.chaoxing.com/widget/pcpick/stu/index?courseId={courseid}&jclassId={clazzid}'.format(
courseid=re.findall(r"courseid=(.*?)&", url)[0], clazzid=re.findall(r"clazzid=(.*?)&", url)[0])
# print(url)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0',
}
res = session.get(url, headers=headers)
# print(res.text)
tree = etree.HTML(res.text)
# fid=tree.xpath('/html/body/input[4]/@value')
activeDetail = tree.xpath('/html/body/div[2]/div[2]/div/div/div/@onclick')
if not activeDetail:
print(course_dict[currClass][0] + "------暂无签到活动")
else:
print('\n')
print(course_dict[currClass][0] + "------检测到:" + str(len(activeDetail)) + "个活动。")
time.sleep(sleepTime)
for activeID in activeDetail:
global id
id = re.findall(r'activeDetail\((.*?),', activeID)
enc = ''
data = session.get(
'https://mobilelearn.chaoxing.com/v2/apis/sign/refreshQRCode?activeId={id}'.format(id=id[0])).json()[
'data']
if data != None:
enc = data['enc']
# print(enc)
url = 'https://mobilelearn.chaoxing.com/pptSign/stuSignajax?activeId={id}&clientip=&latitude=-1&longitude=-1&appType=15&fid=0&enc={enc}&address={address}'.format(
id=id[0], enc=enc, address=address)
# print(url)
res = session.get(url, headers=headers)
# url='https://mobilelearn.chaoxing.com//widget/sign/pcStuSignController/checkSignCode?activeId={id}&signCode={signcode}'.format(id=id[0],signcode=1236)
# res=session.get(url,headers=headers)
# print(url)
print('**********')
print(res.text)
if '非签到活动' in res.text:
continue
push(SENDKEY, res, TGCHATID, BOTTOKEN)
print('\n')
def push(SENDKEY, res, TGCHATID, BOTTOKEN):
if SENDKEY == '':
print("SENDKEY 为空,跳过 server 酱推送")
else:
if res.text == 'success':
# server酱推送
rServerchan = requests.post('https://sctapi.ftqq.com/{sendkey}.send'.format(sendkey=SENDKEY),
data={'text': "学习通-签到成功", 'desp': course_dict[currClass][0] + "签到成功"})
elif res.text == '您已签到过了':
rServerchan = requests.post('https://sctapi.ftqq.com/{sendkey}.send'.format(sendkey=SENDKEY),
data={'text': "学习通-已签到过了", 'desp': course_dict[currClass][0] + "您已签到过了"})
else:
rServerchan = requests.post('https://sctapi.ftqq.com/{sendkey}.send'.format(sendkey=SENDKEY),
data={'text': "学习通-签到失败", 'desp': "签到失败,原因:" + res.text})
if rServerchan.status_code == 200:
print("Server酱推送成功")
elif rServerchan.status_code == 400:
print("Server酱推送失败,SENDKEY 填写有误")
else:
print("Server酱推送失败,未知错误")
if (TGCHATID == '' or BOTTOKEN == ''):
print("Telgram 推送参数配置有错,跳过 telegram 推送")
else:
if res.text == 'success':
# Telegram 推送
rTelegram = requests.get(
'https://api.telegram.org/bot{BOTTOKEN}/sendMessage?chat_id={TGCHATID}&text={desp}'.format(
BOTTOKEN=BOTTOKEN, TGCHATID=TGCHATID, desp=course_dict[currClass][0] + "签到成功"))
elif res.text == '您已签到过了':
rTelegram = requests.get(
'https://api.telegram.org/bot{BOTTOKEN}/sendMessage?chat_id={TGCHATID}&text={desp}'.format(
BOTTOKEN=BOTTOKEN, TGCHATID=TGCHATID, desp=course_dict[currClass][0] + "您已签到过了"))
else:
rTelegram = requests.get(
'https://api.telegram.org/bot{BOTTOKEN}/sendMessage?chat_id={TGCHATID}&text={desp}'.format(
BOTTOKEN=BOTTOKEN, TGCHATID=TGCHATID, desp="签到失败,原因:" + res.text))
if rTelegram.status_code == 200:
print('Telegram 推送成功')
elif rTelegram.status_code == 400:
print('Telegram 推送失败,CHATID 填写有误')
else:
print('Telegram 推送失败,未知错误')
if __name__ == '__main__':
username = os.environ["USERNAME"]
password = os.environ["PASSWORD"]
# server酱sendkey
SENDKEY = os.environ["SENDKEY"]
# Telegram推送参数
TGCHATID = os.environ["TGCHATID"]
BOTTOKEN = os.environ["BOTTOKEN"]
# 在下方可以更改签到地址和二维码的enc
address = os.environ["ADDRESS"]
# 监测到签到活动后,延迟多久进行签到,1s=1000ms
sleepTime = 10
course_dict = {}
while course_dict == {}:
login(username, password)
getclass()
if course_dict == {}:
print("cookie过期,重新登录")
os.remove(os.path.dirname(os.path.realpath(__file__)) + "/cookies.json")
continue
# print(course_dict)
for currClass in course_dict:
# print(course_dict[i][1])
qiandao(course_dict[currClass][1], address, sleepTime, SENDKEY)