图书馆预约脚本新版个人版_requests

script_for_PG

由于之前图书馆敏感的原因下线了GitHub的仓库,也被图书馆管理员叫去谈话,只能在博客里面做一下记录吧,这个是午休期间可以维持座位的脚本,测试可用仅限于四楼东侧座位,其余座位需要轻微修改,早上的预约脚本中mylist可以根据个人需求修改座位号。

日后希望能做出一个完整版个人预约服务脚本,然后放在校内师兄服务器上运行,个人电脑的运行环境不稳定,可能因为宿舍网络原因导致预约失败。

午休脚本

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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python3
# @Author : Guoguo
# @Blog :https://gwyxjtu.github.io
import os
import time
import json
import base64
import requests
from Crypto.Cipher import AES
import re
from lxml import etree
from threading import Thread
import datetime

class XJTUUser(object):

def __init__(self, config_file_path='./config.json'):
with open(config_file_path) as config_file:
config = json.loads(config_file.read())
self.config = config
self.is_login = False
self.session = requests.Session()
self.session.headers.update(config['headers'])
self.session.cookies.update(config['cookies'])


def login(self):
#--------------------------------------
self.weizhi = ''
def vxpush(self,msg):
url = 'http://wxpusher.zjiecode.com/api/send/message'
uid = ''
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
'Content-Type':'application/json;charset=UTF-8'
}
data = {
'appToken':'',
'content': msg,
'contentType':1,
'uids':[uid]
}
print(requests.post(url,headers=headers,json=data).text)

def check_my_seat(self):#返回你的座位字符穿
r = self.session.get('http://rg.lib.xjtu.edu.cn:8010/my/')
dom = etree.HTML(r.text)
a_text = dom.xpath('/html/body/div[2]/center/div[1]/div/div/div/div/div/div[1]/h3/text()[2]')
ans = re.findall('\d+', a_text[0])[0]
#print()
self.weizhi = a_text[0].split('\xa0')[-1].split('\n')[0]
jjj = dom.xpath('/html/body/div[2]/center/div[1]/div/div/div/div/div/div[2]/center/h3[1]')
if(len(jjj)<1):
return 0;
if jjj[0].text == '已取消' or jjj[0].text == '已离馆' or jjj[0].text == '超时未入馆':
vxpush(self,self.config['username']+'没弄上,出现错误')
print(self.config['username']+'没弄上,出现错误')
return 0
else:
vxpush(self,self.config['username']+'弄好了,当前位置'+ans)
print(self.config['username']+'弄好了'+ans)
return 1


def cancel(self):
try:
r = self.session.get("http://rg.lib.xjtu.edu.cn:8010/my/")
pattern = re.compile("cancelconfirm\('(.*?)'\);", re.S)
yu_id = re.findall(pattern, r.text)[0]
print(yu_id)
self.session.get("http://rg.lib.xjtu.edu.cn:8010/my/?cancel=1&ri=" + yu_id)
#微信通知
vxpush(self,'取消成功')
print("取消成功")
return 1
except Exception as e:
print("取消失败\n" + str(e))
return 0
#预约座位
def reserve(self,kid,sp):
r = self.session.get('http://rg.lib.xjtu.edu.cn:8010/ruguan')#入关
#print('--------------------------')
nn = r.text.find('<input id="csrf_token" name="csrf_token" type="hidden" value=')
tok = r.text[nn+1+len('<input id="csrf_token" name="csrf_token" type="hidden" value='):nn+56+len('<input id="csrf_token" name="csrf_token" type="hidden" value=')]
#print(r.text[nn+1+len('<input id="csrf_token" name="csrf_token" type="hidden" value='):nn+56+len('<input id="csrf_token" name="csrf_token" type="hidden" value=')])
data={
'csrf_token':tok,
'csrf_token':tok,
'service':'seat',
'submit':'%E6%8F%90%E4%BA%A4',
'rplace':'east'
}#入关post,比较麻烦
r = self.session.post('http://rg.lib.xjtu.edu.cn:8010/ruguan',data = data)#提交

