refactor: skip a enricher if fields it can provide are already filled

This commit is contained in:
2026-04-03 17:51:59 +02:00
parent 3917d68880
commit c91d5220bd
5 changed files with 19 additions and 1 deletions
+6
View File
@@ -26,6 +26,12 @@ def enrich_track(track: TrackMeta) -> TrackMeta:
""" """
for enricher in _ENRICHERS: for enricher in _ENRICHERS:
try: try:
# Skip if all provided fields are already filled
if all(
getattr(track, field, None) is not None for field in enricher.provides
):
continue
result = enricher.enrich(track) result = enricher.enrich(track)
except Exception as e: except Exception as e:
logger.warning(f"Enricher {enricher.name} failed: {e}") logger.warning(f"Enricher {enricher.name} failed: {e}")
+4
View File
@@ -20,6 +20,10 @@ class AudioTagEnricher(BaseEnricher):
def name(self) -> str: def name(self) -> str:
return "audio-tag" return "audio-tag"
@property
def provides(self) -> set[str]:
return {"title", "artist", "album", "length"}
def enrich(self, track: TrackMeta) -> Optional[dict]: def enrich(self, track: TrackMeta) -> Optional[dict]:
if not track.is_local or not track.url: if not track.is_local or not track.url:
return None return None
+4
View File
@@ -22,6 +22,10 @@ class BaseEnricher(ABC):
@abstractmethod @abstractmethod
def name(self) -> str: ... def name(self) -> str: ...
@property
@abstractmethod
def provides(self) -> set[str]: ...
@abstractmethod @abstractmethod
def enrich(self, track: TrackMeta) -> Optional[dict]: def enrich(self, track: TrackMeta) -> Optional[dict]:
"""Return a dict of {field_name: value} for fields this enricher can fill. """Return a dict of {field_name: value} for fields this enricher can fill.
+4
View File
@@ -33,6 +33,10 @@ class FileNameEnricher(BaseEnricher):
def name(self) -> str: def name(self) -> str:
return "file-name" return "file-name"
@property
def provides(self) -> set[str]:
return {"artist", "title", "album"}
def enrich(self, track: TrackMeta) -> Optional[dict]: def enrich(self, track: TrackMeta) -> Optional[dict]:
if not track.is_local or not track.url: if not track.is_local or not track.url:
return None return None
+1 -1
View File
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project] [project]
name = "lrx-cli" name = "lrx-cli"
version = "0.3.4" version = "0.3.5"
description = "Fetch line-synced lyrics for your music player." description = "Fetch line-synced lyrics for your music player."
readme = "README.md" readme = "README.md"
requires-python = ">=3.13" requires-python = ">=3.13"