From f6a7d41261e4a37387440ce66a5eb71b8597280e Mon Sep 17 00:00:00 2001 From: jojii Date: Thu, 6 Feb 2025 15:45:09 +0100 Subject: [PATCH] Config for custom lrclib server url --- backend/app.go | 2 +- backend/config.go | 1 + backend/lrclib.go | 27 +++++++++++++++++++++++---- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/backend/app.go b/backend/app.go index 4ff7815..f17819b 100644 --- a/backend/app.go +++ b/backend/app.go @@ -146,7 +146,7 @@ func StartupApp(appName, displayAppName, appVersion, appVersionTag, latestReleas _, _ = a.ImageManager.GetCoverThumbnail(coverID) }) if a.Config.Application.EnableLrcLib { - a.LrcLibFetcher = NewLrcLibFetcher(a.cacheDir) + a.LrcLibFetcher = NewLrcLibFetcher(a.cacheDir, a.Config.Application.CustomLrcLibUrl) } a.PlaybackManager.OnPlaying(func() { diff --git a/backend/config.go b/backend/config.go index 047ebaa..cf67efe 100644 --- a/backend/config.go +++ b/backend/config.go @@ -47,6 +47,7 @@ type AppConfig struct { AddToPlaylistSkipDuplicates bool ShowTrackChangeNotification bool EnableLrcLib bool + CustomLrcLibUrl string EnablePasswordStorage bool SkipSSLVerify bool EnqueueBatchSize int diff --git a/backend/lrclib.go b/backend/lrclib.go index 11f215e..685583d 100644 --- a/backend/lrclib.go +++ b/backend/lrclib.go @@ -9,6 +9,7 @@ import ( "fmt" "log" "net/http" + "net/url" "os" "path/filepath" "regexp" @@ -23,13 +24,14 @@ import ( const lrclibCacheFolder = "lrclib" type LrcLibFetcher struct { - cachePath string + cachePath string + customLrcLibUrl string } -func NewLrcLibFetcher(baseCacheDir string) *LrcLibFetcher { +func NewLrcLibFetcher(baseCacheDir string, customLrcLibUrl string) *LrcLibFetcher { cachePath := filepath.Join(baseCacheDir, lrclibCacheFolder) configdir.MakePath(cachePath) - return &LrcLibFetcher{cachePath: cachePath} + return &LrcLibFetcher{cachePath: cachePath, customLrcLibUrl: customLrcLibUrl} } func (l *LrcLibFetcher) FetchLrcLibLyrics(name, artist, album string, durationSecs int) (*mediaprovider.Lyrics, error) { @@ -68,7 +70,9 @@ func (l *LrcLibFetcher) fetchFromServer(name, artist, album string, durationSecs ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://lrclib.net/api/get", nil) + lrclibUrl := l.getLrclibUrl() + fmt.Println(lrclibUrl) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, lrclibUrl, nil) if err != nil { return nil, err } @@ -91,6 +95,21 @@ func (l *LrcLibFetcher) fetchFromServer(name, artist, album string, durationSecs return parseLrcLibResponse(resp) } +// Returns the Lrclib url the fetcher should use, based on the configuration. +func (l *LrcLibFetcher) getLrclibUrl() string { + if l.customLrcLibUrl != "" { + u, err := url.JoinPath(l.customLrcLibUrl, "/api/get") + if err == nil { + // Return custom cunfig url + return u + } + + log.Printf("Invalid LrcLib URL passed (err: %s). Falling back to default", err) + } + + return "https://lrclib.net/api/get" +} + func parseLrcLibResponse(resp *http.Response) (*mediaprovider.Lyrics, error) { if resp.StatusCode == http.StatusNotFound { return nil, errors.New("lrclib lyrics not found")