From 5ac6920ee28f51a3a9a84cfbbe7d02d3445ff786 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Fri, 22 Mar 2024 09:31:21 +0000 Subject: [PATCH] Move code around to make functions readable --- check.py | 65 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/check.py b/check.py index 05d52b4..2e5f193 100755 --- a/check.py +++ b/check.py @@ -19,6 +19,14 @@ from urllib3.exceptions import InsecureRequestWarning # type: ignore from urllib3.util.url import parse_url # type: ignore +class LiveConference(typing.TypedDict): + """Live conference.""" + + conference: str + year: int + live: date + + class AbsoluteDNSAdapter(HTTPAdapter): """A custom adapter for requests to ensure hostnames are treated as absolute.""" @@ -139,36 +147,45 @@ def load_yaml(name: str) -> typing.Any: return yaml.safe_load(open(filename)) -def main(show_not_live: bool = False) -> None: - """Check each conference.""" - today = date.today() +def check_conference_web_site(name: str, src_url: str, year: int) -> bool: + """Check if an individual web site is live.""" + assert "{year}" in src_url + live, msg = check_conference(name, url := src_url.format(year=year)) + if live: + body = f"{name}\n{url}\nWeb page title: {msg}" + send_mail(f"Conference site live: {name}", body) + + return live + + +def find_new_conference_web_sites( + today: date, live: list[LiveConference] +) -> list[LiveConference]: + """Check for new conference web sites going live.""" this_year = today.year - conferences = load_yaml("conferences") - live_conferences = load_yaml("live") - live_set = {(c["conference"], c["year"]) for c in live_conferences} + new: list[LiveConference] = [] - new_live = False + live_set = {(c["conference"], c["year"]) for c in live} + for name, src_url in load_yaml("conferences").items(): + new += [ + {"conference": name, "year": year, "live": today} + for year in (this_year, this_year + 1) + if (name, year) not in live_set + and check_conference_web_site(name, src_url, year) + ] + return new - for name, src_url in conferences.items(): - for year in this_year, this_year + 1: - if (name, year) in live_set: - continue - assert "{year}" in src_url - url = src_url.format(year=year) - live, msg = check_conference(name, url) - if not live: - continue - body = f"{name}\n{url}\nWeb page title: {msg}" - send_mail(f"Conference site live: {name}", body) - new_live = True - live_conferences.append({"conference": name, "year": year, "live": today}) +def main(show_not_live: bool = False) -> None: + """Check fow new conference web sites.""" + live: list[LiveConference] = load_yaml("live") + if not (new := find_new_conference_web_sites(date.today(), live)): + return - if new_live: - live_filename = os.path.expanduser(config["data"]["live"]) - with open(live_filename, "w") as out: - yaml.dump(live_conferences, stream=out, sort_keys=False) + live_filename = os.path.expanduser(config["data"]["live"]) + with open(live_filename, "w") as out: + yaml.dump(live + new, stream=out, sort_keys=False) if __name__ == "__main__":