aboutsummaryrefslogtreecommitdiffstats
path: root/lfmclient.go
blob: 244ab8c8b71574efb14509cb3c3f79a6a20ca0db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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"]
}