🎧 Your Personal Streaming Service
Go airsonicmadsonicmedia-servermusicmusic-servernavidromeopensubsonicraspberry-piself-hostedstreamerstreaming-apistreaming-audiosubsonicsubsonic-server
Move all Spotify and LastFM code into only one folder for each
Deluane80cf80d05d8
diff --git a/utils/lastfm/client.go b/core/agents/lastfm/client.gosimilarity index 94%rename from utils/lastfm/client.gorename to core/agents/lastfm/client.goindex 579c4bc86..22b22c974 100644--- a/utils/lastfm/client.go+++ b/core/agents/lastfm/client.go@@ -14,12 +14,12 @@ const (apiBaseUrl = "https://ws.audioscrobbler.com/2.0/")-type Error struct {+type lastFMError struct {Code intMessage string}-func (e *Error) Error() string {+func (e *lastFMError) Error() string {return fmt.Sprintf("last.fm error(%d): %s", e.Code, e.Message)}@@ -67,7 +67,7 @@ func (c *Client) makeRequest(params url.Values) (*Response, error) {}if response.Error != 0 {- return &response, &Error{Code: response.Error, Message: response.Message}+ return &response, &lastFMError{Code: response.Error, Message: response.Message}}return &response, nildiff --git a/utils/lastfm/client_test.go b/core/agents/lastfm/client_test.gosimilarity index 93%rename from utils/lastfm/client_test.gorename to core/agents/lastfm/client_test.goindex 49f824757..e82dc2cae 100644--- a/utils/lastfm/client_test.go+++ b/core/agents/lastfm/client_test.go@@ -9,7 +9,6 @@ import ("os""github.com/navidrome/navidrome/tests"-. "github.com/onsi/ginkgo". "github.com/onsi/gomega")@@ -51,7 +50,7 @@ var _ = Describe("Client", func() {}_, err := client.ArtistGetInfo(context.TODO(), "U2", "123")- Expect(err).To(MatchError(&Error{Code: 3, Message: "Invalid Method - No method with that name in this package"}))+ Expect(err).To(MatchError(&lastFMError{Code: 3, Message: "Invalid Method - No method with that name in this package"}))})It("fails if Last.FM returns an error", func() {@@ -61,7 +60,7 @@ var _ = Describe("Client", func() {}_, err := client.ArtistGetInfo(context.TODO(), "U2", "123")- Expect(err).To(MatchError(&Error{Code: 6, Message: "The artist you supplied could not be found"}))+ Expect(err).To(MatchError(&lastFMError{Code: 6, Message: "The artist you supplied could not be found"}))})It("fails if HttpClient.Do() returns error", func() {diff --git a/core/agents/lastfm.go b/core/agents/lastfm/lastfm.gosimilarity index 79%rename from core/agents/lastfm.gorename to core/agents/lastfm/lastfm.goindex 44399082e..7c9ae2e87 100644--- a/core/agents/lastfm.go+++ b/core/agents/lastfm/lastfm.go@@ -1,4 +1,4 @@-package agents+package lastfmimport ("context"@@ -6,8 +6,9 @@ import ("github.com/navidrome/navidrome/conf""github.com/navidrome/navidrome/consts"+ "github.com/navidrome/navidrome/core/agents""github.com/navidrome/navidrome/log"- "github.com/navidrome/navidrome/utils/lastfm"+ "github.com/navidrome/navidrome/utils")const (@@ -20,10 +21,10 @@ type lastfmAgent struct {ctx context.ContextapiKey stringlang string- client *lastfm.Client+ client *Client}-func lastFMConstructor(ctx context.Context) Interface {+func lastFMConstructor(ctx context.Context) agents.Interface {l := &lastfmAgent{ctx: ctx,lang: conf.Server.LastFM.Language,@@ -33,8 +34,8 @@ func lastFMConstructor(ctx context.Context) Interface {} else {l.apiKey = lastFMAPIKey}- hc := NewCachedHTTPClient(http.DefaultClient, consts.DefaultCachedHttpClientTTL)- l.client = lastfm.NewClient(l.apiKey, l.lang, hc)+ hc := utils.NewCachedHTTPClient(http.DefaultClient, consts.DefaultCachedHttpClientTTL)+ l.client = NewClient(l.apiKey, l.lang, hc)return l}@@ -48,7 +49,7 @@ func (l *lastfmAgent) GetMBID(id string, name string) (string, error) {return "", err}if a.MBID == "" {- return "", ErrNotFound+ return "", agents.ErrNotFound}return a.MBID, nil}@@ -59,7 +60,7 @@ func (l *lastfmAgent) GetURL(id, name, mbid string) (string, error) {return "", err}if a.URL == "" {- return "", ErrNotFound+ return "", agents.ErrNotFound}return a.URL, nil}@@ -70,22 +71,22 @@ func (l *lastfmAgent) GetBiography(id, name, mbid string) (string, error) {return "", err}if a.Bio.Summary == "" {- return "", ErrNotFound+ return "", agents.ErrNotFound}return a.Bio.Summary, nil}-func (l *lastfmAgent) GetSimilar(id, name, mbid string, limit int) ([]Artist, error) {+func (l *lastfmAgent) GetSimilar(id, name, mbid string, limit int) ([]agents.Artist, error) {resp, err := l.callArtistGetSimilar(name, mbid, limit)if err != nil {return nil, err}if len(resp) == 0 {- return nil, ErrNotFound+ return nil, agents.ErrNotFound}- var res []Artist+ var res []agents.Artistfor _, a := range resp {- res = append(res, Artist{+ res = append(res, agents.Artist{Name: a.Name,MBID: a.MBID,})@@ -93,17 +94,17 @@ func (l *lastfmAgent) GetSimilar(id, name, mbid string, limit int) ([]Artist, erreturn res, nil}-func (l *lastfmAgent) GetTopSongs(id, artistName, mbid string, count int) ([]Song, error) {+func (l *lastfmAgent) GetTopSongs(id, artistName, mbid string, count int) ([]agents.Song, error) {resp, err := l.callArtistGetTopTracks(artistName, mbid, count)if err != nil {return nil, err}if len(resp) == 0 {- return nil, ErrNotFound+ return nil, agents.ErrNotFound}- var res []Song+ var res []agents.Songfor _, t := range resp {- res = append(res, Song{+ res = append(res, agents.Song{Name: t.Name,MBID: t.MBID,})@@ -111,9 +112,9 @@ func (l *lastfmAgent) GetTopSongs(id, artistName, mbid string, count int) ([]Sonreturn res, nil}-func (l *lastfmAgent) callArtistGetInfo(name string, mbid string) (*lastfm.Artist, error) {+func (l *lastfmAgent) callArtistGetInfo(name string, mbid string) (*Artist, error) {a, err := l.client.ArtistGetInfo(l.ctx, name, mbid)- lfErr, isLastFMError := err.(*lastfm.Error)+ lfErr, isLastFMError := err.(*lastFMError)if mbid != "" && ((err == nil && a.Name == "[unknown]") || (isLastFMError && lfErr.Code == 6)) {log.Warn(l.ctx, "LastFM/artist.getInfo could not find artist by mbid, trying again", "artist", name, "mbid", mbid)return l.callArtistGetInfo(name, "")@@ -126,9 +127,9 @@ func (l *lastfmAgent) callArtistGetInfo(name string, mbid string) (*lastfm.Artisreturn a, nil}-func (l *lastfmAgent) callArtistGetSimilar(name string, mbid string, limit int) ([]lastfm.Artist, error) {+func (l *lastfmAgent) callArtistGetSimilar(name string, mbid string, limit int) ([]Artist, error) {s, err := l.client.ArtistGetSimilar(l.ctx, name, mbid, limit)- lfErr, isLastFMError := err.(*lastfm.Error)+ lfErr, isLastFMError := err.(*lastFMError)if mbid != "" && ((err == nil && s.Attr.Artist == "[unknown]") || (isLastFMError && lfErr.Code == 6)) {log.Warn(l.ctx, "LastFM/artist.getSimilar could not find artist by mbid, trying again", "artist", name, "mbid", mbid)return l.callArtistGetSimilar(name, "", limit)@@ -140,9 +141,9 @@ func (l *lastfmAgent) callArtistGetSimilar(name string, mbid string, limit int)return s.Artists, nil}-func (l *lastfmAgent) callArtistGetTopTracks(artistName, mbid string, count int) ([]lastfm.Track, error) {+func (l *lastfmAgent) callArtistGetTopTracks(artistName, mbid string, count int) ([]Track, error) {t, err := l.client.ArtistGetTopTracks(l.ctx, artistName, mbid, count)- lfErr, isLastFMError := err.(*lastfm.Error)+ lfErr, isLastFMError := err.(*lastFMError)if mbid != "" && ((err == nil && t.Attr.Artist == "[unknown]") || (isLastFMError && lfErr.Code == 6)) {log.Warn(l.ctx, "LastFM/artist.getTopTracks could not find artist by mbid, trying again", "artist", artistName, "mbid", mbid)return l.callArtistGetTopTracks(artistName, "", count)@@ -157,7 +158,7 @@ func (l *lastfmAgent) callArtistGetTopTracks(artistName, mbid string, count int)func init() {conf.AddHook(func() {if conf.Server.LastFM.Enabled {- Register(lastFMAgentName, lastFMConstructor)+ agents.Register(lastFMAgentName, lastFMConstructor)}})}diff --git a/utils/lastfm/lastfm_suite_test.go b/core/agents/lastfm/lastfm_suite_test.gosimilarity index 100%rename from utils/lastfm/lastfm_suite_test.gorename to core/agents/lastfm/lastfm_suite_test.godiff --git a/core/agents/lastfm_test.go b/core/agents/lastfm/lastfm_test.gosimilarity index 97%rename from core/agents/lastfm_test.gorename to core/agents/lastfm/lastfm_test.goindex 00e523c1b..13bab4ee0 100644--- a/core/agents/lastfm_test.go+++ b/core/agents/lastfm/lastfm_test.go@@ -1,4 +1,4 @@-package agents+package lastfmimport ("bytes"@@ -8,10 +8,9 @@ import ("net/http""os"- "github.com/navidrome/navidrome/tests"- "github.com/navidrome/navidrome/utils/lastfm"-"github.com/navidrome/navidrome/conf"+ "github.com/navidrome/navidrome/core/agents"+ "github.com/navidrome/navidrome/tests". "github.com/onsi/ginkgo". "github.com/onsi/gomega")@@ -44,7 +43,7 @@ var _ = Describe("lastfmAgent", func() {var httpClient *tests.FakeHttpClientBeforeEach(func() {httpClient = &tests.FakeHttpClient{}- client := lastfm.NewClient("API_KEY", "pt", httpClient)+ client := NewClient("API_KEY", "pt", httpClient)agent = lastFMConstructor(context.TODO()).(*lastfmAgent)agent.client = client})@@ -102,7 +101,7 @@ var _ = Describe("lastfmAgent", func() {var httpClient *tests.FakeHttpClientBeforeEach(func() {httpClient = &tests.FakeHttpClient{}- client := lastfm.NewClient("API_KEY", "pt", httpClient)+ client := NewClient("API_KEY", "pt", httpClient)agent = lastFMConstructor(context.TODO()).(*lastfmAgent)agent.client = client})@@ -110,7 +109,7 @@ var _ = Describe("lastfmAgent", func() {It("returns similar artists", func() {f, _ := os.Open("tests/fixtures/lastfm.artist.getsimilar.json")httpClient.Res = http.Response{Body: f, StatusCode: 200}- Expect(agent.GetSimilar("123", "U2", "mbid-1234", 2)).To(Equal([]Artist{+ Expect(agent.GetSimilar("123", "U2", "mbid-1234", 2)).To(Equal([]agents.Artist{{Name: "Passengers", MBID: "e110c11f-1c94-4471-a350-c38f46b29389"},{Name: "INXS", MBID: "481bf5f9-2e7c-4c44-b08a-05b32bc7c00d"},}))@@ -163,7 +162,7 @@ var _ = Describe("lastfmAgent", func() {var httpClient *tests.FakeHttpClientBeforeEach(func() {httpClient = &tests.FakeHttpClient{}- client := lastfm.NewClient("API_KEY", "pt", httpClient)+ client := NewClient("API_KEY", "pt", httpClient)agent = lastFMConstructor(context.TODO()).(*lastfmAgent)agent.client = client})@@ -171,7 +170,7 @@ var _ = Describe("lastfmAgent", func() {It("returns top songs", func() {f, _ := os.Open("tests/fixtures/lastfm.artist.gettoptracks.json")httpClient.Res = http.Response{Body: f, StatusCode: 200}- Expect(agent.GetTopSongs("123", "U2", "mbid-1234", 2)).To(Equal([]Song{+ Expect(agent.GetTopSongs("123", "U2", "mbid-1234", 2)).To(Equal([]agents.Song{{Name: "Beautiful Day", MBID: "f7f264d0-a89b-4682-9cd7-a4e7c37637af"},{Name: "With or Without You", MBID: "6b9a509f-6907-4a6e-9345-2f12da09ba4b"},}))diff --git a/utils/lastfm/responses.go b/core/agents/lastfm/responses.gosimilarity index 100%rename from utils/lastfm/responses.gorename to core/agents/lastfm/responses.godiff --git a/utils/lastfm/responses_test.go b/core/agents/lastfm/responses_test.gosimilarity index 100%rename from utils/lastfm/responses_test.gorename to core/agents/lastfm/responses_test.godiff --git a/utils/spotify/client.go b/core/agents/spotify/client.gosimilarity index 100%rename from utils/spotify/client.gorename to core/agents/spotify/client.godiff --git a/utils/spotify/client_test.go b/core/agents/spotify/client_test.gosimilarity index 100%rename from utils/spotify/client_test.gorename to core/agents/spotify/client_test.godiff --git a/utils/spotify/responses.go b/core/agents/spotify/responses.gosimilarity index 100%rename from utils/spotify/responses.gorename to core/agents/spotify/responses.godiff --git a/utils/spotify/responses_test.go b/core/agents/spotify/responses_test.gosimilarity index 100%rename from utils/spotify/responses_test.gorename to core/agents/spotify/responses_test.godiff --git a/core/agents/spotify.go b/core/agents/spotify/spotify.gosimilarity index 75%rename from core/agents/spotify.gorename to core/agents/spotify/spotify.goindex 92f08042f..9555c454f 100644--- a/core/agents/spotify.go+++ b/core/agents/spotify/spotify.go@@ -1,4 +1,4 @@-package agents+package spotifyimport ("context"@@ -9,9 +9,10 @@ import ("github.com/navidrome/navidrome/conf""github.com/navidrome/navidrome/consts"+ "github.com/navidrome/navidrome/core/agents""github.com/navidrome/navidrome/log""github.com/navidrome/navidrome/model"- "github.com/navidrome/navidrome/utils/spotify"+ "github.com/navidrome/navidrome/utils""github.com/xrash/smetrics")@@ -21,17 +22,17 @@ type spotifyAgent struct {ctx context.Contextid stringsecret string- client *spotify.Client+ client *Client}-func spotifyConstructor(ctx context.Context) Interface {+func spotifyConstructor(ctx context.Context) agents.Interface {l := &spotifyAgent{ctx: ctx,id: conf.Server.Spotify.ID,secret: conf.Server.Spotify.Secret,}- hc := NewCachedHTTPClient(http.DefaultClient, consts.DefaultCachedHttpClientTTL)- l.client = spotify.NewClient(l.id, l.secret, hc)+ hc := utils.NewCachedHTTPClient(http.DefaultClient, consts.DefaultCachedHttpClientTTL)+ l.client = NewClient(l.id, l.secret, hc)return l}@@ -39,7 +40,7 @@ func (s *spotifyAgent) AgentName() string {return spotifyAgentName}-func (s *spotifyAgent) GetImages(id, name, mbid string) ([]ArtistImage, error) {+func (s *spotifyAgent) GetImages(id, name, mbid string) ([]agents.ArtistImage, error) {a, err := s.searchArtist(name)if err != nil {if err == model.ErrNotFound {@@ -50,9 +51,9 @@ func (s *spotifyAgent) GetImages(id, name, mbid string) ([]ArtistImage, error) {return nil, err}- var res []ArtistImage+ var res []agents.ArtistImagefor _, img := range a.Images {- res = append(res, ArtistImage{+ res = append(res, agents.ArtistImage{URL: img.URL,Size: img.Width,})@@ -60,7 +61,7 @@ func (s *spotifyAgent) GetImages(id, name, mbid string) ([]ArtistImage, error) {return res, nil}-func (s *spotifyAgent) searchArtist(name string) (*spotify.Artist, error) {+func (s *spotifyAgent) searchArtist(name string) (*Artist, error) {artists, err := s.client.SearchArtists(s.ctx, name, 40)if err != nil || len(artists) == 0 {return nil, model.ErrNotFound@@ -84,7 +85,7 @@ func (s *spotifyAgent) searchArtist(name string) (*spotify.Artist, error) {func init() {conf.AddHook(func() {if conf.Server.Spotify.ID != "" && conf.Server.Spotify.Secret != "" {- Register(spotifyAgentName, spotifyConstructor)+ agents.Register(spotifyAgentName, spotifyConstructor)}})}diff --git a/utils/spotify/spotify_suite_test.go b/core/agents/spotify/spotify_suite_test.gosimilarity index 100%rename from utils/spotify/spotify_suite_test.gorename to core/agents/spotify/spotify_suite_test.godiff --git a/core/agents/cached_http_client.go b/utils/cached_http_client.gosimilarity index 99%rename from core/agents/cached_http_client.gorename to utils/cached_http_client.goindex 616b2a296..c8acc1785 100644--- a/core/agents/cached_http_client.go+++ b/utils/cached_http_client.go@@ -1,4 +1,4 @@-package agents+package utilsimport ("bufio"diff --git a/core/agents/cached_http_client_test.go b/utils/cached_http_client_test.gosimilarity index 99%rename from core/agents/cached_http_client_test.gorename to utils/cached_http_client_test.goindex 36fb56b95..c7357328d 100644--- a/core/agents/cached_http_client_test.go+++ b/utils/cached_http_client_test.go@@ -1,4 +1,4 @@-package agents+package utilsimport ("fmt"