implement getArtistInfo

This commit is contained in:
sentriz
2020-02-12 14:56:16 +00:00
parent 36829c69e3
commit c16897dfe4
5 changed files with 102 additions and 24 deletions

View File

@@ -23,6 +23,8 @@ var (
}
)
// TODO: remove this package's dependency on models/db
func getParamSignature(params url.Values, secret string) string {
// the parameters must be in order before hashing
paramKeys := make([]string, 0)
@@ -40,22 +42,22 @@ func getParamSignature(params url.Values, secret string) string {
return hex.EncodeToString(hash[:])
}
func makeRequest(method string, params url.Values) (*LastFM, error) {
func makeRequest(method string, params url.Values) (LastFM, error) {
req, _ := http.NewRequest(method, baseURL, nil)
req.URL.RawQuery = params.Encode()
resp, err := client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "get")
return LastFM{}, errors.Wrap(err, "get")
}
defer resp.Body.Close()
decoder := xml.NewDecoder(resp.Body)
lastfm := &LastFM{}
err = decoder.Decode(lastfm)
if err != nil {
return nil, errors.Wrap(err, "decoding")
lastfm := LastFM{}
if err = decoder.Decode(&lastfm); err != nil {
return LastFM{}, errors.Wrap(err, "decoding")
}
if lastfm.Error != nil {
return nil, fmt.Errorf("parsing: %v", lastfm.Error.Value)
//?
if lastfm.Error.Code != 0 {
return LastFM{}, fmt.Errorf("parsing: %v", lastfm.Error.Value)
}
return lastfm, nil
}
@@ -100,3 +102,15 @@ func Scrobble(apiKey, secret, session string, opts ScrobbleOpts) error {
_, err := makeRequest("POST", params)
return err
}
func ArtistGetInfo(apiKey string, artist *model.Artist) (Artist, error) {
params := url.Values{}
params.Add("method", "artist.getInfo")
params.Add("api_key", apiKey)
params.Add("artist", artist.Name)
resp, err := makeRequest("GET", params)
if err != nil {
return Artist{}, errors.Wrap(err, "making artist GET")
}
return resp.Artist, nil
}

View File

@@ -5,8 +5,9 @@ import "encoding/xml"
type LastFM struct {
XMLName xml.Name `xml:"lfm"`
Status string `xml:"status,attr"`
Session *Session `xml:"session"`
Error *Error `xml:"error"`
Session Session `xml:"session"`
Error Error `xml:"error"`
Artist Artist `xml:"artist"`
}
type Session struct {
@@ -19,3 +20,37 @@ type Error struct {
Code uint `xml:"code,attr"`
Value string `xml:",chardata"`
}
type Artist struct {
XMLName xml.Name `xml:"artist"`
Name string `xml:"name"`
MBID string `xml:"mbid"`
URL string `xml:"url"`
Image []struct {
Text string `xml:",chardata"`
Size string `xml:"size,attr"`
} `xml:"image"`
Streamable string `xml:"streamable"`
Stats struct {
Listeners string `xml:"listeners"`
Plays string `xml:"plays"`
} `xml:"stats"`
Similar struct {
Artists []Artist `xml:"artist"`
} `xml:"similar"`
Tags struct {
Tag []ArtistTag `xml:"tag"`
} `xml:"tags"`
Bio ArtistBio `xml:"bio"`
}
type ArtistTag struct {
Name string `xml:"name"`
URL string `xml:"url"`
}
type ArtistBio struct {
Published string `xml:"published"`
Summary string `xml:"summary"`
Content string `xml:"content"`
}