r = self.session.get('http://rg.lib.xjtu.edu.cn:8010/seat/?kid='+kid+'&sp='+sp)
#print(r.text)
#------------------------------新版判断
#兴庆区的,别的区得自行修改了
if check_my_seat(self) == 1:
#vxpush(self,'预约成功'+str(check_my_seat(self)))
#print('预约成功状态'+str(check_my_seat(self)))
return 1
else:
vxpush(self,'预约失败')
print('预约失败')
return 0
# if(sp == 'west3B'):
# r_w3=self.session.get('http://rg.lib.xjtu.edu.cn:8010/qseat?sp=west3B')
# s2 = json.loads(r_w3.text)['seat']
# if(s2[kid] == 1):
# return 200
# else:
# print('预约了但是没有成功,肯定是你已经预约过了')
# exit(0)
# return 0
# if(sp == 'east3A'):
# r_w3=self.session.get('http://rg.lib.xjtu.edu.cn:8010/qseat?sp=east3A')
# s2 = json.loads(r_w3.text)['seat']
# if(s2[kid] == 1):
# return 200
# else:
# print('预约了但是没有成功,肯定是你已经预约过了')
# exit(0)
# return 0
return r.status_code
def encrypt_pwd(raw_pwd, publicKey='0725@pwdorgopenp'):
''' AES-ECB encrypt '''
publicKey = publicKey.encode('utf-8')
# pkcs7 padding
BS = AES.block_size
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
pwd = pad(raw_pwd)
# zero padding
'''
pwd = raw_pwd
while len(raw_pwd.encode('utf-8')) % 16 != 0:
pwd += '\0'
'''
cipher = AES.new(publicKey, AES.MODE_ECB)
pwd = cipher.encrypt(pwd.encode('utf-8'))
return str(base64.b64encode(pwd), encoding='utf-8')

_headers = self.config['headers']
_headers['Content-Type'] = 'application/x-www-form-urlencoded'

# start with 302 redirection from ehall
_r = self.session.get('http://rg.lib.xjtu.edu.cn:8010/auth/login/?next=%2Fseat%2F')

# get cookie route
self.session.get('https://org.xjtu.edu.cn/openplatform/login.html')

# get JcaptchaCode and cookie JSESSIONID & sid_code
r_JcaptchaCode = self.session.post('https://org.xjtu.edu.cn/openplatform/g/admin/getJcaptchaCode',
headers=_headers)

# is_JcaptchaCode_show
url = 'https://org.xjtu.edu.cn/openplatform/g/admin/getIsShowJcaptchaCode'
params = {
'userName': self.config['username'],
'_': str(int(time.time() * 1000))
}
r = self.session.get(url, params=params, headers=_headers)
print(r.text)
# login
url = 'https://org.xjtu.edu.cn/openplatform/g/admin/login'
cookie = {
'cur_appId_':'JL4oKidbLpQ='
}
data = {
"loginType": 1,
"username": self.config['username'],
"pwd": encrypt_pwd(self.config['password']),
"jcaptchaCode": ""
}
_headers['Content-Type'] = 'application/json;charset=UTF-8'
r = self.session.post(url, data=json.dumps(data), headers=_headers,cookies=cookie)
print(r.text)
token = json.loads(r.text)['data']['tokenKey']

cookie = {
'cur_appId_':'JL4oKidbLpQ=',
'open_Platform_User' : token
}
r=self.session.get('http://org.xjtu.edu.cn/openplatform/oauth/auth/getRedirectUrl?userType=1&personNo='+self.config['person_id']+'&_=1590998261976',cookies = cookie)
print(r.text)
r=self.session.get(json.loads(r.text)['data'])
r=self.session.get('http://rg.lib.xjtu.edu.cn:8080/bxusr/link.jsp?uid='+self.config['username']+'&cn=%E9%83%AD%E7%8E%8B%E6%87%BF&employeeNumber='+self.config['person_id']+'&depId=%E7%94%B5%E5%AD%90%E4%B8%8E%E4%BF%A1%E6%81%AF%E5%AD%A6%E9%83%A8&mobile')#&email=867718012@qq.com去掉了
#print(r.text)
#---------------------登陆成功------------------------
check_my_seat(self)
re_time = datetime.datetime.now()
while(1):
#ii+=1
r=self.session.get('http://rg.lib.xjtu.edu.cn:8086/qseat?sp=north4east')#兴庆区的,别的区得自行修改了
s = json.loads(r.text)['seat']
#print(s1,s2,s_new)
print('循环次数:')
#vxpush(self,'test')
#print(ii)
#reserve(self,'029','south3middle')
print("位置状态" + str(s[self.weizhi]))
if s[self.weizhi] == 0:
while(1):
if reserve(self,self.weizhi,'north4east') == 1:
print('座位号是'+self.weizhi)
time.sleep(60*25)
while(cancel(self) == 0):
time.sleep(5)

