-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth_controller.py
69 lines (56 loc) · 1.97 KB
/
auth_controller.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
import logging
import os
import threading
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.wait import WebDriverWait
import server.webserver
class AuthController:
driver: webdriver.Firefox = None
automatic_login: bool = False
def __init__(self, automatic_login):
self.automatic_login = automatic_login
def stop(self):
if self.driver:
self.driver.quit()
def element_exists(self, by: By, value: str) -> bool:
try:
self.driver.find_element(by, value)
except NoSuchElementException:
return False
return True
def driver_wait(self, secs: float):
try:
WebDriverWait(self.driver, secs).until(lambda l: False, "")
except TimeoutException:
pass
def run_controller(self):
options = Options()
options.set_preference("media.eme.enabled", True)
options.set_preference("media.gmp-manager.updateEnabled", True)
self.driver = webdriver.Firefox(options=options)
self.driver.get("http://localhost:8080/get_auth.html")
while True:
if self.element_exists(By.ID, "initMessage"):
if self.driver.find_element(By.ID, "initMessage").text == "Login success.":
break
self.driver_wait(0.5)
self.driver_wait(1)
self.stop()
logging.log(logging.INFO, "Login success")
def run_auth():
path = os.getcwd()
print(path)
os.environ["PATH"] += os.pathsep + path
srv = server.webserver.Webserver()
threading.Thread(target=srv.run, args=[]).start()
controller = AuthController(False)
controller.run_controller()
if srv is not None:
srv.web_server.shutdown()
srv.stop()
del srv
if __name__ == "__main__":
run_auth()