marriott-notify / app.py
lucianotonet's picture
Upload 3 files
34f9450 verified
raw
history blame
4.67 kB
import os # Import the os module
import logging
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from datetime import datetime
import calendar
# Inicializar o driver do Selenium com o ChromeDriverManager
print("Instalando o ChromeDriver...", end="\n")
chromedriver_path = ChromeDriverManager().install()
print("ChromeDriver instalado com sucesso em " + chromedriver_path + "!",
end="\n")
# options = webdriver.ChromeOptions()
# options.add_experimental_option('w3c', False) # Desativar o protocolo W3C para usar o DevTools Protocol
driver = webdriver.Chrome()
# Habilitar a captura de requisições de rede
driver.execute_cdp_cmd('Network.enable', {})
# Função para encontrar um elemento usando o XPath
def find(xpath):
return WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, xpath)))
# Função para fazer login no Marriott Vacation Club
def login_marriottvc():
driver.get('https://login.marriottvacationclub.com/login')
find("//input[@id='username']").send_keys(os.getenv('MARRIOTT_USERNAME'))
find("//input[@id='password']").send_keys(os.getenv('MARRIOTT_PASSWORD'))
find("//button[@type='submit']").click()
# Função para abrir a página de reserva de pontos
def open_bookvacation_marriottvc():
find("//a[contains(text(), 'USE POINTS')]").click()
find("//li[contains(text(), 'Book Using Club Points')]").click()
# Função para inserir o destino
def input_destination_marriottvc(dest):
destination_box = find("//input[contains(@placeholder, 'Destination')]")
destination_box.send_keys(dest)
find("//ul[@class='hotel']//li[1]").click()
# Função para inserir o número de hóspedes
def input_guests_marriottvc(num_guests):
find("//input[@id='numGuests']").clear()
find("//input[@id='numGuests']").send_keys(num_guests)
# Função para selecionar o mês desejado
def input_month_marriottvc(month):
find("//select[@id='searchbymonth']").click()
print(calendar.month_name[month].lower())
find(f"//option[@id='id_{calendar.month_name[month].lower()}2024']").click(
)
# Função para inserir o número de noites
def input_nights_marriottvc(num_nights):
find("//input[@id='numNights']").clear()
find("//input[@id='numNights']").send_keys(num_nights)
# Função para clicar no botão de pesquisa
def click_search_marriottvc():
find("//span[@class='search-carat']").click()
# Função para verificar a disponibilidade
def check_availability_marriottvc(check_in_day, num_nights, month):
input_month_marriottvc(month)
input_nights_marriottvc(num_nights)
click_search_marriottvc()
try:
find(
'//*[@id="rtWebPanel"]/div/main/div[3]/form/div/div/div[2]/div[3]/div[2]/div[1]/button'
).click()
except TimeoutException:
logging.error("Elemento não encontrado dentro do tempo limite")
while True:
try:
day = WebDriverWait(driver, 0.5).until(
EC.presence_of_element_located((
By.XPATH,
f"//button[contains(@aria-label, ' {check_in_day} ') and @class='day enabled']"
)))
print(
f"Dia {day.text} até {int(day.text) + int(num_nights)} está disponível no marriottvacationclub.com"
)
return day.text, calendar.month_name[month]
except:
check_in_day = str(int(check_in_day) + 1)
if int(check_in_day) > 31:
month += 1
check_in_day = '01'
input_month_marriottvc(month)
input_nights_marriottvc(num_nights)
click_search_marriottvc()
try:
find(
'//*[@id="rtWebPanel"]/div/main/div[3]/form/div/div/div[2]/div[3]/div[2]/div[1]/button'
).click()
except TimeoutException:
logging.error(
"Elemento não encontrado dentro do tempo limite")
# Exemplo de uso
login_marriottvc()
open_bookvacation_marriottvc()
input_destination_marriottvc('Marriott\'s Mountain Valley Lodge')
input_guests_marriottvc('2')
available_day, available_month = check_availability_marriottvc(
27, 4, 7) # Verifica a disponibilidade para 27 de Julho com 4 noites
print(f"Dia disponível: {available_day} de {available_month}")
driver.quit()