Skip to content

Commit

Permalink
support local lyric source without folder
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderr committed Aug 25, 2024
1 parent a3818c3 commit ca05a62
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: ""
```
Expand Down Expand Up @@ -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.

Expand Down
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type Config struct {
} `yaml:"browser"`

Local struct {
Enabled bool `default: "false" yaml:"enabled"`
Folder string `yaml:"folder"`
} `yaml:"local"`
}
Expand Down
16 changes: 12 additions & 4 deletions services/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ca05a62

Please sign in to comment.