-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoLibrary.py
executable file
·194 lines (162 loc) · 6.49 KB
/
autoLibrary.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
----------------------------------------------------------------------------------------------------
* Project Name : AutoAoxiang
* File Name : autoLibrary.py
* Description : Reserve library room automatically.
* Create Time : 2021-01-03 18:39:37
* Version : 1.0
* Author : Steve X
* GitHub : https://github.com/Steve-Xyh/AutoAoxiang
----------------------------------------------------------------------------------------------------
* Notice
-
-
----------------------------------------------------------------------------------------------------
'''
try:
import sys
import json
import datetime
import getpass
import threading
import argh
# import schedule
import functions.formatString as fs
import functions.lib_room as lib_room
import functions.loginAoxiang as aoxiang
from time import sleep
# from functions.getInfo import get_info
except ModuleNotFoundError:
error_info = '缺少函数库, 运行 pip install -r requirements.txt 命令后重试'
print(error_info)
exit(-1)
def log(dic):
"""
:param dic: log dict(e.g. {name: value})
"""
res = datetime.datetime.now().strftime('[\033[0;32;1m%y-%m-%d %H:%M:%S\033[0m]') + ' '
for key in dic:
flg = dic[key] is not None
res += format(key, ' <5')
res += format('\033[0;33;1m' + dic[key] + '\033[0m' if flg else '', ' <30')
print(res)
def reserve(username, password, room: str = '711', i: int = 0):
time = lib_room.get_room_time(i)
room = lib_room.get_room(room)
url_reserve = 'http://202.117.88.170/ClientWeb/pro/ajax/reserve.aspx'
conn, status = aoxiang.login(
user=username,
passwd=password,
url_login='https://uis.nwpu.edu.cn/cas/login?service=http://202.117.88.170/loginall.aspx',
keyword='欢迎',
)
while True:
now = datetime.datetime.now()
now_time = int(now.strftime('%H%M'))
reserve_date = (now+datetime.timedelta(days=2, minutes=10)).strftime('%Y-%m-%d')
if 2355 <= now_time or now_time <= 15 or DEBUG:
try:
res = conn.get(url=url_reserve)
session_id = conn.cookies.get_dict().get('ASP.NET_SessionId')
if res.status_code != 200:
print(res)
print('SSID:'+str(session_id)+'\n')
conn, status = aoxiang.login(
user=username,
passwd=password,
url_login='https://uis.nwpu.edu.cn/cas/login?service=http://202.117.88.170/loginall.aspx'
)
print('RETRIED...')
header = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
# 'Cookie': 'ASP.NET_SessionId=uiwirsdqtscke3qhdpmgnkwy',
'Cookie': 'ASP.NET_SessionId='+str(session_id),
'Host': '202.117.88.170',
'Pragma': 'no-cache',
'Upgrade-Insecure-Requests': '1',
'Referer': 'http://202.117.88.170/ClientWeb/xcus/ic2/Default.aspx',
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
}
data_reserve = {
'dialogid': '',
# 房间号
'dev_id': room['dev_id'],
'lab_id': '857069',
'kind_id': '959784',
'room_id': '',
'type': 'dev',
'prop': '',
'test_id': '',
'term': '',
'number': '',
'classkind': '',
# 房间主题
'test_name': f'学{i+1}',
'min_user': '2',
'max_user': '8',
# 学号
'mb_list': members,
# 时间
'start': f"{reserve_date} {time['start'][:2]}:{time['start'][-2:]}",
'end': f"{reserve_date} {time['end'][:2]}:{time['end'][-2:]}",
'start_time': time['start'],
'end_time': time['end'],
'up_file': '',
'memo': '',
'act': 'set_resv',
}
res = conn.post(
url=url_reserve,
data=data_reserve,
headers=header,
)
response = {
f'{username}: ': room['name'] + f"[{data_reserve['test_name']}]",
reserve_date: f"({data_reserve['start_time']}-{data_reserve['end_time']})",
'msg': json.loads(res.text)['msg'] if res.ok else 'FAILED',
}
status = '成功' in response['msg']
if status or DEBUG:
log(response)
if not res.ok:
print('FAILED')
sleep(2)
except ConnectionError as err:
print(err)
sleep(10)
def init_users(user: dict):
return [threading.Thread(target=reserve, args=[u['userid'], u['passwd'], '710', i]) for i, u in enumerate(user)]
def start_user(user_list):
"""
start all users in user_list and join them
"""
for i in user_list:
try:
i.start()
except AttributeError:
pass
for i in user_list:
try:
i.join()
except (AttributeError, RuntimeError):
pass
print('-'*30)
if __name__ == '__main__':
with open('./.user_passwd', 'r') as pswd:
pswd = pswd.readlines()
user = [{'userid': line.split(',')[0].strip(), 'passwd':line.split(',')[1].strip()} for line in pswd]
user_id = [u['userid'] for u in user]
# parser = argh.ArghParser(description='Library Reservation')
# parser.dispatch()
DEBUG = '--debug' in sys.argv
members = sys.argv[1]
print(members)
users_init = init_users(user)
start_user(users_init)