# return(0)
time.sleep(2)
# if((datetime.datetime.now()-re_time).seconds>=300):
# exit(0)

if __name__ == '__main__':
#time.sleep(60*1)#预计一分钟离馆加延迟
dt = '2020-08-19 11:00:02'
ts = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S")))
t = int(time.time())
cha = ts - t
while cha >0:
t = int(time.time())
cha = ts - t
time.sleep(1)
if(cha%100 == 0):
print(cha)
print("开始")
guoguo = XJTUUser('./config.json')
guoguo.login()
exit(0)

凌晨预约脚本

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
#!/usr/bin/env python3
# @Author : Guoguo
# @Blog :https://gwyxjtu.github.io
import os
import time
import json
import base64
import requests
from Crypto.Cipher import AES
import re
from lxml import etree
from threading import Thread

our_seat = []

class XJTUUser(object):

def __init__(self, config_file_path='./config.json'):
with open(config_file_path) as config_file:
config = json.loads(config_file.read())
self.config = config
self.is_login = False
self.session = requests.Session()
self.session.headers.update(config['headers'])
self.session.cookies.update(config['cookies'])


def login(self):

#--------------------------------------
def check_my_seat(self):#返回你的座位字符穿
r = self.session.get('http://rg.lib.xjtu.edu.cn:8086/my/')
dom = etree.HTML(r.text)
a_text = dom.xpath('/html/body/div[2]/center/div[1]/div/div/div/div/div/div[1]/h3/text()[2]')
jjj = dom.xpath('/html/body/div[2]/center/div[1]/div/div/div/div/div/div[2]/center/h3[1]')
if(len(jjj)<1):
print('没弄上1,出现错误')
return 0;
if jjj[0].text == '已取消' or jjj[0].text == '已离馆' or jjj[0].text == '超时未入馆':
print('没弄上,出现错误')
return 0
else:
print('弄好了'+str(a_text))
our_seat.append(str(a_text))
return 1



def reserve(self,kid,sp):
r = self.session.get('http://rg.lib.xjtu.edu.cn:8086/ruguan')#入关
#print('--------------------------')
nn = r.text.find('<input id="csrf_token" name="csrf_token" type="hidden" value=')
tok = r.text[nn+1+len('<input id="csrf_token" name="csrf_token" type="hidden" value='):nn+56+len('<input id="csrf_token" name="csrf_token" type="hidden" value=')]
#print(r.text[nn+1+len('<input id="csrf_token" name="csrf_token" type="hidden" value='):nn+56+len('<input id="csrf_token" name="csrf_token" type="hidden" value=')])
data={
'csrf_token':tok,
'csrf_token':tok,
'service':'seat',
'submit':'%E6%8F%90%E4%BA%A4',
'rplace':'east'
}#入关post,比较麻烦
r = self.session.post('http://rg.lib.xjtu.edu.cn:8086/ruguan',data = data)#提交

r = self.session.get('http://rg.lib.xjtu.edu.cn:8086/seat/?kid='+kid+'&sp='+sp)
#print(r.text)
#------------------------------新版判断
#兴庆区的,别的区得自行修改了
if check_my_seat(self) == 1:
print('预约成功'+str(check_my_seat(self)))
return 1
else:
print('预约失败')
return 0

return r.status_code
def encrypt_pwd(raw_pwd, publicKey='0725@pwdorgopenp'):
''' AES-ECB encrypt '''
publicKey = publicKey.encode('utf-8')
# pkcs7 padding
BS = AES.block_size
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
pwd = pad(raw_pwd)
# zero padding
'''
pwd = raw_pwd
while len(raw_pwd.encode('utf-8')) % 16 != 0:
pwd += '\0'
'''
cipher = AES.new(publicKey, AES.MODE_ECB)
pwd = cipher.encrypt(pwd.encode('utf-8'))
return str(base64.b64encode(pwd), encoding='utf-8')

