refactor: add is_available method to fetchers
This commit is contained in:
+3
-12
@@ -51,18 +51,9 @@ class LrcManager:
|
|||||||
return [self.fetchers[force_method]]
|
return [self.fetchers[force_method]]
|
||||||
|
|
||||||
sequence: list[BaseFetcher] = []
|
sequence: list[BaseFetcher] = []
|
||||||
if track.is_local:
|
for method in self.fetchers.keys():
|
||||||
sequence.append(self.fetchers["local"])
|
if self.fetchers[method].is_available(track):
|
||||||
if track.title:
|
sequence.append(self.fetchers[method])
|
||||||
sequence.append(self.fetchers["cache-search"])
|
|
||||||
if track.trackid:
|
|
||||||
sequence.append(self.fetchers["spotify"])
|
|
||||||
if track.is_complete:
|
|
||||||
sequence.append(self.fetchers["lrclib"])
|
|
||||||
if track.title:
|
|
||||||
sequence.append(self.fetchers["lrclib-search"])
|
|
||||||
sequence.append(self.fetchers["netease"])
|
|
||||||
sequence.append(self.fetchers["qqmusic"])
|
|
||||||
|
|
||||||
logger.debug(f"Fallback sequence: {[f.source_name for f in sequence]}")
|
logger.debug(f"Fallback sequence: {[f.source_name for f in sequence]}")
|
||||||
return sequence
|
return sequence
|
||||||
|
|||||||
@@ -22,6 +22,11 @@ class BaseFetcher(ABC):
|
|||||||
"""True if this fetcher manages its own cache (skip per-source cache check)."""
|
"""True if this fetcher manages its own cache (skip per-source cache check)."""
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def is_available(self, track: TrackMeta) -> bool:
|
||||||
|
"""Check if the fetcher is available for the given track (e.g. has required metadata)."""
|
||||||
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def fetch(
|
def fetch(
|
||||||
self, track: TrackMeta, bypass_cache: bool = False
|
self, track: TrackMeta, bypass_cache: bool = False
|
||||||
|
|||||||
@@ -30,6 +30,9 @@ class CacheSearchFetcher(BaseFetcher):
|
|||||||
def self_cached(self) -> bool:
|
def self_cached(self) -> bool:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def is_available(self, track: TrackMeta) -> bool:
|
||||||
|
return bool(track.title)
|
||||||
|
|
||||||
def fetch(
|
def fetch(
|
||||||
self, track: TrackMeta, bypass_cache: bool = False
|
self, track: TrackMeta, bypass_cache: bool = False
|
||||||
) -> Optional[LyricResult]:
|
) -> Optional[LyricResult]:
|
||||||
|
|||||||
@@ -25,6 +25,9 @@ class LocalFetcher(BaseFetcher):
|
|||||||
def source_name(self) -> str:
|
def source_name(self) -> str:
|
||||||
return "local"
|
return "local"
|
||||||
|
|
||||||
|
def is_available(self, track: TrackMeta) -> bool:
|
||||||
|
return track.is_local
|
||||||
|
|
||||||
def fetch(
|
def fetch(
|
||||||
self, track: TrackMeta, bypass_cache: bool = False
|
self, track: TrackMeta, bypass_cache: bool = False
|
||||||
) -> Optional[LyricResult]:
|
) -> Optional[LyricResult]:
|
||||||
|
|||||||
@@ -31,6 +31,9 @@ class LrclibFetcher(BaseFetcher):
|
|||||||
def source_name(self) -> str:
|
def source_name(self) -> str:
|
||||||
return "lrclib"
|
return "lrclib"
|
||||||
|
|
||||||
|
def is_available(self, track: TrackMeta) -> bool:
|
||||||
|
return track.is_complete
|
||||||
|
|
||||||
def fetch(
|
def fetch(
|
||||||
self, track: TrackMeta, bypass_cache: bool = False
|
self, track: TrackMeta, bypass_cache: bool = False
|
||||||
) -> Optional[LyricResult]:
|
) -> Optional[LyricResult]:
|
||||||
|
|||||||
@@ -33,6 +33,9 @@ class LrclibSearchFetcher(BaseFetcher):
|
|||||||
def source_name(self) -> str:
|
def source_name(self) -> str:
|
||||||
return "lrclib-search"
|
return "lrclib-search"
|
||||||
|
|
||||||
|
def is_available(self, track: TrackMeta) -> bool:
|
||||||
|
return bool(track.title)
|
||||||
|
|
||||||
def fetch(
|
def fetch(
|
||||||
self, track: TrackMeta, bypass_cache: bool = False
|
self, track: TrackMeta, bypass_cache: bool = False
|
||||||
) -> Optional[LyricResult]:
|
) -> Optional[LyricResult]:
|
||||||
|
|||||||
@@ -40,6 +40,9 @@ class NeteaseFetcher(BaseFetcher):
|
|||||||
def source_name(self) -> str:
|
def source_name(self) -> str:
|
||||||
return "netease"
|
return "netease"
|
||||||
|
|
||||||
|
def is_available(self, track: TrackMeta) -> bool:
|
||||||
|
return bool(track.title)
|
||||||
|
|
||||||
def _search(self, track: TrackMeta, limit: int = 10) -> Optional[int]:
|
def _search(self, track: TrackMeta, limit: int = 10) -> Optional[int]:
|
||||||
"""Search Netease and return the best-matching song ID.
|
"""Search Netease and return the best-matching song ID.
|
||||||
|
|
||||||
|
|||||||
@@ -32,6 +32,9 @@ class QQMusicFetcher(BaseFetcher):
|
|||||||
def source_name(self) -> str:
|
def source_name(self) -> str:
|
||||||
return "qqmusic"
|
return "qqmusic"
|
||||||
|
|
||||||
|
def is_available(self, track: TrackMeta) -> bool:
|
||||||
|
return bool(track.title) and bool(QQ_MUSIC_API_URL)
|
||||||
|
|
||||||
def _search(self, track: TrackMeta, limit: int = 10) -> Optional[str]:
|
def _search(self, track: TrackMeta, limit: int = 10) -> Optional[str]:
|
||||||
"""Search QQ Music and return the best-matching song MID."""
|
"""Search QQ Music and return the best-matching song MID."""
|
||||||
query = f"{track.artist or ''} {track.title or ''}".strip()
|
query = f"{track.artist or ''} {track.title or ''}".strip()
|
||||||
|
|||||||
@@ -55,6 +55,9 @@ class SpotifyFetcher(BaseFetcher):
|
|||||||
def source_name(self) -> str:
|
def source_name(self) -> str:
|
||||||
return "spotify"
|
return "spotify"
|
||||||
|
|
||||||
|
def is_available(self, track: TrackMeta) -> bool:
|
||||||
|
return bool(track.trackid) and bool(SPOTIFY_SP_DC)
|
||||||
|
|
||||||
# ─── Auth helpers ────────────────────────────────────────────────
|
# ─── Auth helpers ────────────────────────────────────────────────
|
||||||
|
|
||||||
def _get_server_time(self, client: httpx.Client) -> Optional[int]:
|
def _get_server_time(self, client: httpx.Client) -> Optional[int]:
|
||||||
|
|||||||
Reference in New Issue
Block a user