-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmonitor.py
More file actions
225 lines (189 loc) · 8.4 KB
/
monitor.py
File metadata and controls
225 lines (189 loc) · 8.4 KB
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
import sys
# --- Dependency Check ---
try:
import requests
from selenium import webdriver
from dotenv import load_dotenv
from webdriver_manager.chrome import ChromeDriverManager
except ImportError as e:
missing_module = str(e).split("'")[1]
print(f"FATAL: Missing required Python package '{missing_module}'.", file=sys.stderr)
print("Please install all required packages by running the following command:", file=sys.stderr)
print("\n pip install -r requirements.txt\n", file=sys.stderr)
sys.exit(1)
import json
import os
import time
import logging
from datetime import datetime
from rmv_checker import (
get_rmv_data,
get_all_locations,
prompt_for_rmv_url,
prompt_for_ntfy_url,
prompt_for_locations,
prompt_for_frequency
)
from selenium.webdriver.chrome.service import Service as ChromeService
# --- Logging Setup ---
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# Create handlers
c_handler = logging.StreamHandler(sys.stdout) # Console handler
f_handler = logging.FileHandler('monitor.log') # File handler
c_handler.setLevel(logging.INFO)
f_handler.setLevel(logging.INFO)
# Create formatters and add it to handlers
c_format = logging.Formatter('%(message)s')
f_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
c_handler.setFormatter(c_format)
f_handler.setFormatter(f_format)
# Add handlers to the logger
if not logger.handlers:
logger.addHandler(c_handler)
logger.addHandler(f_handler)
STATE_FILE = 'state.json'
def load_json(file_path):
"""Loads data from a JSON file."""
if not os.path.exists(file_path):
return {}
with open(file_path, 'r') as f:
return json.load(f)
def save_json(data, file_path):
"""Saves data to a JSON file."""
with open(file_path, 'w') as f:
json.dump(data, f, indent=2)
def send_ntfy_notification(url, message):
"""Sends a notification to a ntfy URL."""
try:
requests.post(url, data=message.encode('utf-8'))
logger.info(f"Sent notification: {message}")
except Exception as e:
logger.error(f"Error sending ntfy notification: {e}")
def parse_date(date_str):
"""Parses the scraped date string into a datetime object."""
if "No Appointments" in date_str or "No Date Found" in date_str:
return None
try:
clean_date_str = date_str.strip().rstrip(',')
return datetime.strptime(clean_date_str, '%a %b %d, %Y, %I:%M %p')
except ValueError:
try:
return datetime.strptime(clean_date_str, '%a %b %d, %Y')
except ValueError as e:
logger.error(f"Error parsing date string '{date_str}': {e}")
return None
def check_for_appointments(rmv_url, ntfy_url, locations_to_monitor, state):
"""The core logic for checking appointments and sending notifications."""
logger.info(f"--- Running RMV Appointment Check ---")
live_data = get_rmv_data(rmv_url, locations_to_monitor)
if not live_data:
logger.warning("Could not fetch live appointment data.")
return state
for location_data in live_data:
location_id = str(location_data['id'])
location_name = location_data['service_center']
new_date_str = location_data['earliest_date']
new_date = parse_date(new_date_str)
if not new_date:
logger.info(f"No appointments found for {location_name}.")
continue
last_known_date_str = state.get(location_id)
last_known_date = parse_date(last_known_date_str) if last_known_date_str else None
if not last_known_date or new_date < last_known_date:
# Check if the original scraped string contained a time component (AM/PM)
if "AM" in new_date_str or "PM" in new_date_str:
# We have a specific time
message = f"New appointment at {location_name}: {new_date.strftime('%a, %b %d, %Y at %I:%M %p')}"
state[location_id] = new_date.strftime('%a %b %d, %Y, %I:%M %p')
else:
# We only have a date
message = f"New earliest date at {location_name}: {new_date.strftime('%a, %b %d, %Y')}"
state[location_id] = new_date.strftime('%a %b %d, %Y')
send_ntfy_notification(ntfy_url, message)
else:
logger.info(f"No change for {location_name}. Earliest is still {last_known_date_str}")
save_json(state, STATE_FILE)
logger.info("--- Check complete ---")
return state
def run_monitor():
"""Main monitoring loop."""
load_dotenv()
# --- Configuration Healing & Data Fetching ---
is_interactive = sys.stdout.isatty()
all_locations_data = None # Initialize
rmv_url = os.getenv("RMV_URL")
if not rmv_url:
if not is_interactive:
logger.error("FATAL: RMV_URL not found. Please run the script interactively once to set it up.")
sys.exit(1)
logger.warning("RMV_URL not found in .env file.")
rmv_url = prompt_for_rmv_url()
load_dotenv(override=True)
ntfy_url = os.getenv("NTFY_URL")
if not ntfy_url:
if not is_interactive:
logger.error("FATAL: NTFY_URL not found. Please run the script interactively once to set it up.")
sys.exit(1)
logger.warning("NTFY_URL not found in .env file.")
ntfy_url = prompt_for_ntfy_url()
load_dotenv(override=True)
locations_to_monitor_ids_str = os.getenv("LOCATIONS_TO_MONITOR")
if not locations_to_monitor_ids_str:
if not is_interactive:
logger.error("FATAL: LOCATIONS_TO_MONITOR not found. Please run the script interactively once to set it up.")
sys.exit(1)
logger.warning("LOCATIONS_TO_MONITOR not found in .env file.")
# This prompt now returns the fetched location data, so we don't have to fetch it again.
locations_to_monitor_ids_str, all_locations_data = prompt_for_locations(rmv_url)
load_dotenv(override=True)
frequency_minutes_str = os.getenv("CHECK_FREQUENCY_MINUTES")
if not frequency_minutes_str:
if not is_interactive:
logger.error("FATAL: CHECK_FREQUENCY_MINUTES not found. Please run the script interactively once to set it up.")
sys.exit(1)
logger.warning("CHECK_FREQUENCY_MINUTES not found in .env file.")
frequency_minutes_str = str(prompt_for_frequency())
load_dotenv(override=True)
locations_to_monitor_ids = locations_to_monitor_ids_str.split(',')
frequency_minutes = int(frequency_minutes_str)
# If we haven't already fetched the location data during setup, fetch it now.
if all_locations_data is None:
logger.info("Fetching all location data for friendly names...")
options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
driver = None
try:
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=options)
all_locations_data = get_all_locations(driver, rmv_url)
finally:
if driver:
driver.quit()
if not all_locations_data:
logger.error("Could not fetch location data. Exiting.")
sys.exit(1)
location_id_to_name_map = {loc['id']: loc['service_center'] for loc in all_locations_data}
locations_to_monitor = [
{'id': loc_id, 'service_center': location_id_to_name_map.get(loc_id, f"ID-{loc_id}")}
for loc_id in locations_to_monitor_ids
]
# --- State Reset ---
if is_interactive and os.path.exists(STATE_FILE):
reset_state_choice = input("Do you want to delete the existing state.json file? [y/N]: ").lower()
if reset_state_choice == 'y':
os.remove(STATE_FILE)
logger.info("Deleted state.json")
state = load_json(STATE_FILE)
logger.info(f"Starting monitor. Will check every {frequency_minutes} minutes.")
while True:
try:
state = check_for_appointments(rmv_url, ntfy_url, locations_to_monitor, state)
except Exception as e:
logger.error("An unexpected error occurred during the check:", exc_info=True)
logger.warning("The monitor will continue running.")
logger.info(f"Sleeping for {frequency_minutes} minutes...")
time.sleep(frequency_minutes * 60)
if __name__ == "__main__":
run_monitor()