Preserve raw HTML alongside stripped text when archiving documents

This commit is contained in:
2026-08-06 16:45:07 +08:00
parent bbbd0c92f6
commit 6971411574
2 changed files with 25 additions and 10 deletions
+11 -2
View File
@@ -106,16 +106,25 @@ def get_medical_index(session: requests.Session, base_url: str, hospital_no: str
return [{"doc_id": doc_id, "date": d.strip(), "name": n.strip()} for doc_id, d, n in docs]
def get_document_text(session: requests.Session, base_url: str, doc_id: str) -> str | None:
def get_document_html(session: requests.Session, base_url: str, doc_id: str) -> str | None:
resp = session.get(f"{base_url}/Medical/MedicalRecord", params={"fileId": doc_id}, timeout=30)
resp.encoding = "utf-8"
if resp.status_code != 200:
return None
text = re.sub(r"<[^>]+>", " ", resp.text)
return resp.text
def html_to_text(html: str) -> str:
text = re.sub(r"<[^>]+>", " ", html)
text = re.sub(r"\s+", " ", text)
return text.replace("&nbsp;", " ")
def get_document_text(session: requests.Session, base_url: str, doc_id: str) -> str | None:
html = get_document_html(session, base_url, doc_id)
return html_to_text(html) if html is not None else None
def first_match(patterns: list[str], text: str, max_len: int = 500) -> str:
for pattern in patterns:
match = re.search(pattern, text, re.DOTALL)