Move code around to make functions readable

This commit is contained in:
Edward Betts 2024-03-22 09:31:21 +00:00
parent b3dc4ebf8e
commit 5ac6920ee2

View file

@ -19,6 +19,14 @@ from urllib3.exceptions import InsecureRequestWarning # type: ignore
from urllib3.util.url import parse_url # 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): class AbsoluteDNSAdapter(HTTPAdapter):
"""A custom adapter for requests to ensure hostnames are treated as absolute.""" """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)) return yaml.safe_load(open(filename))
def main(show_not_live: bool = False) -> None: def check_conference_web_site(name: str, src_url: str, year: int) -> bool:
"""Check each conference.""" """Check if an individual web site is live."""
today = date.today()
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_live = False
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 assert "{year}" in src_url
url = src_url.format(year=year) live, msg = check_conference(name, url := src_url.format(year=year))
live, msg = check_conference(name, url) if live:
if not live:
continue
body = f"{name}\n{url}\nWeb page title: {msg}" body = f"{name}\n{url}\nWeb page title: {msg}"
send_mail(f"Conference site live: {name}", body) send_mail(f"Conference site live: {name}", body)
new_live = True return live
live_conferences.append({"conference": name, "year": year, "live": today})
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
new: list[LiveConference] = []
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
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"]) live_filename = os.path.expanduser(config["data"]["live"])
with open(live_filename, "w") as out: with open(live_filename, "w") as out:
yaml.dump(live_conferences, stream=out, sort_keys=False) yaml.dump(live + new, stream=out, sort_keys=False)
if __name__ == "__main__": if __name__ == "__main__":