-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbookroom.py
198 lines (177 loc) · 7.07 KB
/
bookroom.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
# encoding=utf8
from selenium import webdriver
from selenium.webdriver.support.select import Select
import time
import random
import datetime
import threading
from datetime import timedelta, date
class BookRoomThread(threading.Thread):
startHour = 23
startMin = 58
userName = '2160652004'
passWord = '270749'
roomName = '研究间17'
startTime = '800'
endTime = '2200'
bookDate = date.today() + timedelta(2)
isSlectDate = False
def __init__(self, name):
super().__init__()
self.__running = threading.Event() # 用于停止线程的标识
self.__running.set() # 将running设置为True
self.name = name
self.isStart = False
def stop(self):
if self.driver:
self.driver.close()
self.__running.clear() # 设置为False
def run(self): # 固定名字run !!!必须用固定名
while self.__running.isSet():
if not self.isStart:
self.openbrowser()
self.isStart = True
time.sleep(1)
# 前台开启浏览器模式
def openChrome(self):
# 加启动配置
option = webdriver.ChromeOptions()
option.add_argument('disable-infobars')
self.driver = webdriver.Chrome(chrome_options=option)
# 打开chrome浏览器
# driver = webdriver.Chrome()
def goLogin(self):
try:
username = self.driver.find_element_by_id("username")
password = self.driver.find_element_by_id("password")
username.send_keys(self.userName)
password.send_keys(self.passWord)
self.driver.find_element_by_class_name('btn-success').click()
return True
except Exception as e:
print(str(e))
return False
def goUserCenter(self):
try:
dialogtitle = self.driver.find_element_by_id('ui-id-3')
if dialogtitle.text == '提醒':
self.driver.find_element_by_class_name('ui-button-text-only').click()
return True
else:
return False
except Exception as e:
print(str(e))
return False
def goBookRoomSelection(self):
try:
title = self.driver.find_element_by_class_name('h_title')
if title.text == 'Home Page':
self.driver.find_element_by_link_text('研究小间').click()
return True
else:
return False
except Exception as e:
print(str(e))
return False
def inUserCenter(self):
try:
title = self.driver.find_element_by_class_name('h_title')
if title.text.strip() == '个人中心':
result = self.driver.find_element_by_css_selector('.orange.uni_trans')
if result.text.strip() == '预约成功':
return True
else:
self.driver.find_element_by_link_text('研究小间').click()
return False
else:
return False
except Exception as e:
print(str(e))
return False
def changeTheDate(self):
try:
datetitles = self.driver.find_elements_by_class_name('cld-h-cell')
isFindDateTitle = False
print(self.bookDate)
for i in range(len(datetitles)):
if datetitles[i].get_attribute('date') == str(self.bookDate):
isFindDateTitle = True
if datetitles[i].get_attribute('class').find('cld-d-sel') == -1:
datetitles[i].click()
else:
if self.isSlectDate:
self.isSlectDate = False
if i == 6:
datetitles[5].click()
else:
datetitles[i + 1].click()
else:
self.isSlectDate = True
if not isFindDateTitle:
datetitles[9].click()
else:
roomtitles = self.driver.find_elements_by_class_name('cld-obj-qz')
for i in range(len(roomtitles)):
if roomtitles[i].get_attribute('objname') == self.roomName:
if len(roomtitles[i].find_elements_by_class_name('cld-ttd')) > 2:
roomtitles[i].find_element_by_class_name('cld-ttd-title').click()
break
return True
except Exception as e:
print(str(e))
return False
def comitBook(self):
try:
dialogtitle = self.driver.find_element_by_class_name('ui-dialog-title')
if dialogtitle.text == '预约申请':
st = self.driver.find_elements_by_name('start_time')[2]
et = self.driver.find_elements_by_name('end_time')[2]
Select(st).select_by_value(self.startTime)
Select(et).select_by_value(self.endTime)
self.driver.find_element_by_class_name('submitarea').find_element_by_xpath(
"//input[@value='提交']").click()
return True
else:
return False
except Exception as e:
print(str(e))
return False
def book_room(self):
if self.driver.title == "IC空间管理系统":
if not self.goLogin():
print('not login')
if not self.inUserCenter():
print('not in user center')
if not self.goUserCenter():
print('not go user center')
if not self.comitBook():
print('not go 研究小间')
if not self.changeTheDate():
print('not go commit')
if not self.goBookRoomSelection():
print('not go Date')
else:
print('book success')
self.driver.close()
self.stop()
return
self.start_timer()
# 注册操作
def operationBook(self):
url = "http://seatlib.fjtcm.edu.cn"
self.driver.get(url)
while True:
now = datetime.datetime.now()
if now.hour > self.startHour or (now.hour == self.startHour and now.minute >= self.startMin):
self.driver.refresh()
break
self.driver.refresh()
# 每隔10秒检测一次
time.sleep(10)
self.start_timer()
def start_timer(self, interval=0.5):
self.timer = threading.Timer(interval, self.book_room)
self.timer.start()
def openbrowser(self):
self.openChrome()
self.operationBook()