Fix hedge fund 13F HTML wrapper, field names; fix cache builder rate limiting

This commit is contained in:
Nolan Kovacs 2026-03-26 14:53:14 -04:00
parent c115eee74a
commit 0cad4df12d
1 changed files with 12 additions and 3 deletions

View File

@ -2465,6 +2465,14 @@ def _parse_13f_xml(xml_bytes: bytes) -> list[dict]:
company, class_, cusip, value (USD), shares, shr_type, put_call
"""
import xml.etree.ElementTree as ET
import re as _re
# SEC serves 13F XML wrapped in an HTML document (Content-Type: text/html).
# Strip the HTML wrapper and extract just the <informationTable> block.
if xml_bytes[:500].upper().find(b'<!DOCTYPE HTML') != -1:
m = _re.search(rb'(<informationTable[\s\S]*?</informationTable>)',
xml_bytes, _re.IGNORECASE)
if m:
xml_bytes = m.group(1)
try:
root = ET.fromstring(xml_bytes)
except ET.ParseError:
@ -2607,9 +2615,10 @@ def fetch_hedge_fund_filings(
src = hit.get("_source", {})
acc_no = src.get("adsh", "")
ciks = src.get("ciks", [])
fund_name = src.get("entity_name", "Unknown Fund")
display = src.get("display_names") or src.get("entity_name") or []
fund_name = display[0].split(" (CIK")[0].strip() if display else "Unknown Fund"
filed = (src.get("file_date") or "")[:10]
period = (src.get("period_of_report") or "")[:10]
period = (src.get("period_ending") or src.get("period_of_report") or "")[:10]
if not acc_no or not ciks:
return []