forked from uxDaniel/visa_rescheduler
-
Notifications
You must be signed in to change notification settings - Fork 95
/
visa.py
304 lines (273 loc) · 11.6 KB
/
visa.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
import time
import json
import random
import requests
import configparser
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as Wait
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
from embassy import *
config = configparser.ConfigParser()
config.read('config.ini')
# Personal Info:
# Account and current appointment info from https://ais.usvisa-info.com
USERNAME = config['PERSONAL_INFO']['USERNAME']
PASSWORD = config['PERSONAL_INFO']['PASSWORD']
# Find SCHEDULE_ID in re-schedule page link:
# https://ais.usvisa-info.com/en-am/niv/schedule/{SCHEDULE_ID}/appointment
SCHEDULE_ID = config['PERSONAL_INFO']['SCHEDULE_ID']
# Target Period:
PRIOD_START = config['PERSONAL_INFO']['PRIOD_START']
PRIOD_END = config['PERSONAL_INFO']['PRIOD_END']
# Embassy Section:
YOUR_EMBASSY = config['PERSONAL_INFO']['YOUR_EMBASSY']
EMBASSY = Embassies[YOUR_EMBASSY][0]
FACILITY_ID = Embassies[YOUR_EMBASSY][1]
REGEX_CONTINUE = Embassies[YOUR_EMBASSY][2]
# Notification:
# Get email notifications via https://sendgrid.com/ (Optional)
SENDGRID_API_KEY = config['NOTIFICATION']['SENDGRID_API_KEY']
# Get push notifications via https://pushover.net/ (Optional)
PUSHOVER_TOKEN = config['NOTIFICATION']['PUSHOVER_TOKEN']
PUSHOVER_USER = config['NOTIFICATION']['PUSHOVER_USER']
# Get push notifications via PERSONAL WEBSITE http://yoursite.com (Optional)
PERSONAL_SITE_USER = config['NOTIFICATION']['PERSONAL_SITE_USER']
PERSONAL_SITE_PASS = config['NOTIFICATION']['PERSONAL_SITE_PASS']
PUSH_TARGET_EMAIL = config['NOTIFICATION']['PUSH_TARGET_EMAIL']
PERSONAL_PUSHER_URL = config['NOTIFICATION']['PERSONAL_PUSHER_URL']
# Time Section:
minute = 60
hour = 60 * minute
# Time between steps (interactions with forms)
STEP_TIME = 0.5
# Time between retries/checks for available dates (seconds)
RETRY_TIME_L_BOUND = config['TIME'].getfloat('RETRY_TIME_L_BOUND')
RETRY_TIME_U_BOUND = config['TIME'].getfloat('RETRY_TIME_U_BOUND')
# Cooling down after WORK_LIMIT_TIME hours of work (Avoiding Ban)
WORK_LIMIT_TIME = config['TIME'].getfloat('WORK_LIMIT_TIME')
WORK_COOLDOWN_TIME = config['TIME'].getfloat('WORK_COOLDOWN_TIME')
# Temporary Banned (empty list): wait COOLDOWN_TIME hours
BAN_COOLDOWN_TIME = config['TIME'].getfloat('BAN_COOLDOWN_TIME')
# CHROMEDRIVER
# Details for the script to control Chrome
LOCAL_USE = config['CHROMEDRIVER'].getboolean('LOCAL_USE')
# Optional: HUB_ADDRESS is mandatory only when LOCAL_USE = False
HUB_ADDRESS = config['CHROMEDRIVER']['HUB_ADDRESS']
SIGN_IN_LINK = f"https://ais.usvisa-info.com/{EMBASSY}/niv/users/sign_in"
APPOINTMENT_URL = f"https://ais.usvisa-info.com/{EMBASSY}/niv/schedule/{SCHEDULE_ID}/appointment"
DATE_URL = f"https://ais.usvisa-info.com/{EMBASSY}/niv/schedule/{SCHEDULE_ID}/appointment/days/{FACILITY_ID}.json?appointments[expedite]=false"
TIME_URL = f"https://ais.usvisa-info.com/{EMBASSY}/niv/schedule/{SCHEDULE_ID}/appointment/times/{FACILITY_ID}.json?date=%s&appointments[expedite]=false"
SIGN_OUT_LINK = f"https://ais.usvisa-info.com/{EMBASSY}/niv/users/sign_out"
JS_SCRIPT = ("var req = new XMLHttpRequest();"
f"req.open('GET', '%s', false);"
"req.setRequestHeader('Accept', 'application/json, text/javascript, */*; q=0.01');"
"req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');"
f"req.setRequestHeader('Cookie', '_yatri_session=%s');"
"req.send(null);"
"return req.responseText;")
def send_notification(title, msg):
print(f"Sending notification!")
if SENDGRID_API_KEY:
message = Mail(from_email=USERNAME, to_emails=USERNAME, subject=msg, html_content=msg)
try:
sg = SendGridAPIClient(SENDGRID_API_KEY)
response = sg.send(message)
print(response.status_code)
print(response.body)
print(response.headers)
except Exception as e:
print(e.message)
if PUSHOVER_TOKEN:
url = "https://api.pushover.net/1/messages.json"
data = {
"token": PUSHOVER_TOKEN,
"user": PUSHOVER_USER,
"message": msg
}
requests.post(url, data)
if PERSONAL_SITE_USER:
url = PERSONAL_PUSHER_URL
data = {
"title": "VISA - " + str(title),
"user": PERSONAL_SITE_USER,
"pass": PERSONAL_SITE_PASS,
"email": PUSH_TARGET_EMAIL,
"msg": msg,
}
requests.post(url, data)
def auto_action(label, find_by, el_type, action, value, sleep_time=0):
print("\t"+ label +":", end="")
# Find Element By
match find_by.lower():
case 'id':
item = driver.find_element(By.ID, el_type)
case 'name':
item = driver.find_element(By.NAME, el_type)
case 'class':
item = driver.find_element(By.CLASS_NAME, el_type)
case 'xpath':
item = driver.find_element(By.XPATH, el_type)
case _:
return 0
# Do Action:
match action.lower():
case 'send':
item.send_keys(value)
case 'click':
item.click()
case _:
return 0
print("\t\tCheck!")
if sleep_time:
time.sleep(sleep_time)
def start_process():
# Bypass reCAPTCHA
driver.get(SIGN_IN_LINK)
time.sleep(STEP_TIME)
Wait(driver, 60).until(EC.presence_of_element_located((By.NAME, "commit")))
auto_action("Click bounce", "xpath", '//a[@class="down-arrow bounce"]', "click", "", STEP_TIME)
auto_action("Email", "id", "user_email", "send", USERNAME, STEP_TIME)
auto_action("Password", "id", "user_password", "send", PASSWORD, STEP_TIME)
auto_action("Privacy", "class", "icheckbox", "click", "", STEP_TIME)
auto_action("Enter Panel", "name", "commit", "click", "", STEP_TIME)
Wait(driver, 60).until(EC.presence_of_element_located((By.XPATH, "//a[contains(text(), '" + REGEX_CONTINUE + "')]")))
print("\n\tlogin successful!\n")
def reschedule(date):
time = get_time(date)
driver.get(APPOINTMENT_URL)
headers = {
"User-Agent": driver.execute_script("return navigator.userAgent;"),
"Referer": APPOINTMENT_URL,
"Cookie": "_yatri_session=" + driver.get_cookie("_yatri_session")["value"]
}
data = {
"utf8": driver.find_element(by=By.NAME, value='utf8').get_attribute('value'),
"authenticity_token": driver.find_element(by=By.NAME, value='authenticity_token').get_attribute('value'),
"confirmed_limit_message": driver.find_element(by=By.NAME, value='confirmed_limit_message').get_attribute('value'),
"use_consulate_appointment_capacity": driver.find_element(by=By.NAME, value='use_consulate_appointment_capacity').get_attribute('value'),
"appointments[consulate_appointment][facility_id]": FACILITY_ID,
"appointments[consulate_appointment][date]": date,
"appointments[consulate_appointment][time]": time,
}
r = requests.post(APPOINTMENT_URL, headers=headers, data=data)
if(r.text.find('Successfully Scheduled') != -1):
title = "SUCCESS"
msg = f"Rescheduled Successfully! {date} {time}"
else:
title = "FAIL"
msg = f"Reschedule Failed!!! {date} {time}"
return [title, msg]
def get_date():
# Requesting to get the whole available dates
session = driver.get_cookie("_yatri_session")["value"]
script = JS_SCRIPT % (str(DATE_URL), session)
content = driver.execute_script(script)
return json.loads(content)
def get_time(date):
time_url = TIME_URL % date
session = driver.get_cookie("_yatri_session")["value"]
script = JS_SCRIPT % (str(time_url), session)
content = driver.execute_script(script)
data = json.loads(content)
time = data.get("available_times")[-1]
print(f"Got time successfully! {date} {time}")
return time
def is_logged_in():
content = driver.page_source
if(content.find("error") != -1):
return False
return True
def get_available_date(dates):
# Evaluation of different available dates
def is_in_period(date, PSD, PED):
new_date = datetime.strptime(date, "%Y-%m-%d")
result = ( PED > new_date and new_date > PSD )
# print(f'{new_date.date()} : {result}', end=", ")
return result
PED = datetime.strptime(PRIOD_END, "%Y-%m-%d")
PSD = datetime.strptime(PRIOD_START, "%Y-%m-%d")
for d in dates:
date = d.get('date')
if is_in_period(date, PSD, PED):
return date
print(f"\n\nNo available dates between ({PSD.date()}) and ({PED.date()})!")
def info_logger(file_path, log):
# file_path: e.g. "log.txt"
with open(file_path, "a") as file:
file.write(str(datetime.now().time()) + ":\n" + log + "\n")
if LOCAL_USE:
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
else:
driver = webdriver.Remote(command_executor=HUB_ADDRESS, options=webdriver.ChromeOptions())
if __name__ == "__main__":
first_loop = True
while 1:
LOG_FILE_NAME = "log_" + str(datetime.now().date()) + ".txt"
if first_loop:
t0 = time.time()
total_time = 0
Req_count = 0
start_process()
first_loop = False
Req_count += 1
try:
msg = "-" * 60 + f"\nRequest count: {Req_count}, Log time: {datetime.today()}\n"
print(msg)
info_logger(LOG_FILE_NAME, msg)
dates = get_date()
if not dates:
# Ban Situation
msg = f"List is empty, Probabely banned!\n\tSleep for {BAN_COOLDOWN_TIME} hours!\n"
print(msg)
info_logger(LOG_FILE_NAME, msg)
send_notification("BAN", msg)
driver.get(SIGN_OUT_LINK)
time.sleep(BAN_COOLDOWN_TIME * hour)
first_loop = True
else:
# Print Available dates:
msg = ""
for d in dates:
msg = msg + "%s" % (d.get('date')) + ", "
msg = "Available dates:\n"+ msg
print(msg)
info_logger(LOG_FILE_NAME, msg)
date = get_available_date(dates)
if date:
# A good date to schedule for
END_MSG_TITLE, msg = reschedule(date)
break
RETRY_WAIT_TIME = random.randint(RETRY_TIME_L_BOUND, RETRY_TIME_U_BOUND)
t1 = time.time()
total_time = t1 - t0
msg = "\nWorking Time: ~ {:.2f} minutes".format(total_time/minute)
print(msg)
info_logger(LOG_FILE_NAME, msg)
if total_time > WORK_LIMIT_TIME * hour:
# Let program rest a little
send_notification("REST", f"Break-time after {WORK_LIMIT_TIME} hours | Repeated {Req_count} times")
driver.get(SIGN_OUT_LINK)
time.sleep(WORK_COOLDOWN_TIME * hour)
first_loop = True
else:
msg = "Retry Wait Time: "+ str(RETRY_WAIT_TIME)+ " seconds"
print(msg)
info_logger(LOG_FILE_NAME, msg)
time.sleep(RETRY_WAIT_TIME)
except:
# Exception Occured
msg = f"Break the loop after exception!\n"
END_MSG_TITLE = "EXCEPTION"
break
print(msg)
info_logger(LOG_FILE_NAME, msg)
send_notification(END_MSG_TITLE, msg)
driver.get(SIGN_OUT_LINK)
driver.stop_client()
driver.quit()