Preserve raw HTML alongside stripped text when archiving documents
This commit is contained in:
+11
-2
@@ -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(" ", " ")
|
||||
|
||||
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user