Skip to content

Commit

Permalink
Remove empty timestamps from synced lyrics
Browse files Browse the repository at this point in the history
  • Loading branch information
snejus committed Oct 19, 2024
1 parent d1bf76a commit af40165
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
8 changes: 7 additions & 1 deletion beetsplug/lyrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ class LRCLibItem(TypedDict):
class LRCLyrics:
#: Percentage tolerance for max duration difference between lyrics and item.
DURATION_DIFF_TOLERANCE = 0.05
remove_empty_times = partial(
re.compile(r"^\[\d+:\d+.\d+\] *$", re.M).sub, ""
)

target_duration: float
duration: float
Expand Down Expand Up @@ -348,7 +351,10 @@ def get_text(self, want_synced: bool) -> str:
if self.instrumental:
return INSTRUMENTAL_LYRICS

return self.synced if want_synced and self.synced else self.plain
if want_synced and self.synced:
return self.remove_empty_times(self.synced).strip()

return self.plain


class LRCLib(Backend):
Expand Down
5 changes: 2 additions & 3 deletions test/plugins/lyrics_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,21 +278,20 @@ def backend(self) -> str:
[00:39.18] See how they run
[00:43.33] Lady Madonna, baby at your breast
[00:48.50] Wonders how you manage to feed the rest
[00:52.54]
[01:01.32] Ba-ba, ba-ba, ba-ba, ba-ba-ba
[01:05.03] Ba-ba, ba-ba, ba-ba, ba, ba-ba, ba-ba
[01:09.58] Ba-ba, ba-ba, ba-ba, ba-ba-ba
[01:14.27] See how they run
[01:19.05] Lady Madonna, lying on the bed
[01:22.99] Listen to the music playing in your head
[01:27.92]
[01:36.33] Tuesday afternoon is never ending
[01:40.47] Wednesday morning papers didn't come
[01:44.76] Thursday night your stockings needed mending
[01:49.35] See how they run
[01:53.73] Lady Madonna, children at your feet
[01:58.65] Wonder how you manage to make ends meet
[02:06.04]
""",
),
LyricsPage.make(
Expand Down
17 changes: 15 additions & 2 deletions test/plugins/test_lyrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,23 @@ def fetch_lyrics(self, backend, requests_mock, request_kwargs):

return partial(backend.fetch, "la", "la", "la", self.ITEM_DURATION)

@pytest.mark.parametrize("response_data", [[lyrics_match()]])
@pytest.mark.parametrize(
"response_data",
[
[
lyrics_match(
plainLyrics="plain",
syncedLyrics="[00:00.00] synced\n[00:01.00] ",
)
]
],
)
@pytest.mark.parametrize(
"plugin_config, expected_lyrics",
[({"synced": True}, "synced"), ({"synced": False}, "plain")],
[
({"synced": True}, "[00:00.00] synced"), # empty timestamp is gone
({"synced": False}, "plain"),
],
)
def test_synced_config_option(self, fetch_lyrics, expected_lyrics):
assert fetch_lyrics() == expected_lyrics
Expand Down

0 comments on commit af40165

Please sign in to comment.