Created
January 5, 2023 12:50
-
-
Save achetverikov/c83de1eee677c1f74f777d5a625b310b to your computer and use it in GitHub Desktop.
UDI checker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from apscheduler.schedulers.blocking import BlockingScheduler | |
from datetime import datetime | |
import json | |
import pickle | |
from selenium import webdriver | |
from selenium.webdriver.common.keys import Keys | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.support.ui import WebDriverWait | |
from selenium.webdriver.support import expected_conditions as EC | |
from selenium.webdriver.common.action_chains import ActionChains | |
import requests | |
## fill in the data here; if you don't have a telegram bot, see here for an intro https://www.shellhacks.com/python-send-message-to-telegram/ | |
udi_email = '' | |
udi_pass = '' | |
tg_bot_token = '' | |
tg_chat_id = '' | |
check_interval_minutes = 30 | |
def send_to_telegram(message): | |
apiURL = f'https://api.telegram.org/bot{tg_bot_token}/sendMessage' | |
try: | |
response = requests.post(apiURL, json={'chat_id': tg_chat_id, 'text': message}) | |
print(response.text) | |
except Exception as e: | |
print(e) | |
def check_for_updates(): | |
driver = webdriver.Firefox() | |
driver.get("https://www.udi.no/en/system/login_booking/") | |
elements = driver.find_elements(By.CSS_SELECTOR, "div:nth-child(1) > .large-6 p") | |
if elements: | |
elements[0].click() | |
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, 'intro'))) | |
if driver.find_elements(By.XPATH, '//input[@id="password"]'): | |
driver.find_element(By.ID,'logonIdentifier').send_keys(udi_email) | |
driver.find_element(By.ID, 'password').send_keys(udi_pass) | |
driver.find_element(By.ID, 'next').click() | |
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//td[contains(@data-label,"Navn:")]'))) | |
all_applications = driver.find_elements(By.XPATH, '//td[contains(@data-label,"Navn:")]') | |
driver.execute_script("arguments[0].scrollIntoView(true);", all_applications[len(all_applications)-1]) | |
all_buttons = all_applications[len(all_applications) - 1].find_elements(By.XPATH, './../td/a') | |
driver.execute_script("arguments[0].click();", all_buttons[len(all_buttons)-1]) | |
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.jss391:nth-child(1) > .MuiButton-label'))).click() | |
all_open_dates = [] | |
for i in range(3): | |
cur_month = driver.find_element(By.XPATH, '//div[contains(@class,"month")]/h2') | |
half_open_dates = driver.find_elements(By.XPATH, '//td[contains(@class, "bookingCalendarHalfBookedDay") or contains(@class, "bookingCalendarBookedDay")]/span[@class="dayNumber"]') | |
if half_open_dates: | |
for d in half_open_dates: | |
date_string = 'Open date: %s, %s' % (cur_month.text, d.text) | |
date_dt = datetime.strptime('%s, %s' % (cur_month.text, d.text), '%B %Y, %d') | |
all_open_dates.append([date_string, date_dt]) | |
print('Open date: %s, %s' % (cur_month.text, d.text)) | |
driver.find_element(By.ID, 'ctl00_BodyRegion_PageRegion_MainRegion_appointmentReservation_appointmentCalendar_btnNext').click() | |
print(all_open_dates) | |
prev_dates = pickle.load( open( "dates.pkl", "rb" )) | |
min_prev_date = min([x[1] for x in prev_dates]) | |
min_curr_date = min([x[1] for x in all_open_dates]) | |
if min_prev_date!=min_curr_date: | |
send_to_telegram(all_open_dates[0][0]) | |
with open('dates.pkl', 'wb') as f: | |
pickle.dump(all_open_dates, f) | |
driver.close() | |
check_for_updates() | |
scheduler = BlockingScheduler() | |
scheduler.add_job(check_for_updates, 'interval', minutes = check_interval_minutes) | |
scheduler.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment