| 682 | |
| 683 | @staticmethod |
| 684 | def _parse_table(html: str) -> list[dict]: |
| 685 | import re |
| 686 | results = [] |
| 687 | try: |
| 688 | from bs4 import BeautifulSoup |
| 689 | soup = BeautifulSoup(html, "html.parser") |
| 690 | table = soup.find("table") |
| 691 | if not table: |
| 692 | return results |
| 693 | for tr in table.find_all("tr")[1:]: |
| 694 | cells = tr.find_all("td") |
| 695 | if len(cells) < 4: |
| 696 | continue |
| 697 | date_text = cells[0].get_text(strip=True) |
| 698 | comm_type = cells[1].get_text(strip=True) |
| 699 | prod_type = cells[2].get_text(strip=True) |
| 700 | topic = cells[3].get_text(strip=True) |
| 701 | if "device" not in prod_type.lower(): |
| 702 | continue |
| 703 | a_tag = cells[3].find("a") |
| 704 | link = "" |
| 705 | if a_tag and a_tag.get("href"): |
| 706 | href = a_tag["href"] |
| 707 | if not href.startswith("http"): |
| 708 | link = "https://medsafe.govt.nz/safety/" + href |
| 709 | else: |
| 710 | link = href |
| 711 | dm = re.search(r"(\d{1,2})\s+(January|February|March|April|May|June|July|August|September|October|November|December)\s+(\d{4})", date_text) |
| 712 | pub_date = "" |
| 713 | if dm: |
| 714 | month_map = {"January": "01", "February": "02", "March": "03", "April": "04", |
| 715 | "May": "05", "June": "06", "July": "07", "August": "08", |
| 716 | "September": "09", "October": "10", "November": "11", "December": "12"} |
| 717 | pub_date = f"{dm.group(3)}-{month_map.get(dm.group(2), '01')}-{dm.group(1).zfill(2)}" |
| 718 | results.append({ |
| 719 | "title": f"[{comm_type}] {topic}", |
| 720 | "link": link, |
| 721 | "pub_date": pub_date, |
| 722 | "description": f"Medsafe {comm_type} - Product: {prod_type}", |
| 723 | }) |
| 724 | except ImportError: |
| 725 | pass |
| 726 | return results |
| 727 | |
| 728 | |
| 729 | # --------------------------------------------------------------------------- |