-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.go
64 lines (51 loc) · 1.32 KB
/
url.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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
type Urls struct {
MyspaceUrl string `json:"myspace_url,omitempty"`
LastfmUrl string `json:"lastfm_url,omitempty"`
MbUrl string `json:"mb_url,omitempty"`
WikipediaUrl string `json:"wikipedia_url,omitempty"`
OfficialUrl string `json:"official_url,omitempty"`
}
type UrlsStatus struct {
Code int `json:"code"`
Message string `json:"message"`
Version string `json:"version"`
}
type UrlResponse struct {
Status UrlsStatus `json:"status"`
Urls Urls `json:"urls"`
}
type UrlsSearchResults struct {
Response UrlResponse `json:"response"`
}
func FetchUrls(artist string) (Urls, error) {
url := fmt.Sprintf("%s/urls?api_key=%s&name=%s&format=json", ApiRoot, ApiKey, url.QueryEscape(artist))
results := UrlsSearchResults{}
res, err := http.Get(url)
eUrls := Urls{}
if err != nil {
return eUrls, err
}
err = json.NewDecoder(res.Body).Decode(&results)
if err != nil {
return eUrls, err
}
if results.Response.Status.Code == 5 {
return eUrls, fmt.Errorf("Theres no band with this name: %s. Please try again", artist)
}
return results.Response.Urls, nil
}
func RunUrls(artist string, out io.Writer) {
urls, err := FetchUrls(artist)
if err != nil {
fmt.Fprintln(out, err)
}
fmt.Fprintf(out, FormatStructToText(urls))
}