-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathacoustid.go
78 lines (64 loc) · 1.55 KB
/
acoustid.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
70
71
72
73
74
75
76
77
78
package acoustid
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
func check(err error) {
if err != nil {
panic(err)
}
}
type AcoustIDRequest struct {
Fingerprint string `json:"fingerprint"`
Duration int `json:"duration"`
ApiKey string `json:"client"`
Metadata string `json:"meta"`
}
type Result struct {
ID string `json:"id"`
Recordings []struct {
Artists []struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"artists"`
ReleaseGroups []struct {
Type string `json:"type"`
ID string `json:"id"`
Title string `json:"title"`
SecondaryTypes []string `json:"secondarytypes"`
} `json:"releasegroups"`
Duration float64 `json:"duration"`
ID string `json:"id"`
Title string `json:"title"`
} `json:"recordings"`
Score float64 `json:"score"`
}
type AcoustIDResponse struct {
Results []Result `json:"results"`
Status string `json:"status"`
}
func (a *AcoustIDRequest) Do() AcoustIDResponse {
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)
aidResp := AcoustIDResponse{}
err = json.Unmarshal(body, &aidResp)
check(err)
return aidResp
}
func (a *AcoustIDRequest) PostValues() url.Values {
query := fmt.Sprintf(
"client=%s&duration=%d&meta=%s&fingerprint=%s",
a.ApiKey,
a.Duration,
a.Metadata,
a.Fingerprint)
values, err := url.ParseQuery(query)
check(err)
return values
}