This repository was archived by the owner on Dec 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcredits.go
307 lines (280 loc) · 8.13 KB
/
credits.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
// Copyright (C) 2020 The Takeout Authors.
//
// This file is part of Takeout.
//
// Takeout is free software: you can redistribute it and/or modify it under the
// terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// Takeout is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
// more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with Takeout. If not, see <https://www.gnu.org/licenses/>.
package music
import (
"fmt"
"strings"
"github.com/defsub/takeout/lib/log"
"github.com/defsub/takeout/lib/musicbrainz"
"github.com/defsub/takeout/lib/search"
)
const (
FieldArtist = "artist"
FieldAsin = "asin"
FieldDate = "date"
FieldFirstDate = "first_date"
FieldGenre = "genre"
FieldLabel = "label"
FieldLength = "length"
FieldMedia = "media"
FieldMediaTitle = "media_title"
FieldPopularity = "popularity"
FieldRating = "rating"
FieldRelease = "release"
FieldReleaseDate = "release_date"
FieldSeries = "series"
FieldStatus = "status"
FieldTag = "tag"
FieldTitle = "title"
FieldTrack = "track"
FieldType = "type"
FieldBass = "base"
FieldClarinet = "clarinet"
FieldDrums = "drums"
FieldFlute = "flute"
FieldGuitar = "guitar"
FieldPiano = "piano"
FieldSaxophone = "saxophone"
FieldVocals = "vocals"
TypePopular = "popular"
TypeSingle = "single"
TypeCover = "cover"
TypeLive = "live"
)
type trackIndex struct {
// these fields are used to match against existing tracks
DiscNum int
TrackNum int
Title string
Artist string
RID string
// these are the indexed fields to store in the search db
Fields search.FieldMap
}
func (m *Music) creditsIndex(reid string) ([]trackIndex, error) {
rel, err := m.mbz.Release(reid)
if err != nil {
return nil, err
}
fields := make(search.FieldMap)
// general fields
if rel.Disambiguation != "" {
addField(fields, FieldRelease, fmt.Sprintf("%s (%s)", rel.Title, rel.Disambiguation))
} else {
addField(fields, FieldRelease, rel.Title)
}
addField(fields, FieldAsin, rel.Asin)
addField(fields, FieldStatus, rel.Status)
if rel.ReleaseGroup.Rating.Votes > 0 {
addField(fields, FieldRating, rel.ReleaseGroup.Rating.Value)
}
for _, l := range rel.LabelInfo {
addField(fields, FieldLabel, l.Label.Name)
}
// dates
// date: first release date of any release associated with this track
// release_date: date of the release associated with this track
// first_date: first release date of this track
addField(fields, FieldDate, rel.ReleaseGroup.FirstReleaseDate)
addField(fields, FieldReleaseDate, rel.Date)
addField(fields, FieldFirstDate, rel.ReleaseGroup.FirstReleaseDate) // refined later
// genres for artist and release group
for _, a := range rel.ArtistCredit {
if a.Name == VariousArtists {
// this has many genres and tags so don't add
continue
}
// use top 3 genres; could also just use PrimaryGenre()
for i, g := range a.Artist.SortedGenres() {
if i < 3 && g.Count > 0 {
addField(fields, FieldGenre, g.Name)
}
}
for _, t := range a.Artist.Tags {
if t.Count > 0 {
addField(fields, FieldTag, t.Name)
}
}
}
// use top 3 genres
for i, g := range rel.ReleaseGroup.SortedGenres() {
if i < 3 && g.Count > 0 {
addField(fields, FieldGenre, g.Name)
}
}
for _, t := range rel.ReleaseGroup.Tags {
if t.Count > 0 {
addField(fields, FieldTag, t.Name)
}
}
relationCredits(fields, rel.Relations)
var indices []trackIndex
for _, m := range rel.Media {
for _, t := range m.Tracks {
trackFields := search.CloneFields(fields)
addField(trackFields, FieldMedia, m.Position)
if m.Title != "" {
// include media specific title
// Eagles / The Long Run (Legacy)
addField(trackFields, FieldMediaTitle, m.Title)
}
addField(trackFields, FieldTrack, t.Position)
// replace with first release date of this track
if len(t.Recording.FirstReleaseDate) > 0 {
setField(trackFields, FieldFirstDate, t.Recording.FirstReleaseDate)
}
addField(trackFields, FieldTitle, t.Recording.Title)
addField(trackFields, FieldLength, t.Recording.Length/1000)
relationCredits(trackFields, t.Recording.Relations)
for _, a := range t.ArtistCredit {
addField(trackFields, FieldArtist, a.Name)
}
index := trackIndex{
DiscNum: m.Position,
TrackNum: t.Position,
Title: fixName(t.Recording.Title),
Artist: fixName(t.Artist()),
RID: t.Recording.ID,
Fields: trackFields,
}
//fmt.Printf("%d/%d/%s/%s\n", index.DiscNum, index.TrackNum, index.Title, index.RID)
indices = append(indices, index)
}
}
return indices, nil
}
func setField(c search.FieldMap, key string, value interface{}) search.FieldMap {
// first remove
k := strings.Replace(key, " ", "_", -1)
delete(c, k)
// now add
return addField(c, key, value)
}
// TODO refactor to use search.AddField
func addField(c search.FieldMap, key string, value interface{}) search.FieldMap {
key = strings.ToLower(key)
keys := []string{key}
// drums = drums (drum set)
// guitar = lead guitar, slide guitar, rhythm guitar, acoustic
// bass = bass guitar, electric bass guitar8eb5ae9e-ba52-4a8f-8513-822a5ccde819
// vocals = lead vocals, backing vocals
alternates := []string{
FieldBass,
FieldClarinet,
FieldDrums,
FieldFlute,
FieldGuitar,
FieldPiano,
FieldSaxophone,
FieldVocals,
}
for _, alt := range alternates {
if strings.Contains(key, alt) {
keys = append(keys, alt)
// only match one; order matters
break
}
}
for _, k := range keys {
k := strings.Replace(k, " ", "_", -1)
switch value.(type) {
case string:
svalue := value.(string)
svalue = fixName(svalue)
if v, ok := c[k]; ok {
switch v.(type) {
case string:
// string becomes array of 2 strings
c[k] = []string{v.(string), svalue}
case []string:
// array of 3+ strings
s := v.([]string)
s = append(s, svalue)
c[k] = s
default:
panic("bad field types")
}
} else {
// single string
c[k] = svalue
}
default:
// numeric, date, etc.
c[k] = value
}
}
return c
}
func relationCredits(c search.FieldMap, relations []musicbrainz.Relation) search.FieldMap {
for _, r := range relations {
if "performance" == r.Type {
for _, wr := range r.Work.Relations {
switch wr.Type {
case "arranger", "arrangement", "composer",
"lyricist", "orchestrator", "orchestration",
"writer":
addField(c, wr.Type, wr.Artist.Name)
case "based on", "medley", "misc",
"instrument arranger", "named after",
"other version", "revised by",
"revision of", "parts", "premiere",
"publishing", "translator", "vocal arranger":
// ignore these
default:
log.Printf("** ignore performance work relation '%s'\n", wr.Type)
}
}
// check if this song is a cover
if len(r.AttributeIds.Cover) > 0 || hasAttribute(r.Attributes, "cover") {
addField(c, FieldType, TypeCover)
}
// check if this song is performed live
if len(r.AttributeIds.Live) > 0 || hasAttribute(r.Attributes, "live") {
addField(c, FieldType, TypeLive)
}
} else if "instrument" == r.Type {
for _, a := range r.Attributes {
addField(c, a, r.Artist.Name)
}
} else if "part of" == r.Type && "series" == r.TargetType {
addField(c, FieldSeries, r.Series.Name)
} else {
if len(r.Attributes) > 0 {
attr := r.Attributes[0]
switch attr {
case "co":
addField(c, fmt.Sprintf("%s-%s", r.Attributes[0], r.Type), r.Artist.Name)
case "additional", "assistant":
addField(c, fmt.Sprintf("%s %s", r.Attributes[0], r.Type), r.Artist.Name)
case "lead vocals":
addField(c, attr, r.Artist.Name)
}
} else {
addField(c, r.Type, r.Artist.Name)
}
}
}
return c
}
func hasAttribute(attrs []string, name string) bool {
for _, a := range attrs {
if a == name {
return true
}
}
return false
}