This repository has been archived by the owner on May 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cddb.go
197 lines (166 loc) · 4.76 KB
/
cddb.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package cddb
import (
"bytes"
"encoding/xml"
"errors"
"fmt"
"net/http"
"strings"
)
// Constants for supported Gracenote commands.
const (
CommandFetch = "ALBUM_FETCH"
CommandTOC = "ALBUM_TOC"
)
// Format string for Gracenote's API endpoint.
//
// The first verb is replaced with the digits of the supplied Client ID that
// precede the hyphen.
const endpointFormat = "https://c%v.web.cddbp.net/webapi/xml/1.0/"
// Auth stores the required authentication strings needed to use
// the Gracenote API.
type Auth struct {
Client string `xml:"CLIENT"`
User string `xml:"USER"`
}
// A client to query Gracenote.
//
// For specifics on the effects of the Language and Country parameters,
// please refer to the follow articles:
// https://developer.gracenote.com/sites/default/files/web/webapi/Content/music-web-api/Setting%20the%20Language%20Preference.html
//
// https://developer.gracenote.com/sites/default/files/web/webapi/Content/music-web-api/Specifying%20a%20Country%20Specific.html
type Client struct {
// Auth stores the required authentication strings needed to use
// the Gracenote API.
//
// Both Client and User fields of Auth are required, an error will be
// returned if either are empty.
Auth Auth `xml:"AUTH"`
// Language sets the LANG parameter of the Gracenote request.
//
// If Language is empty, it will be omitted from the query.
Language string `xml:"LANG,omitempty"`
// Country sets the COUNTRY parameter of the Gracenote request.
//
// If Country is empty, it will be omitted from the query.
Country string `xml:"COUNTRY,omitempty"`
// HTTPClient is the HTTP client used to query the Gracenote API.
//
// If HTTPClient is nil, http.DefaultClient is used.
HTTPClient *http.Client `xml:"-"`
}
// TOC describes the Table of Contents of an Audio CD.
//
// Gracenote expects the TOC to contain the absolute frame offset to
// the start of each track, with the final offset being the total CD
// length in frames. Each offset should be separated by a space.
//
// Gracenote expects the offsets to include the lead-in frames of the CD,
// and will return an error if the TOC contains any offset less than 1.
type TOC struct {
Offsets string `xml:"OFFSETS,omitempty"`
}
// Gracenote query
//
// Command field is required, an error will be returned if it is empty.
type Query struct {
Command string `xml:"CMD,attr"`
ID string `xml:"GN_ID,omitempty"`
TOC *TOC `xml:"TOC,omitempty"`
}
type queries struct {
XMLName string `xml:"QUERIES"`
*Client
Query *Query `xml:"QUERY"`
}
type Track struct {
Number int `xml:"TRACK_NUM"`
ID string `xml:"GN_ID"`
Artist string `xml:"ARTIST"`
Title string `xml:"TITLE"`
}
type Album struct {
ID string `xml:"GN_ID"`
Artist string `xml:"ARTIST"`
Title string `xml:"TITLE"`
Date int `xml:"DATE"`
Genre string `xml:"GENRE"`
TrackCount int `xml:"TRACK_COUNT"`
Tracks []Track `xml:"TRACK"`
}
type Response struct {
Status string `xml:"STATUS,attr"`
Albums []Album `xml:"ALBUM"`
}
type responses struct {
Message string `xml:"MESSAGE"`
Response Response `xml:"RESPONSE"`
}
func (q *Query) SetCommand(command string) {
q.Command = command
}
func (q *Query) SetID(id string) {
q.ID = id
}
func (q *Query) SetTOC(offsets string) {
if q.TOC == nil {
q.TOC = &TOC{}
}
q.TOC.Offsets = offsets
}
func (c *Client) SetAuth(client string, user string) {
c.Auth.Client = client
c.Auth.User = user
}
func (c *Client) SetLanguage(language string) {
c.Language = language
}
func (c *Client) SetCountry(country string) {
c.Country = country
}
func (c *Client) SetHTTPClient(httpClient *http.Client) {
c.HTTPClient = httpClient
}
func (c *Client) Do(q *Query) (*Response, error) {
if c.Auth.Client == "" {
return nil, errors.New("cddb: Missing Client ID")
}
if c.Auth.User == "" {
return nil, errors.New("cddb: Missing User ID")
}
if q.Command == "" {
return nil, errors.New("cddb: Missing command")
}
query := &queries{Client: c, Query: q}
buffer := &bytes.Buffer{}
err := xml.NewEncoder(buffer).Encode(query)
if err != nil {
return nil, err
}
endpoint := fmt.Sprintf(endpointFormat, strings.Split(c.Auth.Client, "-")[0])
httpClient := c.httpClient()
resp, err := httpClient.Post(endpoint, "application/xml", buffer)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("cddb: Gracenote returned status code %v", resp.StatusCode)
}
var r responses
err = xml.NewDecoder(resp.Body).Decode(&r)
if err != nil {
return nil, err
}
if r.Response.Status == "ERROR" {
return nil, fmt.Errorf("cddb: Gracenote returned error: %v", r.Message)
}
return &r.Response, nil
}
func (c *Client) httpClient() *http.Client {
if c.HTTPClient != nil {
return c.HTTPClient
}
return http.DefaultClient
}