mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-14 19:20:37 +03:00
Add initial last.fm client implementation
This commit is contained in:
parent
61d0bd4729
commit
eb74dad7cd
@ -41,6 +41,7 @@ type configOptions struct {
|
|||||||
AuthWindowLength time.Duration
|
AuthWindowLength time.Duration
|
||||||
|
|
||||||
Scanner scannerOptions
|
Scanner scannerOptions
|
||||||
|
LastFM lastfmOptions
|
||||||
|
|
||||||
// DevFlags. These are used to enable/disable debugging and incomplete features
|
// DevFlags. These are used to enable/disable debugging and incomplete features
|
||||||
DevLogSourceLine bool
|
DevLogSourceLine bool
|
||||||
@ -51,6 +52,12 @@ type scannerOptions struct {
|
|||||||
Extractor string
|
Extractor string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type lastfmOptions struct {
|
||||||
|
ApiKey string
|
||||||
|
Secret string
|
||||||
|
Language string
|
||||||
|
}
|
||||||
|
|
||||||
var Server = &configOptions{}
|
var Server = &configOptions{}
|
||||||
|
|
||||||
func LoadFromFile(confFile string) {
|
func LoadFromFile(confFile string) {
|
||||||
@ -107,6 +114,7 @@ func init() {
|
|||||||
viper.SetDefault("authwindowlength", 20*time.Second)
|
viper.SetDefault("authwindowlength", 20*time.Second)
|
||||||
|
|
||||||
viper.SetDefault("scanner.extractor", "taglib")
|
viper.SetDefault("scanner.extractor", "taglib")
|
||||||
|
viper.SetDefault("lastfm.language", "en")
|
||||||
|
|
||||||
// DevFlags. These are used to enable/disable debugging and incomplete features
|
// DevFlags. These are used to enable/disable debugging and incomplete features
|
||||||
viper.SetDefault("devlogsourceline", false)
|
viper.SetDefault("devlogsourceline", false)
|
||||||
|
67
core/lastfm/client.go
Normal file
67
core/lastfm/client.go
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
package lastfm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
apiBaseUrl = "https://ws.audioscrobbler.com/2.0/"
|
||||||
|
)
|
||||||
|
|
||||||
|
type HttpClient interface {
|
||||||
|
Do(req *http.Request) (*http.Response, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewClient(apiKey string, lang string, hc HttpClient) *Client {
|
||||||
|
return &Client{apiKey, lang, hc}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Client struct {
|
||||||
|
apiKey string
|
||||||
|
lang string
|
||||||
|
hc HttpClient
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO SimilarArtists()
|
||||||
|
func (c *Client) ArtistGetInfo(name string) (*Artist, error) {
|
||||||
|
params := url.Values{}
|
||||||
|
params.Add("method", "artist.getInfo")
|
||||||
|
params.Add("format", "json")
|
||||||
|
params.Add("api_key", c.apiKey)
|
||||||
|
params.Add("artist", name)
|
||||||
|
params.Add("lang", c.lang)
|
||||||
|
req, _ := http.NewRequest("GET", apiBaseUrl, nil)
|
||||||
|
req.URL.RawQuery = params.Encode()
|
||||||
|
|
||||||
|
resp, err := c.hc.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
data, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
return nil, c.parseError(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
var response Response
|
||||||
|
err = json.Unmarshal(data, &response)
|
||||||
|
return &response.Artist, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) parseError(data []byte) error {
|
||||||
|
var e Error
|
||||||
|
err := json.Unmarshal(data, &e)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return fmt.Errorf("last.fm error(%d): %s", e.Code, e.Message)
|
||||||
|
}
|
76
core/lastfm/client_test.go
Normal file
76
core/lastfm/client_test.go
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
package lastfm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
. "github.com/onsi/ginkgo"
|
||||||
|
. "github.com/onsi/gomega"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ = Describe("Client", func() {
|
||||||
|
var httpClient *fakeHttpClient
|
||||||
|
var client *Client
|
||||||
|
|
||||||
|
BeforeEach(func() {
|
||||||
|
httpClient = &fakeHttpClient{}
|
||||||
|
client = NewClient("API_KEY", "pt", httpClient)
|
||||||
|
})
|
||||||
|
|
||||||
|
Describe("ArtistInfo", func() {
|
||||||
|
It("returns an artist for a successful response", func() {
|
||||||
|
f, _ := os.Open("tests/fixtures/lastfm.artist.getinfo.json")
|
||||||
|
httpClient.res = http.Response{Body: f, StatusCode: 200}
|
||||||
|
|
||||||
|
artist, err := client.ArtistGetInfo("U2")
|
||||||
|
Expect(err).To(BeNil())
|
||||||
|
Expect(artist.Name).To(Equal("U2"))
|
||||||
|
Expect(httpClient.savedRequest.URL.String()).To(Equal(apiBaseUrl + "?api_key=API_KEY&artist=U2&format=json&lang=pt&method=artist.getInfo"))
|
||||||
|
})
|
||||||
|
|
||||||
|
It("fails if Last.FM returns an error", func() {
|
||||||
|
httpClient.res = http.Response{
|
||||||
|
Body: ioutil.NopCloser(bytes.NewBufferString(`{"error":3,"message":"Invalid Method - No method with that name in this package"}`)),
|
||||||
|
StatusCode: 400,
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := client.ArtistGetInfo("U2")
|
||||||
|
Expect(err).To(MatchError("last.fm error(3): Invalid Method - No method with that name in this package"))
|
||||||
|
})
|
||||||
|
|
||||||
|
It("fails if HttpClient.Do() returns error", func() {
|
||||||
|
httpClient.err = errors.New("generic error")
|
||||||
|
|
||||||
|
_, err := client.ArtistGetInfo("U2")
|
||||||
|
Expect(err).To(MatchError("generic error"))
|
||||||
|
})
|
||||||
|
|
||||||
|
It("fails if returned body is not a valid JSON", func() {
|
||||||
|
httpClient.res = http.Response{
|
||||||
|
Body: ioutil.NopCloser(bytes.NewBufferString(`<xml>NOT_VALID_JSON</xml>`)),
|
||||||
|
StatusCode: 200,
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := client.ArtistGetInfo("U2")
|
||||||
|
Expect(err).To(MatchError("invalid character '<' looking for beginning of value"))
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
type fakeHttpClient struct {
|
||||||
|
res http.Response
|
||||||
|
err error
|
||||||
|
savedRequest *http.Request
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *fakeHttpClient) Do(req *http.Request) (*http.Response, error) {
|
||||||
|
c.savedRequest = req
|
||||||
|
if c.err != nil {
|
||||||
|
return nil, c.err
|
||||||
|
}
|
||||||
|
return &c.res, nil
|
||||||
|
}
|
17
core/lastfm/lastfm_suite_test.go
Normal file
17
core/lastfm/lastfm_suite_test.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package lastfm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/deluan/navidrome/log"
|
||||||
|
"github.com/deluan/navidrome/tests"
|
||||||
|
. "github.com/onsi/ginkgo"
|
||||||
|
. "github.com/onsi/gomega"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLastFM(t *testing.T) {
|
||||||
|
tests.Init(t, false)
|
||||||
|
log.SetLevel(log.LevelCritical)
|
||||||
|
RegisterFailHandler(Fail)
|
||||||
|
RunSpecs(t, "LastFM Test Suite")
|
||||||
|
}
|
45
core/lastfm/responses.go
Normal file
45
core/lastfm/responses.go
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
package lastfm
|
||||||
|
|
||||||
|
type Response struct {
|
||||||
|
Artist Artist `json:"artist"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Artist struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
MBID string `json:"mbid"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
Image []ArtistImage `json:"image"`
|
||||||
|
Streamable string `json:"streamable"`
|
||||||
|
Stats struct {
|
||||||
|
Listeners string `json:"listeners"`
|
||||||
|
Plays string `json:"plays"`
|
||||||
|
} `json:"stats"`
|
||||||
|
Similar struct {
|
||||||
|
Artists []Artist `json:"artist"`
|
||||||
|
} `json:"similar"`
|
||||||
|
Tags struct {
|
||||||
|
Tag []ArtistTag `json:"tag"`
|
||||||
|
} `json:"tags"`
|
||||||
|
Bio ArtistBio `json:"bio"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ArtistImage struct {
|
||||||
|
URL string `json:"#text"`
|
||||||
|
Size string `json:"size"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ArtistTag struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ArtistBio struct {
|
||||||
|
Published string `json:"published"`
|
||||||
|
Summary string `json:"summary"`
|
||||||
|
Content string `json:"content"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Error struct {
|
||||||
|
Code int `json:"error"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
}
|
42
core/lastfm/responses_test.go
Normal file
42
core/lastfm/responses_test.go
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package lastfm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io/ioutil"
|
||||||
|
|
||||||
|
. "github.com/onsi/ginkgo"
|
||||||
|
. "github.com/onsi/gomega"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ = Describe("LastFM responses", func() {
|
||||||
|
Describe("Artist", func() {
|
||||||
|
It("parses the response correctly", func() {
|
||||||
|
var resp Response
|
||||||
|
body, _ := ioutil.ReadFile("tests/fixtures/lastfm.artist.getinfo.json")
|
||||||
|
err := json.Unmarshal(body, &resp)
|
||||||
|
Expect(err).To(BeNil())
|
||||||
|
|
||||||
|
Expect(resp.Artist.Name).To(Equal("U2"))
|
||||||
|
Expect(resp.Artist.MBID).To(Equal("a3cb23fc-acd3-4ce0-8f36-1e5aa6a18432"))
|
||||||
|
Expect(resp.Artist.URL).To(Equal("https://www.last.fm/music/U2"))
|
||||||
|
Expect(resp.Artist.Bio.Summary).To(ContainSubstring("U2 é uma das mais importantes bandas de rock de todos os tempos"))
|
||||||
|
|
||||||
|
similarArtists := []string{"Passengers", "INXS", "R.E.M.", "Simple Minds", "Bono"}
|
||||||
|
for i, similar := range similarArtists {
|
||||||
|
Expect(resp.Artist.Similar.Artists[i].Name).To(Equal(similar))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Describe("Error", func() {
|
||||||
|
It("parses the error response correctly", func() {
|
||||||
|
var error Error
|
||||||
|
body := []byte(`{"error":3,"message":"Invalid Method - No method with that name in this package"}`)
|
||||||
|
err := json.Unmarshal(body, &error)
|
||||||
|
Expect(err).To(BeNil())
|
||||||
|
|
||||||
|
Expect(error.Code).To(Equal(3))
|
||||||
|
Expect(error.Message).To(Equal("Invalid Method - No method with that name in this package"))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
1
tests/fixtures/lastfm.artist.getinfo.json
vendored
Normal file
1
tests/fixtures/lastfm.artist.getinfo.json
vendored
Normal file
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user