-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocation.go
69 lines (55 loc) · 1.46 KB
/
location.go
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
61
62
63
64
65
66
67
68
69
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
type ArtistLocation struct {
City string `json:"city"`
Country string `json:"country"`
Location string `json:"location"`
Region string `json:"region"`
}
type Artist struct {
ID string `json:"id"`
Name string `json:"name"`
ArtistLocation ArtistLocation `json:"artist_location"`
}
type LocationStatus struct {
Code int `json:"code"`
Message string `json:"message"`
Version string `json:"-"`
}
type LocationResponse struct {
Artist Artist `json:"artist"`
Status LocationStatus `json:"status"`
}
type LocationResults struct {
Response LocationResponse `json:"response"`
}
func RetrieveLocation(artist string) (ArtistLocation, error) {
url := fmt.Sprintf("%s/profile?api_key=%s&name=%s&format=json&bucket=artist_location", ApiRoot, ApiKey, url.QueryEscape(artist))
results := LocationResults{}
res, err := http.Get(url)
al := ArtistLocation{}
if err != nil {
return al, err
}
err = json.NewDecoder(res.Body).Decode(&results)
if err != nil {
return al, err
}
if results.Response.Status.Code == 5 {
return al, fmt.Errorf("Theres no band with this name: %s. Please try again", artist)
}
return results.Response.Artist.ArtistLocation, nil
}
func RunLocation(art string, out io.Writer) {
location, err := RetrieveLocation(art)
if err != nil {
fmt.Fprintln(out, err)
}
fmt.Fprintf(out, FormatStructToText(location))
}