-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathak2.py
325 lines (224 loc) · 9.87 KB
/
ak2.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# [Project AutoKupis2] Auto-sugang project GUI.
# 0.1.0va, 19.12.29. First launched.
# written by acoustikue(SukJoon Oh)
# __ _ __
# ____ __________ __ _______/ /_(_) /____ _____
# / __ `/ ___/ __ \/ / / / ___/ __/ / //_/ / / / _ \
# / /_/ / /__/ /_/ / /_/ (__ ) /_/ / ,< / /_/ / __/
# \__,_/\___/\____/\__,_/____/\__/_/_/|_|\__,_/\___/
#
# Visual Studio Code
#
# PyQt
from PyQt5.QtWidgets import *
from PyQt5 import QtGui
from PyQt5 import uic
from PyQt5.QtCore import pyqtSignal, pyqtSlot
from PyQt5.QtWidgets import QMessageBox
import os, sys
import time
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.common.exceptions import WebDriverException
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import UnexpectedAlertPresentException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.firefox.options import Options # for headless option
from selenium.webdriver.common.keys import Keys
import threading
# for import from parent directory
sys.path.append(
os.path.dirname(
os.path.abspath(os.path.dirname(__file__))) )
from config import *
import dynamic as dy
# UI file
# AutoKupis2GUI_MainWndFile = QT_UI_MAIN
# File link
# AutoKupis2GUI_MainWnd, QtBaseClass = uic.loadUiType(AutoKupis2GUI_MainWndFile)
from auto_kupis_2_ui import *
from ak2_worker import *
class AutoKupis2GUI_MAIN(QMainWindow, Ui_AutoKupisWnd):
def __init__(self):
QMainWindow.__init__(self)
self.setWindowIcon(QtGui.QIcon(QT_UI_ICON))
self.setupUi(self)
self.show()
#
# Thread background object
self.driver_check_handler = AutoKupis2_DriverCheckHandler()
self.login_test_handler = AutoKupis2_LoginTestHandler()
self.run_request_handler = AutoKupis2_RunRequestHandler()
# UI connect
self.driver_check_handler._signal.connect(self.updateUI)
self.login_test_handler._signal.connect(self.updateUI)
self.run_request_handler._signal.connect(self.updateUI)
# First show user status
self.statusBar().showMessage("No ID and password has been set.")
# First tab
# self.pushButton.clicked.connect(self.buttonWindow1_onClick)
# self.ID_EDIT.returnPressed.connect(self.setID)
self.LOGIN_TEST.clicked.connect(self.loginTest)
self.SET_USER.clicked.connect(self.setUser)
self.ID_EDIT.returnPressed.connect(self.setUser)
self.PW_EDIT.returnPressed.connect(self.setUser)
self.RESET_USER.clicked.connect(self.resetUser)
self.DRIVER_CHECK.clicked.connect(self.driverCheck)
# Second tab
self.ADD_LIST.clicked.connect(self.addList)
self.ADD_LIST_BOX.returnPressed.connect(self.addList)
self.DELETE_LIST.clicked.connect(self.deleteList)
self.RESET_LIST.clicked.connect(self.resetList)
self.DELAY_BOX.valueChanged.connect(self.delaySet)
# run
self.RUN.clicked.connect(self.runRequest)
self.HEADLESS_CHECK.clicked.connect(self.toggleHeadlessMode)
self.OPEN_AND_WAIT.clicked.connect(self.openAndWait)
self.QUIT.clicked.connect(self.quitBrowser)
#
# Thread signal/slot
@pyqtSlot(dict)
def updateUI(self, sig):
# print('\tSignal received. {}'.format(sig))
try:
# General
if sig['type'] == 'message_box':
QMessageBox.warning(self, sig['title'], sig['body'])
elif sig['type'] == 'status_bar':
self.statusBar().showMessage('{}'.format(sig['msg']))
# Specific
elif sig['type'] == 'STAT_1':
self.STAT_1.setText('{}'.format(sig['text']))
elif sig['type'] == 'STAT_2':
self.STAT_2.setText('{}'.format(sig['text']))
elif sig['type'] == 'STAT_3':
self.STAT_3.setText('{}'.format(sig['text']))
except:
pass
# self.statusBar().showMessage('{}'.format(status))
# Button Actions
def setUser(self):
dy.VAR_USER_ID = self.ID_EDIT.text()
dy.VAR_USER_PW = self.PW_EDIT.text()
if dy.VAR_USER_PW == '' or dy.VAR_USER_ID == '':
# Alert
QMessageBox.warning(self, "User information", "User information cannot be blank.")
self.statusBar().showMessage("User information cannot be blank.")
# Reset
dy.VAR_USER_ID = dy.VAR_USER_PW = ''
dy.VAR_USER_OK = False
else:
self.ID_EDIT.setText('')
self.PW_EDIT.setText('')
self.statusBar().showMessage("Login info set to ID({id}), PW({pw})".format(id=dy.VAR_USER_ID, pw=('*' * len(dy.VAR_USER_PW))))
self.STAT_2.setText("User info: ID({id}), PW({pw})".format(id=dy.VAR_USER_ID, pw=('*' * len(dy.VAR_USER_PW))))
dy.VAR_USER_OK = True
def resetUser(self):
# Reset
dy.VAR_USER_ID = ''
dy.VAR_USER_PW = ''
dy.VAR_USER_OK = False
# Display
self.STAT_2.setText("User info: Unknown")
self.statusBar().showMessage("User information has been successfully reset.")
self.ID_EDIT.setText('')
self.PW_EDIT.setText('')
# Driver check
def driverCheck(self):
self.driver_check_handler.start()
def loginTest(self):
self.login_test_handler.start()
#
# Second tab
def addList(self):
lecture_code = self.ADD_LIST_BOX.text()
if lecture_code.isdigit() == False:
self.ADD_LIST_BOX.setText('')
self.statusBar().showMessage("Only number can be added.")
else:
if len(lecture_code) is not 4:
self.ADD_LIST_BOX.setText('')
self.statusBar().showMessage("Lecture code is 4 digit number.")
else:
# Add to dynamics
dy.VAR_COURSE.append(lecture_code)
self.TARGET_LIST.addItem(lecture_code)
# Clear the box
self.ADD_LIST_BOX.setText('')
self.statusBar().showMessage("Added to the list.")
def deleteList(self):
try:
sel = self.TARGET_LIST.currentItem().text()
sel_row = self.TARGET_LIST.currentRow()
dy.VAR_COURSE.remove(sel) # Delete from the list
self.TARGET_LIST.takeItem(sel_row)
self.statusBar().showMessage("Selected item deleted.")
except:
self.statusBar().showMessage("Error.")
def resetList(self):
dy.VAR_COURSE = []
# display
self.TARGET_LIST.clear()
self.statusBar().showMessage("List cleared.")
# Act
def toggleHeadlessMode(self):
if self.HEADLESS_CHECK.isChecked():
dy.VAR_HEADLESS = True
self.statusBar().showMessage("Headless mode enabled.")
else:
dy.VAR_HEADLESS = False
self.statusBar().showMessage("Headless mode disabled.")
def openAndWait(self):
if dy.VAR_USER_OK == False:
self.statusBar().showMessage("User info unknown.")
QMessageBox.warning(self, "User information", "User info unknown.")
elif dy.VAR_DRIVER_CHECK == False:
self.statusBar().showMessage("Check the driver status.")
QMessageBox.warning(self, "Driver status", "Check the driver status.")
else:
try:
options = Options()
options.headless = dy.VAR_HEADLESS
dy.VAR_CURRENT_DRIVER = webdriver.Firefox(options=options, executable_path=FIREFOX_WEBDRIVER)
dy.VAR_CURRENT_DRIVER.get(KUPIS_LOGIN_URL)
dy.VAR_CURRENT_DRIVER.find_element_by_name('stdNo').send_keys(dy.VAR_USER_ID)
dy.VAR_CURRENT_DRIVER.find_element_by_name('pwd').send_keys(dy.VAR_USER_PW)
# executing script
dy.VAR_CURRENT_DRIVER.execute_script('Login();')
dy.VAR_CURRENT_DRIVER.execute_script('document.onkeydown = function() {};') # set an empty function
# New tab
# body = dy.VAR_CURRENT_DRIVER.find_element_by_tag_name("body")
# body.send_keys(Keys.CONTROL + 't')
dy.VAR_CURRENT_DRIVER.find_element_by_tag_name("body").send_keys(Keys.CONTROL + 't')
if dy.VAR_HEADLESS:
print('\tBrowser opened as headless')
print('\tWaiting...')
except Exception as e:
self.statusBar().showMessage("Browser open failed.")
def quitBrowser(self):
# Close browser
try: dy.VAR_CURRENT_DRIVER.quit()
except: self.statusBar().showMessage("Browser close failed.")
finally:
dy.VAR_CURRENT_DRIVER = None
# self.DELAY_BOX.valueChanged.connect(self.delaySet)
def delaySet(self):
# b_hdle.set_page_load_timeout(0.25)
dy.VAR_DELAY = self.DELAY_BOX.value()
try:
dy.VAR_CURRENT_DRIVER.set_page_load_timeout(dy.VAR_DELAY)
self.statusBar().showMessage("Delay set to {0}".format(dy.VAR_DELAY))
except:
self.statusBar().showMessage("No browser handle loaded.")
# Main opertion
def runRequest(self):
if dy.VAR_DRIVER_CHECK == False:
self.statusBar().showMessage("Check the driver status.")
QMessageBox.warning(self, "Driver status", "Check the driver status.")
self.run_request_handler.start()
# Test & Debug
if __name__ == "__main__":
app = QApplication(sys.argv)
xwin = AutoKupis2GUI_MAIN()
app.exec()