_headers = self.config['headers']
_headers['Content-Type'] = 'application/x-www-form-urlencoded'

# start with 302 redirection from ehall
_r = self.session.get('http://rg.lib.xjtu.edu.cn:8086/auth/login/?next=%2Fseat%2F')

# get cookie route
self.session.get('https://org.xjtu.edu.cn/openplatform/login.html')

# get JcaptchaCode and cookie JSESSIONID & sid_code
r_JcaptchaCode = self.session.post('https://org.xjtu.edu.cn/openplatform/g/admin/getJcaptchaCode',
headers=_headers)

# is_JcaptchaCode_show
url = 'https://org.xjtu.edu.cn/openplatform/g/admin/getIsShowJcaptchaCode'
params = {
'userName': self.config['username'],
'_': str(int(time.time() * 1000))
}
r = self.session.get(url, params=params, headers=_headers)
print(r.text)
# login
url = 'https://org.xjtu.edu.cn/openplatform/g/admin/login'
cookie = {
'cur_appId_':'JL4oKidbLpQ='
}
data = {
"loginType": 1,
"username": self.config['username'],
"pwd": encrypt_pwd(self.config['password']),
"jcaptchaCode": ""
}
_headers['Content-Type'] = 'application/json;charset=UTF-8'
r = self.session.post(url, data=json.dumps(data), headers=_headers,cookies=cookie)
print(r.text)
token = json.loads(r.text)['data']['tokenKey']

cookie = {
'cur_appId_':'JL4oKidbLpQ=',
'open_Platform_User' : token
}
r=self.session.get('http://org.xjtu.edu.cn/openplatform/oauth/auth/getRedirectUrl?userType=1&personNo='+self.config['person_id']+'&_=1590998261976',cookies = cookie)
print(r.text)
r=self.session.get(json.loads(r.text)['data'])
r=self.session.get('http://rg.lib.xjtu.edu.cn:8080/bxusr/link.jsp?uid='+self.config['username']+'&cn=%E9%83%AD%E7%8E%8B%E6%87%BF&employeeNumber='+self.config['person_id']+'&depId=%E7%94%B5%E5%AD%90%E4%B8%8E%E4%BF%A1%E6%81%AF%E5%AD%A6%E9%83%A8&mobile')#&email=867718012@qq.com去掉了
#print(r.text)
#---------------------登陆成功------------------------

#print(r_w3.text)
#print('各层座位数量',end = '')
#print(json.loads(r_e3.text)['scount'])
#s.update(json.loads(r_w3.text)['seat'])/seat/?kid=015&sp=north4southwest
#print(s1,s2)


#check_my_seat(self)
my_list = ['G029','G054','G066']
ii=0
while(1):
ii+=1
r=self.session.get('http://rg.lib.xjtu.edu.cn:8086/qseat?sp=north4east')#兴庆区的,别的区得自行修改了

#print('各层座位数量',end = '')
s = json.loads(r.text)['seat']
print('循环次数:')
print(ii)
for i in my_list:
if s[i] == 0:
print(i)
if reserve(self,i,'north4east') == 1:
return(0)
for i in s:
if s[i] == 0:
print(i)
if reserve(self,i,'north4east') == 1:
return(0)
time.sleep(2)

if __name__ == '__main__':

guoguo = XJTUUser('./config.json')
zxz = XJTUUser('./config_zxz.json')
dt = '2020-08-20 06:00:02'
ts = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S")))
t = int(time.time())
cha = ts - t
while cha >0:
t = int(time.time())
cha = ts - t
time.sleep(1)
if(cha%100 == 0):
print(cha)
print("开始")

#guoguo.login()
#别的人就分别建立新的结构体就行
#ruo = XJTUUser('./config_1.json')
guoguo.login()
zxz.login()
exit(0)

config文件

{
    "username": "netid",
    "password": "",
    "person_id":"217611****",
    "headers": {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"
    },
    "cookies": {}
}

考研加油!

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2015-2024 galaxy
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信