-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
145 lines (125 loc) · 5.22 KB
/
main.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
from dotenv import load_dotenv
import os
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from datetime import datetime
# Load environment variables from .env file
load_dotenv()
# Access the email-related variables
smtp_server = os.environ.get('SMTP_SERVER')
smtp_port = os.environ.get('SMTP_PORT')
email_address = os.environ.get('EMAIL_ADDRESS')
email_password = os.environ.get('SENDER_EMAIL_PASSWORD')
sender_name = os.environ.get('SENDER_NAME')
sender_email = os.environ.get('SENDER_EMAIL')
recipient_email = os.environ.get('RECIPIENT_EMAIL')
appointment_date = os.environ.get('APPOINTMENT_DATE')
visa_email = os.environ.get('VISA_EMAIL')
visa_password = os.environ.get('VISA_PASSWORD')
def parse_date(date_str):
# Define a dictionary to map month names to month numbers
month_map = {
'January': 1, 'February': 2, 'March': 3, 'April': 4, 'May': 5, 'June': 6,
'July': 7, 'August': 8, 'September': 9, 'October': 10, 'November': 11, 'December': 12
}
# Split the date string into day, month, and year parts
day, month_name, year = date_str.split()
# Use the month map to get the month number
month = month_map[month_name]
# Create a datetime object
parsed_date = datetime(int(year), month, int(day))
return parsed_date
def send_email(content):
try:
#Setup the MIME
message = MIMEMultipart()
message['From'] = sender_email
message['To'] = recipient_email
message['Subject'] = 'Sistema de Mensajes' #The subject line
#The body and the attachments for the mail
message.attach(MIMEText(content, 'plain'))
#Create SMTP session for sending the mail
session = smtplib.SMTP(smtp_server, smtp_port) #use gmail with port
session.starttls() #enable security
session.login(email_address, email_password) #login with mail_id and password
text = message.as_string()
session.sendmail(sender_email, email_address, text)
session.quit()
except Exception as e:
print(f"An error occurred: {str(e)}")
def get_available_date(element):
for row in element.find_elements(By.TAG_NAME, "td"):
class_t = row.text, row.get_attribute("class")
if row.text.strip() and not "disabled" in class_t[1]:
month = row.parent.find_elements(By.CLASS_NAME, "ui-datepicker-title")[0].text
date = row.text
return f"{date} {month}"
return None
if __name__=="__main__":
driver = webdriver.Chrome()
driver.get("https://ais.usvisa-info.com/en-ar/niv/users/sign_in")
time.sleep(2)
email_input = driver.find_element(By.ID,"user_email")
email_input.send_keys(visa_email)
time.sleep(2)
password_input = driver.find_element(By.ID,"user_password")
password_input.send_keys(visa_password)
time.sleep(2)
policy_confirm_input = driver.find_element(By.CLASS_NAME, "icheckbox")
policy_confirm_input.click()
time.sleep(2)
login_form = driver.find_element(By.TAG_NAME,"form")
login_form.submit()
time.sleep(2)
continue_button = driver.find_element(By.XPATH,"//a[@href='/en-ar/niv/schedule/51699052/continue_actions']")
continue_button.click()
time.sleep(2)
schedule_button = driver.find_element(By.CLASS_NAME,"fa-calendar-minus")
schedule_button.click()
time.sleep(2)
continue_button = driver.find_element(By.XPATH,"//a[@href='/en-ar/niv/schedule/51699052/appointment']")
continue_button.click()
time.sleep(2)
continue_button2 = driver.find_element(By.NAME,"commit")
continue_button2.click()
time.sleep(2)
location_select = driver.find_element(By.ID,"appointments_consulate_appointment_facility_id")
location_select.click()
time.sleep(2)
location_option = driver.find_element(By.XPATH,"//option[contains(text(), 'Buenos Aires')]")
location_option.click()
time.sleep(2)
location_select = driver.find_element(By.ID,"appointments_consulate_appointment_date")
location_select.click()
time.sleep(2)
#element = driver.find_element(By.ID,"consulate_date_time")
calendar = driver.find_element(By.ID,"ui-datepicker-div")
appointment_date = parse_date(appointment_date)
while True:
i = 0
first_calendar = calendar.find_element(By.CLASS_NAME,"ui-datepicker-group-first")
second_calendar = calendar.find_element(By.CLASS_NAME,"ui-datepicker-group-last")
available_date = get_available_date(first_calendar)
if not available_date:
available_date = get_available_date(second_calendar)
if available_date:
new_data = parse_date(available_date)
if new_data > appointment_date:
print("NEW DATE")
send_email(available_date)
else:
print("New date but later")
break
if i > 12:
print("couldn't find")
send_email("Didn't find a date")
break
i += 1
time.sleep(2)
next_button = calendar.find_element(By.CLASS_NAME,"ui-datepicker-next")
next_button.click()
time.sleep(2)