-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path图书馆预约.py
139 lines (120 loc) · 6.07 KB
/
图书馆预约.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
# 图书馆预约
import json
import time
import random
import traceback
import datetime
import requests
from bs4 import BeautifulSoup
def sendMessage(msg):
print(msg)
# 如果需要通知提醒,请在此处添加邮箱、或QQ机器人、微信企业号、微信公众号、pushplus等通知提醒功能的代码
def appointmentLibrary(username, password):
try:
baseUrl = 'https://ehall.njtech.edu.cn'
session = requests.Session()
response = session.get(baseUrl)
loginUrl = response.url
soup = BeautifulSoup(response.content, "html.parser")
lt0 = soup.find('input', attrs={'name': 'lt'})['value']
execution0 = soup.find('input', attrs={'name': 'execution'})['value']
loginPostData = {
'username': username,
'password': password,
'channelshow': '校园内网',
'channel': 'default',
'lt': lt0,
'execution': execution0,
'_eventId': 'submit',
'login': '登录',
}
response = session.post(loginUrl, data=loginPostData, allow_redirects=False)
response = session.get(response.headers['Location'])
entryUrl = baseUrl+'/infoplus/form/TSGXY/start'
response = session.get(entryUrl)
soup = BeautifulSoup(response.content, "html.parser")
csrfToken = soup.find('meta', attrs={'itemscope': 'csrfToken'})['content']
idc = soup.find('input', attrs={'id': 'idc'})['value']
release = soup.find('input', attrs={'id': 'release'})['value']
response = session.post(entryUrl, data={
"idc": idc,
"release": release,
"csrfToken": csrfToken,
"formData": json.dumps({
'_VAR_URL': entryUrl,
'_VAR_URL_Attr': None,
}),
})
formUrl = response.url
stepId = formUrl.split('form/')[1].split('/')[0]
response = session.post(baseUrl+'/infoplus/interface/render',
headers={'referer': formUrl},
data={
"stepId": stepId,
"instanceId": "",
"admin": "false",
"rand": random.random()*999,
"width": "1550",
"lang": "zh",
"csrfToken": csrfToken,
})
formData=json.loads(response.text)['entities'][0]['data']
formData['_VAR_ENTRY_NAME'] = "图书馆预约申请"
formData['_VAR_ENTRY_TAGS'] = "预约服务"
formData['fieldXq_Name'] = "逸夫图书馆" # 逸夫图书馆 或 浦江图书馆
formData['fieldXq'] = "1" # 1(逸夫) 或 2(浦江)
formData['_VAR_URL'] = formUrl
formData['fieldSFLG'] = ""
targetDay = datetime.date.today()
targetTimeStamp = str(int(time.mktime((targetDay.year, targetDay.month, targetDay.day, 0, 0, 0, 0, 0, 0))))
formData['fieldJzzt'] = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日'][targetDay.weekday()]
formData['fieldDateTo'] = targetTimeStamp
formData['fieldDateFrom'] = targetTimeStamp
formData['fieldYyy'] = "0" # 已预约人数
formData['fieldKyy'] = "1000" # 可预约人数
formData['fieldYyzt'] = "1"
postData = {
"stepId": stepId,
"actionId": 1,
"formData": json.dumps(formData),
"timestamp": int(time.time()),
"boundFields": "fieldDateFrom,fieldDateTo,fieldSFLG,fieldJzzt,fieldJzr,fieldSfz,fieldGh,fieldKyy,fieldLxfs,fieldXq,fieldYyy,fieldYyzt",
"csrfToken": csrfToken,
"lang": "zh",
"nextUsers": "{}"
}
response = session.post(baseUrl+'/infoplus/interface/listNextStepsUsers',data={**postData,"rand": random.random()*999})
response = session.post(baseUrl+'/infoplus/interface/doAction',data={**postData,"rand": random.random()*999})
result = json.loads(response.content)
try:
if result['entities'][0]['name']=='申请人确认':
sendMessage("图书馆预约成功!\n请手动进入办事大厅进行申请人确认\n数据如下:\n"+json.dumps(result, indent=0, separators=(', ', ': '), ensure_ascii=False)[2:-1])
# 申请人确认,不用抢名额,可以手动确认
# try:
# time.sleep(2)
# postData["actionId"] = 2
# response = session.post(baseUrl+'/infoplus/interface/listNextStepsUsers',data={**postData,"rand": random.random()*999})
# response = session.post(baseUrl+'/infoplus/interface/doAction',data={**postData,"rand": random.random()*999})
# print("申请人确认成功!")
# except Exception as e:
# print("申请人确认失败!\n报错信息如下:\n"+traceback.format_exc())
return True
else:
sendMessage("❗❗❗\n图书馆预约失败!\n数据如下:\n"+json.dumps(result, indent=0, separators=(', ', ': '), ensure_ascii=False)[2:-1])
return False
except Exception as e:
sendMessage("❗❗❗\n图书馆预约失败!\n数据如下:\n"+json.dumps(result, indent=0, separators=(', ', ': '), ensure_ascii=False)[2:-1])
return False
except Exception as e:
sendMessage("❗❗❗\图书馆预约失败!\n报错信息如下:\n"+traceback.format_exc())
return False
# 自动重试
def retryAppointment(username, password, maxRetryTimes=3):
for i in range(maxRetryTimes+1):
i and sendMessage("图书馆预约第"+str(i)+"次重试...")
if appointmentLibrary(username, password):
return True
time.sleep(random.randint(1,2))
sendMessage("图书馆预约失败!已超过最大重试次数")
return False
retryAppointment('你的学号', 'i南工的登录密码')