-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
207 lines (178 loc) · 7.67 KB
/
app.py
File metadata and controls
207 lines (178 loc) · 7.67 KB
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
"""This python file is the main file of the program."""
import time
from selenium import webdriver
from selenium.common import TimeoutException, NoAlertPresentException, NoSuchElementException
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
import utilities as utils
config = utils.read_config()
options = webdriver.ChromeOptions()
if config.get("headless"):
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)
driver.maximize_window()
wait = WebDriverWait(driver, 10)
def driver_send_keys(locator, key):
"""Send keys to element.
:param locator: Locator of element.
:param key: Keys to send.
"""
wait.until(ec.presence_of_element_located(locator)).send_keys(key)
def driver_click(locator):
"""Click element.
:param locator: Locator of element.
"""
wait.until(ec.presence_of_element_located(locator)).click()
def driver_select(locator, select_value, select_by='index'):
"""Select element.
:param locator: Locator of element.
:param select_value: Value to select.
:param select_by: Select by index, value or visible_text. Default is index.
"""
select = Select(wait.until(ec.presence_of_element_located(locator)))
if select_by == 'index':
select.select_by_index(select_value)
elif select_by == 'value':
select.select_by_value(select_value)
elif select_by == 'visible_text':
select.select_by_visible_text(select_value)
def driver_screenshot(locator, path):
"""Take screenshot of element.
:param locator: Locator of element.
:param path: Path to save screenshot.
"""
wait.until(ec.presence_of_element_located(locator)).screenshot(path)
def driver_get_text(locator):
"""Get text of element.
:param locator: Locator of element.
:return: Text of element.
"""
return wait.until(ec.presence_of_element_located(locator)).text
def driver_get_background_color(locator):
"""Get background color of element.
:param locator: Locator of element.
:return: Background color of element.
"""
return wait.until(ec.presence_of_element_located(locator)).value_of_css_property(
'background-color')
def login():
driver.get('https://www.miramarcinemas.tw/Member/Login')
cookies = utils.read_cookies()
for cookie in cookies:
driver.add_cookie(cookie)
driver.refresh()
try:
alert = driver.switch_to.alert
alert.accept()
except (TimeoutException, NoAlertPresentException):
print('-------------------------------------')
print(
"Login Failed, cookies might not be correct or expired, please login manually.")
print(
"If you can't login from this browser, please login from your local browser then copy cookies value to cookies.json.")
driver.delete_all_cookies()
driver.refresh()
driver_send_keys((By.ID, "Account"), config['email'])
driver_send_keys((By.ID, "Password"), config['password'])
utils.play_ding_sound()
print('Waiting for login...')
while True:
try:
alert = driver.switch_to.alert
alert.accept()
request_verification_token = driver.get_cookie('__RequestVerificationToken')[
'value']
asp_net_session_id = driver.get_cookie('ASP.NET_SessionId')['value']
utils.update_cookies(request_verification_token, asp_net_session_id)
print('Success! new cookies saved to cookies.json.')
break
except (TimeoutException, NoAlertPresentException):
continue
print('-------------------------------------')
print("Successfully Logged In!")
grab_tickets()
def grab_tickets():
driver.get('https://www.miramarcinemas.tw/Timetable/Index?cinema=imax')
print('-------------------------------------')
print('Trying to select desired date...')
while True:
try:
driver.find_element(By.XPATH, f"//*[contains(text(), '{config.get('date')}')]").click()
print(f"Date selected: {config.get('date')}")
break
except (TimeoutException, NoSuchElementException):
time.sleep(1)
driver.refresh()
print('Trying to select desired time...')
try:
selected_time = driver.find_element(By.XPATH,
f"//div[contains(@class, '{config['cn_date']}')]"
f"//a[contains(text(), '{config['time']}')]")
print(f"Time selected: {config.get('time')}")
driver.get(selected_time.get_attribute('href'))
except (TimeoutException, NoSuchElementException):
print('Time not available, please select manually.')
utils.play_ding_sound()
while True:
try:
wait.until(ec.url_contains('https://www.miramarcinemas.tw/Booking/'))
break
except TimeoutException:
continue
print('-------------------------------------')
if config.get("imax_adults") > 0:
driver_select((By.ID, "ticket_type_select_0149"), config.get("imax_adults"))
print(f"Imax_adults tickets: {config.get('imax_adults')}")
if config.get("imax_students") > 0:
driver_select((By.ID, "ticket_type_select_0150"), config.get("imax_students"))
print(f"Imax_adults tickets: {config.get('imax_students')}")
if config.get("imax_seniors") > 0:
driver_select((By.ID, "ticket_type_select_0050"), config.get("imax_seniors"))
print(f"Imax_adults tickets: {config.get('imax_seniors')}")
if config.get("imax_disabled") > 0:
driver_select((By.ID, "ticket_type_select_0051"), config.get("imax_disabled"))
print(f"Imax_adults tickets: {config.get('imax_disabled')}")
print(
f'Total tickets: {config["imax_adults"] + config["imax_students"] + config["imax_seniors"] + config["imax_disabled"]}')
print('-------------------------------------')
driver_click(
(By.XPATH, "/html/body/div[1]/section[3]/section/div/div/div[2]/form/div[5]/label"))
seats = utils.get_seats()
checkout = True
for seat in seats:
selected = (By.XPATH,
f"/html/body/div[1]/section[3]/section[2]/div/div/div/table/tbody/tr[{seat[0]}]/td[{seat[1]}]")
if driver_get_background_color(selected) == 'rgba(128, 128, 128, 1)':
print(f"Seat {seat[2]} is not available.")
checkout = False
utils.play_ding_sound()
continue
else:
driver_click(selected)
print(f"Seat {seat[2]} selected.")
if not checkout:
print('-------------------------------------')
print('Some seats are not available, please select manually.')
while True:
try:
wait.until(ec.url_contains('https://www.miramarcinemas.tw/Booking/Confirm'))
break
except TimeoutException:
continue
else:
driver_click((By.XPATH, "/html/body/div[1]/section[3]/section[2]/div/form/div/label[2]"))
if config.get("invoice") != '':
driver_send_keys((By.ID, "invoice_vehicle"), config.get("invoice"))
driver_send_keys((By.ID, "AgreeRule"), Keys.SPACE)
utils.play_ding_sound()
print('-------------------------------------')
print('All done! Please checkout manually.')
print('Seats have been locked for 10 minutes, take your time.')
if __name__ == "__main__":
login()
time.sleep(999999)
print('Time out, quit.')
driver.quit()