Move code around to make functions readable
This commit is contained in:
parent
b3dc4ebf8e
commit
5ac6920ee2
65
check.py
65
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__":
|
||||
|
|
Loading…
Reference in a new issue