-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.py
More file actions
195 lines (165 loc) · 5.94 KB
/
utilities.py
File metadata and controls
195 lines (165 loc) · 5.94 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
"""This python file will handle some extra functions."""
import json
import sys
import threading
from os.path import exists
import yaml
from playsound import playsound
from yaml import SafeLoader
def config_file_generator():
"""Generate the template of config file"""
with open('config.yml', 'w', encoding="utf8") as f:
f.write("""# ++--------------------------------++
# | MiramarTicketBot v1.0 |
# | Made by LD (MIT License) |
# ++--------------------------------++
# NOTES:
# If any ding sound is played while the program running
# It means that there's a manual action is required.
# Login information
# Note that you have to finish the captcha manually.
email: ''
password: ''
# Tickets date and time
# Enter the date and time you want to book.
date: '8/3'
time: '23:10'
# Tickets selection
# Please enter the amount of ticket you want to book.
imax_adults: 2
imax_students: 0
imax_seniors: 0
imax_disabled: 0
# Seat selection
# Please enter the seat you want to book.
seats:
- 'G23'
- 'G24'
# Invoice settings (optional)
# Invoice number should start with / and contains 7 character behind.
# Example: /A123456
invoice: ''
# Headless mode
# If you want to run this script in headless mode, please set this to true.
headless: false
"""
)
sys.exit()
def read_config():
"""Read config file.
Check if config file exists, if not, create one.
if exists, read config file and return config with dict type.
:rtype: dict
"""
if not exists('./config.yml'):
print("Config file not found, create one by default.\nPlease finish filling config.yml")
with open('config.yml', 'w', encoding="utf8"):
config_file_generator()
try:
with open('config.yml', 'r', encoding="utf8") as f:
data = yaml.load(f, Loader=SafeLoader)
config = {
'email': data['email'],
'password': data['password'],
'date': data['date'],
'time': data['time'],
'imax_adults': data['imax_adults'],
'imax_students': data['imax_students'],
'imax_seniors': data['imax_seniors'],
'imax_disabled': data['imax_disabled'],
'seats': data['seats'],
'invoice': data['invoice'],
'headless': data['headless']
}
config['cn_date'] = config['date'].replace('/', '月') + '日'
return config
except (KeyError, TypeError):
print(
"An error occurred while reading config.yml, please check if the file is corrected filled.\n"
"If the problem can't be solved, consider delete config.yml and restart the program.\n")
sys.exit()
def cookies_file_generator():
"""Generate the template of cookies files"""
with open('cookies.json', 'w', encoding="utf8") as f:
f.write("""[
{
"name": "__RequestVerificationToken",
"value": "",
"domain": "www.miramarcinemas.tw"
},
{
"name": "ASP.NET_SessionId",
"value": "",
"domain": "www.miramarcinemas.tw"
}
]"""
)
f.close()
sys.exit()
def read_cookies():
"""Read cookies file.
Check if cookies file exists, if not, create one.
if exists, read cookies file and return cookies with list type.
:rtype: list
"""
if not exists('./cookies.json'):
print(
"Cookies file not found, generated by default, please restart the program.")
with open('cookies.json', 'w', encoding="utf8"):
cookies_file_generator()
sys.exit()
try:
with open('cookies.json', 'r', encoding="utf8") as file:
cookies = json.loads(file.read())
file.close()
return cookies
except (KeyError, TypeError):
print(
"An error occurred while reading cookies.json, please check if the file is corrected filled.\n"
"If the problem can't be solved, consider delete cookies.json and restart the program.\n")
sys.exit()
def update_cookies(request_verification_token, asp_net_session_id):
"""Update cookies file.
Update cookies file with new cookies.
:param request_verification_token: __RequestVerificationToken
:param asp_net_session_id: ASP.NET_SessionId
:rtype: None
"""
cookies = read_cookies()
cookies[0]['value'] = request_verification_token
cookies[1]['value'] = asp_net_session_id
with open('cookies.json', 'w', encoding="utf8") as file:
file.write(json.dumps(cookies, indent=4))
file.close()
def get_seats():
config = read_config()
seats = config['seats']
seat_table = {'A': 2, 'B': 3, 'C': 4, 'D': 5, 'E': 6, 'F': 7, 'G': 8, 'H': 10, 'I': 11, 'J': 12,
'K': 13, 'L': 21, 'M': 22}
seats_list = []
for seat in seats:
if seat[0:1] not in seat_table:
print(f"Seat {seat} is not valid, please fix it in config.yml.")
sys.exit()
temp = []
if seat == 'HW1':
temp = [10, 38, seat]
elif seat == 'HW2':
temp = [11, 38, seat]
elif int(seat_table[seat[0:1]]) <= 13 and int(seat[1:]) <= 11:
temp = [seat_table[seat[0:1]], int(seat[1:]), seat]
elif int(seat_table[seat[0:1]]) <= 13 and 14 <= int(seat[1:]) <= 25:
temp = [seat_table[seat[0:1]], int(seat[1:]) + 1, seat]
elif int(seat_table[seat[0:1]]) <= 13 and 28 <= int(seat[1:]) <= 38:
temp = [seat_table[seat[0:1]], int(seat[1:]) + 2, seat]
elif int(seat[1:]) >= 21:
temp = [seat_table[seat[0:1]], int(seat[1:]), seat]
else:
print(f"Seat {seat} is not valid, please fix it in config.yml.")
sys.exit()
seats_list.append(temp)
return seats_list
def play_ding_sound():
"""Play ding sound when manual action is needed."""
thread = threading.Thread(target=playsound, args=('./ding.wav',))
thread.start()