-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlastfm.go
More file actions
60 lines (53 loc) · 1.31 KB
/
lastfm.go
File metadata and controls
60 lines (53 loc) · 1.31 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
)
type ArtistInfoResponse struct {
Artist ArtistInfo `json:"artist"`
}
type ArtistInfo struct {
Tags Tags `json:"tags"`
}
type Tags struct {
Tag []TagInfo `json:"tag"`
}
type TagInfo struct {
Name string `json:"name"`
}
const lastFmBaseUrl = "http://ws.audioscrobbler.com/2.0/?%s"
func GetArtistGenre(artist string) (string, error) {
params := make(url.Values)
params["method"] = []string{"artist.getinfo"}
params["artist"] = []string{artist}
params["api_key"] = []string{os.Getenv("LASTFMTOKEN")}
params["format"] = []string{"json"}
log.Println("GET:", fmt.Sprintf(lastFmBaseUrl, params.Encode()))
resp, err := http.Get(fmt.Sprintf(lastFmBaseUrl, params.Encode()))
if err != nil {
log.Println("Could not GET Last.FM:", err.Error())
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("Could not read resp:", err.Error())
return "", err
}
artistInfo := ArtistInfoResponse{}
err = json.Unmarshal(body, &artistInfo)
if err != nil {
log.Println("Could not unmarshal JSON:", err.Error())
return "", err
}
if len(artistInfo.Artist.Tags.Tag) == 0 {
return "", errors.New("Artist not found")
}
return artistInfo.Artist.Tags.Tag[0].Name, nil
}