From 488ecf8b71c68d8199b0caed768eb4f74f18f89d Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Mon, 23 Sep 2024 09:22:20 +0100 Subject: [PATCH] GWR ticket date mail shows weekends GWR advance ticket change email now shows Saturdays and Sundays in addition to Weekdays. Switched from ISO dates to human readable dates. Closes: #177 --- update.py | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/update.py b/update.py index bde5d6f..8465e8a 100755 --- a/update.py +++ b/update.py @@ -54,26 +54,49 @@ def update_gwr_advance_ticket_date(config: flask.config.Config) -> None: """Update GWR advance ticket date cache.""" filename = os.path.join(config["DATA_DIR"], "advance-tickets.html") existing_html = open(filename).read() - existing_date = agenda.gwr.extract_weekday_date(existing_html) + + existing_dates = agenda.gwr.extract_dates(existing_html) + assert existing_dates + assert list(existing_dates.keys()) == ["Weekdays", "Saturdays", "Sundays"] new_html = requests.get(agenda.gwr.url).text - open(filename, "w").write(new_html) - new_date = agenda.gwr.extract_weekday_date(new_html) + new_dates = agenda.gwr.extract_dates(new_html) + assert new_dates + assert list(new_dates.keys()) == ["Weekdays", "Saturdays", "Sundays"] - if existing_date == new_date: + if existing_dates == new_dates: if sys.stdin.isatty(): - print("date has't changed:", existing_date) + print(filename) + print(agenda.gwr.url) + print("dates haven't changed:", existing_dates) return - subject = f"New GWR advance ticket booking date: {new_date}" - body = f"""Old date: {existing_date} -New date: {new_date} + open(filename, "w").write(new_html) + + subject = ( + "New GWR advance ticket booking date: " + + f'{new_dates["Weekdays"].strftime("%d %b %Y")} (Weekdays)' + ) + body = f""" +{"\n".join(f'{key}: {when.strftime("%d %b %Y")}' for key, when in new_dates.items())} {agenda.gwr.url} Agenda: https://edwardbetts.com/agenda/ """ + + if sys.stdin.isatty(): + print(filename) + print(agenda.gwr.url) + print() + print("dates have changed") + print("old:", existing_dates) + print("new:", new_dates) + print() + print(subject) + print(body) + agenda.mail.send_mail(config, subject, body)