Skip to content

Commit

Permalink
lyrics: sort lrclib lyrics by synced field and query search first
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
snejus committed Jan 20, 2025
1 parent 33aafdd commit 2e51a49
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
8 changes: 4 additions & 4 deletions beetsplug/lyrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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."""
Expand Down
19 changes: 17 additions & 2 deletions test/plugins/test_lyrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()],
Expand Down

0 comments on commit 2e51a49

Please sign in to comment.