-
Notifications
You must be signed in to change notification settings - Fork 22
/
reschedule_appointment.py
66 lines (52 loc) · 2.16 KB
/
reschedule_appointment.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
import sys
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium_scraper import log_in
from telegram import send_message, send_photo
url = 'https://ais.usvisa-info.com/en-pe/niv/schedule/32404946/appointment'
# Setting Chrome options to run the scraper headless.
chrome_options = Options()
# chrome_options.add_argument("--disable-extensions")
# chrome_options.add_argument("--disable-gpu")
# chrome_options.add_argument("--no-sandbox") # linux only
chrome_options.add_argument("--headless")
# Initialize the chromediver (must be installed and in PATH)
# Needed to implement the headless option
# driver = webdriver.Chrome(options=chrome_options)
driver = webdriver.Chrome()
def check_for_appointments():
'''Checks for changes in the site. Returns True if a change was found.'''
# First website
driver.get(url)
# Logging in
log_in()
print('Checking for changes.')
no_appointment_text = 'There are no available appointments at this time.'
main_page = driver.find_element_by_id('main')
print(main_page.text)
# return no_appointment_text not in main_page.text
def repeat_check(seconds_between_checks):
'''A function that calls the checking function in a given interval of time'''
while True:
current_time = time.strftime('%a, %d %b %Y %H:%M:%S', time.localtime())
print(f'Starting a new check at {current_time}.')
if check_for_appointments():
print('A change was found. Notifying it.')
send_message('A change was found. Here is an screenshot.')
# Implement screenshot.
# Missing get the driver object after checking...
# send_photo()
exit()
else:
#
for seconds_remaining in range(int(seconds_between_checks), 0, -1):
sys.stdout.write('\r')
sys.stdout.write(
f'No change was found. Checking again in {seconds_remaining} seconds.'
)
sys.stdout.flush()
time.sleep(1)
print('\n')
if __name__ == "__main__":
repeat_check(seconds_between_checks=5 * 60)