works: enrich publication topics with descriptions from topics index

This commit is contained in:
2026-07-06 23:54:38 +02:00
parent e86a19f800
commit 53f5440d01
+37
View File
@@ -71,6 +71,7 @@ async def get_author_works(
)
elements, total = _parse_full_work_search_response(data)
await _enrich_work_topic_descriptions(elements)
offset = (page - 1) * size
last_page = offset + len(elements) >= total
@@ -202,9 +203,45 @@ async def get_work(
elements, _ = _parse_full_work_search_response(response_by_title)
if not elements:
return None
await _enrich_work_topic_descriptions(elements[:1])
return elements[0]
async def _enrich_work_topic_descriptions(works: List[Work]) -> None:
"""Fill in topic descriptions missing from raw work documents.
"""
topic_ids = set()
for work in works:
for topic in work.topics or []:
if topic.id and not topic.description:
topic_ids.add(topic.id)
primary = work.primary_topic
if primary and primary.id and not primary.description:
topic_ids.add(primary.id)
if not topic_ids:
return
details = await _get_topic_details(list(topic_ids))
if not details:
return
for work in works:
for topic in work.topics or []:
info = details.get(topic.id)
if info and not topic.description:
topic.description = info.get("description")
primary = work.primary_topic
if primary and primary.id:
info = details.get(primary.id)
if info and not primary.description:
primary.description = info.get("description")
async def _get_topic_details(topic_ids: List[str]) -> dict[str, dict]:
if not topic_ids:
return {}