Move all Spotify and LastFM code into only one folder for each

This commit is contained in:
Deluan 2021-06-08 11:25:46 -04:00
parent 182e3ec78e
commit e80cf80d05
15 changed files with 52 additions and 52 deletions

View file

@ -14,12 +14,12 @@ const (
apiBaseUrl = "https://ws.audioscrobbler.com/2.0/"
)
type Error struct {
type lastFMError struct {
Code int
Message 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, nil

View file

@ -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() {

View file

@ -1,4 +1,4 @@
package agents
package lastfm
import (
"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.Context
apiKey string
lang 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.Artist
for _, 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, er
return 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.Song
for _, 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) ([]Son
return 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.Artis
return 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)
}
})
}

View file

@ -1,4 +1,4 @@
package agents
package lastfm
import (
"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.FakeHttpClient
BeforeEach(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.FakeHttpClient
BeforeEach(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.FakeHttpClient
BeforeEach(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"},
}))

View file

@ -1,4 +1,4 @@
package agents
package spotify
import (
"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.Context
id string
secret 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.ArtistImage
for _, 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)
}
})
}

View file

@ -1,4 +1,4 @@
package agents
package utils
import (
"bufio"

View file

@ -1,4 +1,4 @@
package agents
package utils
import (
"fmt"