From 2e51a49caf70118a97699798e6d4abd7670154ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0ar=C5=ABnas=20Nejus?= Date: Sun, 15 Dec 2024 00:17:07 +0000 Subject: [PATCH] lyrics: sort lrclib lyrics by synced field and query search first I found that the `/get` endpoint often returns incorrect or unsynced lyrics, while results returned by the `/search` more accurate options. Thus I reversed the change in the previous commit to prioritize searching first. --- beetsplug/lyrics.py | 8 ++++---- test/plugins/test_lyrics.py | 19 +++++++++++++++++-- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/beetsplug/lyrics.py b/beetsplug/lyrics.py index 159518281a..70edaa0011 100644 --- a/beetsplug/lyrics.py +++ b/beetsplug/lyrics.py @@ -332,7 +332,7 @@ def is_valid(self) -> bool: ) @cached_property - def dist(self) -> tuple[float, bool]: + def dist(self) -> tuple[bool, float]: """Distance/score of the given lyrics item. Return a tuple with the following values: @@ -342,7 +342,7 @@ def dist(self) -> tuple[float, bool]: Best lyrics match is the one that has the closest duration to ``target_duration`` and has synced lyrics available. """ - return self.duration_dist, not self.synced + return not self.synced, self.duration_dist def get_text(self, want_synced: bool) -> str: if self.instrumental: @@ -391,11 +391,11 @@ def fetch_candidates( if album: get_params["album_name"] = album + yield self.fetch_json(self.SEARCH_URL, params=base_params) + with suppress(NotFoundError): yield [self.fetch_json(self.GET_URL, params=get_params)] - yield self.fetch_json(self.SEARCH_URL, params=base_params) - @classmethod def pick_best_match(cls, lyrics: Iterable[LRCLyrics]) -> LRCLyrics | None: """Return best matching lyrics item from the given list.""" diff --git a/test/plugins/test_lyrics.py b/test/plugins/test_lyrics.py index 0dee427ec3..47c9837701 100644 --- a/test/plugins/test_lyrics.py +++ b/test/plugins/test_lyrics.py @@ -414,8 +414,23 @@ def test_synced_config_option(self, fetch_lyrics, expected_lyrics): ), lyrics_match(syncedLyrics="synced", plainLyrics="plain 2"), ], - "plain with closer duration", - id="prefer closer duration", + "synced", + id="prefer synced lyrics even if plain duration is closer", + ), + pytest.param( + [ + lyrics_match( + duration=ITEM_DURATION, + syncedLyrics=None, + plainLyrics="valid plain", + ), + lyrics_match( + duration=1, + syncedLyrics="synced with invalid duration", + ), + ], + "valid plain", + id="ignore synced with invalid duration", ), pytest.param( [lyrics_match(syncedLyrics=None), lyrics_match()],