diff options
Diffstat (limited to 'lfmclient.go')
| -rw-r--r-- | lfmclient.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/lfmclient.go b/lfmclient.go new file mode 100644 index 0000000..244ab8c --- /dev/null +++ b/lfmclient.go @@ -0,0 +1,47 @@ +package main + +import ( + "encoding/json" + "io/ioutil" + "net/http" +) + +type AlbumImage struct { + Size string `json:"size"` + Href string `json:"#text"` +} + +type TrackAlbum struct { + Images []AlbumImage `json:"image"` +} + +func (t TrackGetInfo) GetImage(size string) string { + images := t.Album.Images + for _, image := range images { + if image.Size == size { + return image.Href + } + } + return "https://lastfm-img2.akamaized.net/i/u/64s/4128a6eb29f94943c9d206c08e625904.png" +} + +type TrackGetInfo struct { + Mbid string `json:"mbid"` + Album TrackAlbum `jon:"album"` +} + +func (app *App) TrackInfo(track string, artist string) TrackGetInfo { + r, _ := http.NewRequest("GET", "http://ws.audioscrobbler.com/2.0/", nil) + values := r.URL.Query() + values.Add("method", "track.getInfo") + values.Add("api_key", app.Config.Lfm.ApiKey) + values.Add("artist", artist) + values.Add("track", track) + values.Add("format", "json") + r.URL.RawQuery = values.Encode() + resp, _ := http.DefaultClient.Do(r) + body, _ := ioutil.ReadAll(resp.Body) + var dst map[string]TrackGetInfo + json.Unmarshal(body, &dst) + return dst["track"] +} |
