/
acousticid.go
60 lines (53 loc) · 1.39 KB
/
acousticid.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
package acousticid
import (
"net/http"
"io/ioutil"
"net/url"
"strconv"
"encoding/json"
)
func check(err error) {
if err != nil {
panic(err)
}
}
type AcousticidRequest struct {
Fingerprint string `json:fingerprint`
Duration int `json:duration`
Apikey string `json:"client"`
Metadata string `json:meta`
}
//Generated using http://json2struct.mervine.net/
type Result struct {
ID string `json:"id"`
Recordings []struct {
Artists []struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"artists"`
Duration float64 `json:"duration"`
ID string `json:"id"`
Title string `json:"title"`
} `json:"recordings"`
Score float64 `json:"score"`
}
type AcousticidResponse struct {
Results []Result `json:"results"`
Status string `json:"status"`
}
func (a *AcousticidRequest) Request() AcousticidResponse {
client := http.Client{}
response, err := client.PostForm("http://api.acoustid.org/v2/lookup", a.PostValues())
check(err)
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
aidresponse := AcousticidResponse{}
err = json.Unmarshal(body, &aidresponse)
check(err)
return aidresponse
}
func (a *AcousticidRequest) PostValues() url.Values {
values, err := url.ParseQuery("client=" + a.Apikey + "&duration=" + strconv.Itoa(a.Duration) + "&meta=" + a.Metadata + "&fingerprint=" + a.Fingerprint)
check(err)
return values
}