mirror of
https://github.com/denizsafak/abogen.git
synced 2026-09-20 11:40:57 +02:00
- Add expand_metadata_aliases() for concept fan-out (series→5, author→2, description→2, tags→3 keys) - Replace _normalize_metadata_tags in webui/service.py with normalize_metadata_map from domain - Replace _normalize_metadata in epub3/exporter.py with normalize_metadata_map from domain - Replace manual fan-out in form.py with expand_metadata_aliases() - Rewrite metadata_overrides.py to use expand_metadata_aliases() - Remove unused normalize_metadata_casefold import from exporters.py - 19 new tests for expand_metadata_aliases (1528 total)
57 lines
1.9 KiB
Python
57 lines
1.9 KiB
Python
"""OPDS metadata normalization.
|
|
|
|
Normalizes metadata keys from various OPDS/Calibre sources into
|
|
a canonical set of overrides for the audiobook conversion pipeline.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any, Dict, Mapping
|
|
|
|
from abogen.domain.metadata_helpers import expand_metadata_aliases
|
|
|
|
|
|
def normalize_opds_metadata(metadata_payload: Mapping[str, Any]) -> Dict[str, Any]:
|
|
"""Normalize OPDS/Calibre metadata into canonical override keys.
|
|
|
|
Takes a metadata payload with various key aliases (e.g. 'series'/'series_name',
|
|
'tags'/'keywords', 'authors'/'creator') and returns a dict with all
|
|
concept aliases expanded.
|
|
|
|
Args:
|
|
metadata_payload: Raw metadata dict from OPDS/Calibre import.
|
|
|
|
Returns:
|
|
Dict with all canonical metadata key aliases expanded.
|
|
"""
|
|
def _stringify(value: Any) -> str:
|
|
if value is None:
|
|
return ""
|
|
if isinstance(value, (list, tuple, set)):
|
|
parts = [str(item).strip() for item in value if item is not None]
|
|
return ", ".join(part for part in parts if part)
|
|
return str(value).strip()
|
|
|
|
# Map OPDS-specific keys to common concept keys before expansion
|
|
normalized_input: Dict[str, Any] = {}
|
|
for key, value in metadata_payload.items():
|
|
if value is None:
|
|
continue
|
|
key_lower = str(key).strip().lower()
|
|
if not key_lower:
|
|
continue
|
|
text = _stringify(value)
|
|
if not text:
|
|
continue
|
|
|
|
# Map OPDS-specific author aliases
|
|
if key_lower in ("creator", "dc_creator"):
|
|
normalized_input["author"] = text
|
|
# Map OPDS-specific subtitle aliases
|
|
elif key_lower in ("sub_title", "calibre_subtitle"):
|
|
normalized_input["subtitle"] = text
|
|
else:
|
|
normalized_input[key_lower] = text
|
|
|
|
return expand_metadata_aliases(normalized_input)
|