Compare commits

..

No commits in common. "7f7cbfe65ef83ceec605c05fed3be56cdd56457c" and "a3f00de87ecccfb087640ea2b2db47e3f2f536de" have entirely different histories.

2 changed files with 10 additions and 30 deletions

1
.gitignore vendored
View file

@ -1 +0,0 @@
.mypy_cache

View file

@ -2,9 +2,7 @@
import configparser
import json
import logging
import smtplib
import sys
from datetime import date
from email.mime.text import MIMEText
from pathlib import Path
@ -35,22 +33,6 @@ DATA_DIR = Path.home() / "lib" / "data"
DATA_FILE = DATA_DIR / "ferrocarril_dates.json"
URL = "https://ferrocarrilcentral.com.pe/appfcca/"
# Configure logging
logger = logging.getLogger("FerrocarrilMonitor")
logger.setLevel(logging.INFO)
# Handler for stdout
handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter("%(levelname)s: %(message)s")
handler.setFormatter(formatter)
# Only log INFO when in TTY, always log ERROR
if sys.stdout.isatty():
logger.addHandler(handler)
else:
handler.setLevel(logging.ERROR)
logger.addHandler(handler)
def parse_dates(html_content: str) -> list[date]:
doc = lxml.html.fromstring(html_content)
@ -75,7 +57,7 @@ def parse_dates(html_content: str) -> list[date]:
dates.append(date(year, month, day))
except (IndexError, ValueError) as e:
logger.error(f"Error parsing date: {e}")
print(f"Error parsing date: {e}")
continue
return dates
@ -105,7 +87,7 @@ def load_config() -> configparser.ConfigParser:
config = configparser.ConfigParser()
config.read(CONFIG_FILE)
if "mail" not in config:
raise ValueError(f"Config file {CONFIG_FILE} must have an [mail] section")
raise ValueError(f"Config file {CONFIG_FILE} must have an [email] section")
return config
@ -123,10 +105,9 @@ def send_email(new_dates: list[date], config: configparser.ConfigParser) -> None
try:
with smtplib.SMTP(email_config["smtp_host"]) as server:
server.send_message(msg)
logger.info("Email sent successfully")
print("Email sent successfully")
except Exception as e:
logger.error(f"Failed to send email: {e}")
raise # Re-raise to prevent saving dates if email fails
print(f"Failed to send email: {e}")
def main() -> None:
@ -140,13 +121,13 @@ def main() -> None:
response.raise_for_status()
html_content = response.text
except requests.RequestException as e:
logger.error(f"Failed to fetch webpage: {e}")
print(f"Failed to fetch webpage: {e}")
return
# Parse current dates
current_dates = set(parse_dates(html_content))
if not current_dates:
logger.info("No dates found on webpage")
print("No dates found on webpage")
return
# Load previous dates
@ -156,7 +137,7 @@ def main() -> None:
new_dates = [d for d in current_dates if d.isoformat() not in previous_dates]
if new_dates:
logger.info(f"New dates found: {new_dates}")
print(f"New dates found: {new_dates}")
try:
config = load_config()
send_email(new_dates, config)
@ -164,11 +145,11 @@ def main() -> None:
list(current_dates)
) # Update stored dates only if email succeeds
except FileNotFoundError as e:
logger.error(str(e))
print(e)
except Exception as e:
logger.error(f"Error in processing: {e}")
print(f"Error in processing: {e}")
else:
logger.info("No new dates found")
print("No new dates found")
if __name__ == "__main__":