-
Notifications
You must be signed in to change notification settings - Fork 0
/
hyundai_bot.py
76 lines (52 loc) · 2.18 KB
/
hyundai_bot.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
import json, time, platform, zlib
from types import SimpleNamespace
from seleniumwire import webdriver
class HyundaiBot:
def __init__(self, config):
self.config = config
def get_info(self):
self.create_driver()
self.login()
info = self.query_info()
self.driver.quit()
del self.driver
return info
def create_driver(self):
options = webdriver.FirefoxOptions()
options.add_argument('--headless')
if self.config.driver_path:
filename = self.config.driver_path
else:
filename = self.driver_for_system()
self.driver = webdriver.Firefox(options=options, executable_path=filename)
def driver_for_system(self):
machine, system = platform.machine(), platform.system()
filename = f'./drivers/geckodriver-{system}-{machine}'
return filename
def login(self):
self.driver.get(self.config.main_uri)
time.sleep(1)
self.driver.get(self.config.login_uri)
time.sleep(1)
self.wait(self.config.redirected_login_uri)
email_element = self.driver.find_element_by_xpath(self.config.email_xpath)
password_element = self.driver.find_element_by_xpath(self.config.password_xpath)
button_element = self.driver.find_element_by_xpath(self.config.button_xpath)
email_element.send_keys(self.config.email)
password_element.send_keys(self.config.password)
button_element.click()
self.wait(self.config.main_uri)
time.sleep(1)
def query_info(self):
self.driver.get(self.config.contract_uri)
time.sleep(1)
for request in self.driver.requests:
if request.response and request.url == self.config.query_uri:
try:
decoded_info = request.response.body.decode('utf-8')
except UnicodeDecodeError:
decoded_info = zlib.decompress(request.response.body, 15+32).decode('utf-8')
return json.loads(decoded_info, object_hook=lambda d: SimpleNamespace(**d))
def wait(self, uri):
while(self.driver.current_url != uri):
pass