From ca05a62c0b06089be1775466661aa4ddabf2ac14 Mon Sep 17 00:00:00 2001 From: Sander Van Balen Date: Sun, 25 Aug 2024 13:37:55 +0200 Subject: [PATCH] support local lyric source without folder --- README.md | 10 +++++++++- cmd/root.go | 3 ++- config/config.go | 1 + services/local/local.go | 16 ++++++++++++---- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 2b5fd3c..c98596d 100644 --- a/README.md +++ b/README.md @@ -150,6 +150,9 @@ browser: ### Local lyrics source ### local: + # Enable the local lyrics source. + # For backwards compatibility reasons setting the folder also enables this source. + enabled: false # Folder for scanning .lrc files. Example: "~/Music". folder: "" ``` @@ -226,10 +229,15 @@ You need to install a [browser extension](https://wnp.keifufu.dev/extension/gett ```yaml # config.yaml local: + enabled: true folder: "" ``` -If you want to use your local collection of `.lrc` files to display lyrics, specify the folder to scan. The application will use files with the most similar name. When used in conjunction with a local player, it will first look for a file with the same path as the music file, with the extension replaced by `.lrc`. +Display lyrics from local `.lrc` files. + +By default, the application will look for a file that is a sibling of a local music file (e.g. local player via mpdris), i.e. with the same path, with the extension replaced by `.lrc`. + +If the `folder` config option is set, it will additionally search for files within that folder. If the player provides a relative path to the music file (e.g. mpd), an exact match is attempted first as described above. If that fails, a best-effort search will be performed, returning a `.lrc` file in the folder (can be nested) with the most similar name. All other lyrics sources will be disabled. diff --git a/cmd/root.go b/cmd/root.go index b05a43a..7dc342a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -141,7 +141,8 @@ func loadPlayer(conf *config.Config) (player.Player, error) { } func loadProvider(conf *config.Config, player player.Player) (lyrics.Provider, error) { - if conf.Local.Folder != "" { + // For backwards compatibility reasons, this is auto-enabled when Folder is set + if conf.Local.Enabled || conf.Local.Folder != "" { return local.New(conf.Local.Folder) } if conf.Cookie == "" { diff --git a/config/config.go b/config/config.go index e67cebd..0925588 100644 --- a/config/config.go +++ b/config/config.go @@ -69,6 +69,7 @@ type Config struct { } `yaml:"browser"` Local struct { + Enabled bool `default: "false" yaml:"enabled"` Folder string `yaml:"folder"` } `yaml:"local"` } diff --git a/services/local/local.go b/services/local/local.go index aaac93b..0cfdf7c 100644 --- a/services/local/local.go +++ b/services/local/local.go @@ -72,13 +72,18 @@ func (c *Client) findFile(track *player.TrackMetadata) string { if filepath.IsAbs(track.Uri) { // Uri is already absolute absUri = track.Uri - } else { + } else if c.folder != "" { // Uri is relative to local music directory absUri = filepath.Join(c.folder, track.Uri) + } else { + // Can not handle relative uri without folder configured + absUri = "" } - absLyricsUri := strings.TrimSuffix(absUri, filepath.Ext(absUri)) + ".lrc" - if _, err := os.Stat(absLyricsUri); err == nil { - return absLyricsUri + if absUri != "" { + absLyricsUri := strings.TrimSuffix(absUri, filepath.Ext(absUri)) + ".lrc" + if _, err := os.Stat(absLyricsUri); err == nil { + return absLyricsUri + } } } @@ -112,6 +117,9 @@ func (c *Client) findFile(track *player.TrackMetadata) string { func createIndex(folder string) ([]*file, error) { index := []*file{} + if folder == "" { + return index, nil + } return index, filepath.WalkDir(folder, func(path string, d fs.DirEntry, err error) error { if d == nil { return fmt.Errorf("invalid path: %s", path)