-
Notifications
You must be signed in to change notification settings - Fork 24
/
release.go
197 lines (171 loc) · 7.36 KB
/
release.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
/*
* Copyright (c) 2014 Michael Wendland
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Authors:
* Michael Wendland <[email protected]>
*/
package gomusicbrainz
import "encoding/xml"
// Release represents a unique release (i.e. issuing) of a product on a
// specific date with specific release information such as the country, label,
// barcode, packaging, etc. More information at https://musicbrainz.org/doc/Release
type Release struct {
ID MBID `xml:"id,attr"`
Title string `xml:"title"`
Status string `xml:"status"`
Disambiguation string `xml:"disambiguation"`
TextRepresentation TextRepresentation `xml:"text-representation"`
ArtistCredit ArtistCredit `xml:"artist-credit"`
ReleaseGroup ReleaseGroup `xml:"release-group"`
Date BrainzTime `xml:"date"`
CountryCode string `xml:"country"`
Barcode string `xml:"barcode"`
Asin string `xml:"asin"`
Quality string `xml:"quality"`
LabelInfos []LabelInfo `xml:"label-info-list>label-info"`
Mediums []*Medium `xml:"medium-list>medium"`
Relations TargetRelationsMap `xml:"relation-list"`
}
func (mbe *Release) lookupResult() interface{} {
var res struct {
XMLName xml.Name `xml:"metadata"`
Ptr *Release `xml:"release"`
}
res.Ptr = mbe
return &res
}
func (mbe *Release) apiEndpoint() string {
return "/release"
}
func (mbe *Release) Id() MBID {
return mbe.ID
}
// LookupRelease performs a release lookup request for the given MBID.
func (c *WS2Client) LookupRelease(id MBID, inc ...string) (*Release, error) {
a := &Release{ID: id}
err := c.Lookup(a, inc...)
return a, err
}
// SearchRelease queries MusicBrainz´ Search Server for Releases.
//
// Possible search fields to provide in searchTerm are:
//
// arid artist id
// artist complete artist name(s) as it appears on the release
// artistname an artist on the release, each artist added as a separate field
// asin the Amazon ASIN for this release
// barcode The barcode of this release
// catno The catalog number for this release, can have multiples when major using an imprint
// comment Disambiguation comment
// country The two letter country code for the release country
// creditname name credit on the release, each artist added as a separate field
// date The release date (format: YYYY-MM-DD)
// discids total number of cd ids over all mediums for the release
// discidsmedium number of cd ids for the release on a medium in the release
// format release format
// laid The label id for this release, a release can have multiples when major using an imprint
// label The name of the label for this release, can have multiples when major using an imprint
// lang The language for this release. Use the three character ISO 639 codes to search for a specific language. (e.g. lang:eng)
// mediums number of mediums in the release
// primarytype primary type of the release group (album, single, ep, other)
// puid The release contains recordings with these puids
// quality The quality of the release (low, normal, high)
// reid release id
// release release name
// releaseaccent name of the release with any accent characters retained
// rgid release group id
// script The 4 character script code (e.g. latn) used for this release
// secondarytype secondary type of the release group (audiobook, compilation, interview, live, remix, soundtrack, spokenword)
// status release status (e.g official)
// tag a tag that appears on the release
// tracks total number of tracks over all mediums on the release
// tracksmedium number of tracks on a medium in the release
// type type of the release group, old type mapping for when we did not have separate primary and secondary types
//
// With no fields specified searchTerm searches the release field only. For
// more information visit
// https://musicbrainz.org/doc/Development/XML_Web_Service/Version_2/Search#Release
func (c *WS2Client) SearchRelease(searchTerm string, limit, offset int) (*ReleaseSearchResponse, error) {
result := releaseListResult{}
err := c.searchRequest("/release", &result, searchTerm, limit, offset)
rsp := ReleaseSearchResponse{}
rsp.WS2ListResponse = result.ReleaseList.WS2ListResponse
rsp.Scores = make(ScoreMap)
for i, v := range result.ReleaseList.Releases {
rsp.Releases = append(rsp.Releases, v.Release)
rsp.Scores[rsp.Releases[i]] = v.Score
}
return &rsp, err
}
// ReleaseSearchResponse is the response type returned by the SearchRelease method.
type ReleaseSearchResponse struct {
WS2ListResponse
Releases []*Release
Scores ScoreMap
}
// ResultsWithScore returns a slice of Releases with a min score.
func (r *ReleaseSearchResponse) ResultsWithScore(score int) []*Release {
var res []*Release
for _, v := range r.Releases {
if r.Scores[v] >= score {
res = append(res, v)
}
}
return res
}
// OriginalRelease is a helper function that returns the earliest release of
// a release array with the most accurate date. It can be used to determine
// the original/first release from releases of a release group.
func OriginalRelease(releases []*Release) *Release {
if len(releases) == 0 {
return nil
}
original := releases[0] // fall back on the first item
for _, release := range releases {
if !release.Date.IsZero() {
if release.Date.Year() < original.Date.Year() || original.Date.IsZero() {
original = release
} else if release.Date.Year() == original.Date.Year() &&
release.Date.Accuracy > Year {
if original.Date.Accuracy == Year ||
release.Date.Month() < original.Date.Month() {
original = release
} else if release.Date.Month() == original.Date.Month() &&
release.Date.Accuracy > Month {
if original.Date.Accuracy == Month ||
release.Date.Day() < original.Date.Day() {
original = release
}
}
}
}
}
return original
}
type releaseListResult struct {
ReleaseList struct {
WS2ListResponse
Releases []struct {
*Release
Score int `xml:"http://musicbrainz.org/ns/ext#-2.0 score,attr"`
} `xml:"release"`
} `xml:"release-list"`
}