def la_check(url: str, online_soup: bs, local_soup: bs): """ compares online_soup to local_soup specifically for LinAl website """ # LECTURES sel_path_lec = "table:nth-of-type(1) > tr:not(:first-child)" def decode_lec(tr): """ Returns a dict with the changes or None if theres's no matching lecture updates. """ tds = tr.select("td") if len(tds) < 3: return a = tds[2].select_one("a") return { "date": tds[0].text.strip(), "name": f"Kapitel {tds[1].text.strip()}", "links": [{ "text": "notes", "url": urljoin(url, a.attrs["href"]) }] if a else [] } events = check_wrapper(online_soup, local_soup, sel_path_lec, decode_lec, "lecture") # LECTURES sel_path_ex = "table:nth-of-type(2) > tr:not(:first-child)" def decode_ex(tr): tds = tr.select("td") links = [{ "text": a.text.strip(), "url": urljoin(url, a.attrs["href"]) } for a in tr.select("a")] return { "name": tds[1].text.strip(), "date": tds[0].text.strip(), "abgabe_date": tds[4].text.strip(), "links": links } events += check_wrapper(online_soup, local_soup, sel_path_ex, decode_ex, "exercise") return events if events else [other_event()]
def dm_check(url: str, online_soup: bs, local_soup: bs): """ compares online_soup to local_soup specifically for DiscMath website """ # LECTURES sel_path_lec = "#lecture > table > tbody > tr" def decode_lec(tr): tds = tr.select("td") a = tr.select_one("a") link = { "text": "Video: " + a.text.strip(), "url": urljoin(url, a.attrs["href"]) } if a else None return { "date": tds[0].text.strip(), "name": tds[1].text.strip(), "links": [link] if link else [] } events = check_wrapper(online_soup, local_soup, sel_path_lec, decode_lec, "lecture") # EXERCISES sel_path_ex = "#exercise > table.headercol > tbody > tr" def decode_ex(tr): tds = tr.select("td") links = [] for a in tds[3].select("a") + tds[4].select("a"): links.append({ "text": a.text.strip(), "url": urljoin(url, a.attrs["href"]) }) return { "date": tds[1].text.strip(), "abgabe_date": tds[2].text.strip(), "name": tds[3].text.strip(), "links": links } events += check_wrapper(online_soup, local_soup, sel_path_ex, decode_ex, "exercise") return events if events else [other_event()]
def ad_check(url: str, online_soup: bs, local_soup: bs): """ compares online_soup to local_soup specifically for A&D website """ sel_path_lec = "#vorlesung > tr" def decode_lec(tr): tds = tr.select("td") name = tds[1].select_one("em").text.strip().strip(".") + ": " name += ", ".join([li.text for li in tds[1].select("li")]) links = [{ "url": urljoin(url, a.attrs["href"]), "text": a.text.strip() } for a in tds[2].select("a")] return {"date": tds[0].text.strip(), "name": name, "links": links} events = check_wrapper(online_soup, local_soup, sel_path_lec, decode_lec, "lecture") # EXERCISES sel_path_ex = "#uebungen > tr" def decode_ex(tr): tds = tr.select("td") links = [] for a in tr.select("a"): links.append({ "text": a.text.strip(), "url": urljoin(url, a.attrs["href"]) }) return { "date": "unknown", "abgabe_date": "unknown", "name": tds[0].text.strip(), "links": links } events += check_wrapper(online_soup, local_soup, sel_path_ex, decode_ex, "exercise") return events if events else [other_event()]
def ep_check(url: str, online_soup: bs, local_soup: bs) -> bool: """ compares online_soup to local_soup specifically for EProg website """ # EXERCISES def decode_ex(tr) -> dict: tds = tr.select("td") links = [] for a in tds[0].select("a") + tds[3].select("a"): # urls are absolute links.append({ "text": a.text.strip(), "url": urljoin(url, a.attrs["href"]) }) return { "name": tds[0].text.strip(), "date": tds[1].text.strip(), "abgabe_date": tds[2].text.strip(), "links": links } events = check_wrapper( online_soup.select("table")[1], local_soup.select("table")[1], "tbody > tr", decode_ex, "exercise") # LECTURES # can't use check_wrapper() here, as one # single entry is not in one html element. # aka; two lectures per table row html_now = online_soup.select("table")[0].select("tbody > tr") html_old = local_soup.select("table")[0].select("tbody > tr") def decode_lecs(trs): all_lecs = [] for tr in trs: tds = tr.select("td") for part in [tds[:2], tds[2:]]: # to parts per tr links = [ { "url": urljoin( url, a.attrs["href"] ), # don't join with url, as already absolute url "text": a.text.strip() } for a in part[1].select("a") ] all_lecs.append({ "date": part[0].text.strip(), "name": part[1].text.strip(), "links": links }) return all_lecs lecs_now, lecs_old = decode_lecs(html_now), decode_lecs(html_old) events += find_edit_events(lecs_now, lecs_old, "lecture") # shouldn't have any new_events in eprog but doesn't harm to put it in events += [new_event(lec, "lecture") for lec in lecs_now[len(lecs_old):]] return events if events else [other_event()]