Skip to content

Commit cec1270

Browse files
committed
store any uri instead of only local ones
1 parent ca05a62 commit cec1270

File tree

5 files changed

+47
-26
lines changed

5 files changed

+47
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ local:
235235

236236
Display lyrics from local `.lrc` files.
237237

238-
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`.
238+
By default, the application will look for a file that is a sibling of a local music file (e.g. local player via mpris), i.e. with the same path, with the extension replaced by `.lrc`.
239239

240240
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.
241241

player/player.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package player
22

3+
import "net/url"
4+
35
type Player interface {
46
State() (*State, error)
57
}
68

79
type TrackMetadata struct {
810
// ID of the current track.
911
ID string
10-
// URI is the path to the local music file, if it exists.
11-
// May be either absolute or relative to the local music directory (configured in "local" source).
12-
Uri string
12+
// URI to music file, if it is known. May be a (local) relative path.
13+
Uri *url.URL
1314
// Query is a string that can be used to find lyrics.
1415
Query string
1516
}

services/local/local.go

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"io/fs"
8+
"net/url"
89
"os"
910
"path/filepath"
1011
"sptlrx/lyrics"
@@ -67,26 +68,12 @@ func (c *Client) findFile(track *player.TrackMetadata) string {
6768
}
6869

6970
// If it is a local track, try for similarly named .lrc file first
70-
if track.Uri != "" {
71-
var absUri string
72-
if filepath.IsAbs(track.Uri) {
73-
// Uri is already absolute
74-
absUri = track.Uri
75-
} else if c.folder != "" {
76-
// Uri is relative to local music directory
77-
absUri = filepath.Join(c.folder, track.Uri)
78-
} else {
79-
// Can not handle relative uri without folder configured
80-
absUri = ""
81-
}
82-
if absUri != "" {
83-
absLyricsUri := strings.TrimSuffix(absUri, filepath.Ext(absUri)) + ".lrc"
84-
if _, err := os.Stat(absLyricsUri); err == nil {
85-
return absLyricsUri
86-
}
87-
}
71+
var exactMatch string = c.fileByLocalUri(track.Uri)
72+
if exactMatch != "" {
73+
return exactMatch
8874
}
8975

76+
// Fall back to best-effort search
9077
parts := splitString(track.Query)
9178

9279
var best *file
@@ -115,6 +102,32 @@ func (c *Client) findFile(track *player.TrackMetadata) string {
115102
return best.Path
116103
}
117104

105+
func (c *Client) fileByLocalUri(uri *url.URL) string {
106+
if uri == nil {
107+
return ""
108+
}
109+
if uri.Scheme != "file" && uri.Scheme != "" {
110+
return ""
111+
}
112+
var absUri string
113+
if filepath.IsAbs(uri.Path) {
114+
// uri is already absolute
115+
absUri = uri.Path
116+
} else if c.folder != "" {
117+
// Uri is relative to local music directory
118+
absUri = filepath.Join(c.folder, uri.Path)
119+
} else {
120+
// Can not handle relative uri without folder configured
121+
return ""
122+
}
123+
absLyricsUri := strings.TrimSuffix(absUri, filepath.Ext(absUri)) + ".lrc"
124+
_, err := os.Stat(absLyricsUri)
125+
if err != nil {
126+
return ""
127+
}
128+
return absLyricsUri
129+
}
130+
118131
func createIndex(folder string) ([]*file, error) {
119132
index := []*file{}
120133
if folder == "" {

services/mpd/mpd.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package mpd
22

33
import (
4+
"net/url"
45
"sptlrx/player"
56
"strconv"
67

@@ -73,10 +74,16 @@ func (c *Client) State() (*player.State, error) {
7374
query = title
7475
}
7576

77+
var uri *url.URL
78+
u, err := url.Parse(current["file"])
79+
if err == nil && u.Path != "" {
80+
uri = u
81+
}
82+
7683
return &player.State{
7784
Track: player.TrackMetadata{
7885
ID: status["songid"],
79-
Uri: current["file"],
86+
Uri: uri,
8087
Query: query,
8188
},
8289
Playing: status["state"] == "play",

services/mpris/mpris_unix.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ func (c *Client) State() (*player.State, error) {
8181
title = t
8282
}
8383

84-
var uri string
84+
var uri *url.URL
8585
// In case the player uses the file name with extension as title
8686
if u, ok := meta["xesam:url"].Value().(string); ok {
8787
u, err := url.Parse(u)
88-
if err == nil {
88+
if err == nil && u.Path != "" {
8989
ext := filepath.Ext(u.Path)
90-
uri = u.Path
90+
uri = u
9191
// some players use filename as title when tag is absent => trim extension from title
9292
title = strings.TrimSuffix(title, ext)
9393
}

0 commit comments

Comments
 (0)