Working TOC

This commit is contained in:
Adrian Rumpold
2025-04-23 12:11:53 +02:00
parent 54a3aba531
commit 6dcf39dc58
6 changed files with 195 additions and 14 deletions

View File

@@ -63,6 +63,49 @@ def article_ids(celex_id: str, language: Language = Language.ENG):
return article_ids
@app.get("/{celex_id}/toc/{language}")
def toc(celex_id: str, language: Language = Language.ENG):
"""
Fetch the table of contents from the server.
"""
fmx4_data = _get_fmx4_data(celex_id, language)
xml = ET.fromstring(fmx4_data.encode("utf-8"))
toc = []
for division in xml.xpath("//DIVISION"):
print(division)
title = ti_el[0] if (ti_el := division.xpath("TITLE/TI//text()")) else ""
subtitle = sti_el[0] if (sti_el := division.xpath("TITLE/STI//text()")) else ""
articles = []
for article in division.xpath("ARTICLE") or []:
art_id = article.get("IDENTIFIER")
if not art_id:
continue
art_title = ti_el[0] if (ti_el := article.xpath("TI.ART//text()")) else ""
art_subtitle = (
sti_el[0] if (sti_el := article.xpath("STI.ART//text()")) else ""
)
articles.append(
{
"id": int(art_id.lstrip("0")),
"type": "article",
"title": art_title,
"subtitle": art_subtitle,
}
)
toc.append(
{
"title": title,
"type": "division",
"subtitle": subtitle,
"articles": articles,
}
)
return toc
@app.get("/{celex_id}/articles/{article_id}/{language}")
def article(celex_id: str, article_id: int, language: Language = Language.ENG):
"""