-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusicbrainz.go
348 lines (296 loc) · 8.55 KB
/
musicbrainz.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package musicbrainz
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
)
// MusicBrainzAPIEndpoint represents the base URL of the MusicBrainz API
const MusicBrainzAPIEndpoint = "https://musicbrainz.org/ws/2/"
// Artist represents an artist in the MusicBrainz database
type Artist struct {
ID string `json:"id"`
Name string `json:"name"`
SortName string `json:"sort-name"`
Type string `json:"type"`
Country string `json:"country"`
Area string `json:"area"`
BeginDate string `json:"begin_date"`
EndDate string `json:"end_date"`
Disambig string `json:"disambiguation"`
Aliases []Alias `json:"aliases"`
Relations []Relation `json:"relations"`
Tags []Tag `json:"tags"`
}
// Alias represents an artist's alias in the MusicBrainz database
type Alias struct {
Name string `json:"name"`
Type string `json:"type"`
}
// Relation represents a relation between artists in the MusicBrainz database
type Relation struct {
Type string `json:"type"`
URL string `json:"url"`
Artist Artist `json:"artist"`
}
// Tag represents a tag associated with an artist in the MusicBrainz database
type Tag struct {
Name string `json:"name"`
}
// Release represents a release in the MusicBrainz database
type Release struct {
ID string `json:"id"`
Title string `json:"title"`
Status string `json:"status"`
TextRepresetation TextRepresentation `json:"text-representation"`
ArtistCredit []ArtistCredit `json:"artist-credit"`
ReleaseGroup ReleaseGroup `json:"release-group"`
Relations []Relation `json:"relations"`
Tags []Tag `json:"tags"`
CoverArtURL []CoverArtURL `json:"cover-art-archive"`
}
type CoverArtURL struct {
Artwork bool `json:"artwork"`
Front bool `json:"front"`
Back bool `json:"back"`
Count int `json:"count"`
Images []GBImage `json:"images"`
}
type GBImage struct {
ImageURL string `json:"image"`
Types []string `json:"types"`
}
// TextRepresentation represents the text representation of a release in the MusicBrainz database
type TextRepresentation struct {
Language string `json:"language"`
Script string `json:"script"`
}
// ArtistCredit represents the artist credit of a release in the MusicBrainz database
type ArtistCredit struct {
Name string `json:"name"`
}
// ArtistName represents the name of an artist in the MusicBrainz database
type ArtistName struct {
Name string `json:"name"`
}
// ReleaseGroup represents the release group of a release in the MusicBrainz database
type ReleaseGroup struct {
ID string `json:"id"`
Type string `json:"type"`
}
// Recording represents a recording in the MusicBrainz database
type Recording struct {
ID string `json:"id"`
Title string `json:"title"`
Length int `json:"length"`
ReleaseDate string `json:"first-release-date"`
Relations []Relation `json:"relations"`
Tags []Tag `json:"tags"`
ArtistCredit []ArtistName `json:"artist-credit"`
Releases []Release `json:"releases"`
}
// SearchArtists searches for artists by their name
func SearchArtists(name string, limit int) ([]Artist, error) {
params := url.Values{}
params.Set("query", name)
params.Set("limit", strconv.Itoa(limit))
params.Set("fmt", "json")
url := fmt.Sprintf("%sartist/?%s", MusicBrainzAPIEndpoint, params.Encode())
response, err := http.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
var result struct {
Artists []Artist `json:"artists"`
}
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}
return result.Artists, nil
}
// GetArtistByID retrieves an artist by their ID
func GetArtistByID(id string) (*Artist, error) {
url := fmt.Sprintf("%sartist/%s?fmt=json", MusicBrainzAPIEndpoint, id)
response, err := http.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
var artist Artist
err = json.Unmarshal(body, &artist)
if err != nil {
return nil, err
}
return &artist, nil
}
// SearchReleases searches for releases by their title
func SearchReleases(title string, limit int) ([]Release, error) {
params := url.Values{}
params.Set("query", title)
params.Set("limit", strconv.Itoa(limit))
params.Set("fmt", "json")
url := fmt.Sprintf("%srelease/?%s", MusicBrainzAPIEndpoint, params.Encode())
response, err := http.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
var result struct {
Releases []Release `json:"releases"`
}
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}
return result.Releases, nil
}
// GetReleaseByID retrieves a release by its ID
func GetReleaseByID(id string) (*Release, error) {
url := fmt.Sprintf("%srelease/%s?fmt=json", MusicBrainzAPIEndpoint, id)
response, err := http.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
var release Release
err = json.Unmarshal(body, &release)
if err != nil {
return nil, err
}
return &release, nil
}
// SearchRecordings searches for recordings by their title
func SearchRecordings(title string, limit int) ([]Recording, error) {
params := url.Values{}
params.Set("query", title)
params.Set("limit", strconv.Itoa(limit))
params.Set("fmt", "json")
url := fmt.Sprintf("%srecording/?%s", MusicBrainzAPIEndpoint, params.Encode())
response, err := http.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
var result struct {
Recordings []Recording `json:"recordings"`
}
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}
return result.Recordings, nil
}
// GetRecordingByID retrieves a recording by its ID
func GetRecordingByID(id string) (*Recording, error) {
url := fmt.Sprintf("%srecording/%s?fmt=json", MusicBrainzAPIEndpoint, id)
response, err := http.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
var recording Recording
err = json.Unmarshal(body, &recording)
if err != nil {
return nil, err
}
return &recording, nil
}
// searchRecordings searches for recordings by song title and artist name
func SearchRecordingsByTitleAndArtist(title, artist string) ([]Recording, error) {
query := url.QueryEscape(fmt.Sprintf("recording:%s artist:%s", title, artist))
url := fmt.Sprintf("%srecording/?query=%s&limit=20&fmt=json", MusicBrainzAPIEndpoint, query)
response, err := http.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
var result struct {
Recordings []Recording `json:"recordings"`
}
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}
return result.Recordings, nil
}
func GetTagsByTitleAndArtistAndAlbum(title, artist string, album string) ([]Tag, string, error) {
query := url.QueryEscape(fmt.Sprintf("recording:%s artist:%s release:%s", title, artist, album))
url := fmt.Sprintf("%srecording/?query=%s&limit=1&fmt=json", MusicBrainzAPIEndpoint, query)
response, err := http.Get(url)
if err != nil {
return nil, "", err
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, "", err
}
var result struct {
Recordings []Recording `json:"recordings"`
}
err = json.Unmarshal(body, &result)
if err != nil {
return nil, "", err
}
if len(result.Recordings) == 1 {
recording, err := GetRecordingByIDWithTags(result.Recordings[0].ID)
if err != nil {
return nil, "", err
}
return recording.Tags, recording.ReleaseDate, nil
} else {
err = errors.New("MusicBrainz didn't find the song")
}
return nil, "", err
}
func GetRecordingByIDWithTags(id string) (*Recording, error) {
url := fmt.Sprintf("%srecording/%s?fmt=json", MusicBrainzAPIEndpoint, id)
response, err := http.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
var recording Recording
err = json.Unmarshal(body, &recording)
if err != nil {
return nil, err
}
return &recording, nil
}