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)