forked from metabrainz/picard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mbxml.py
408 lines (366 loc) · 15.3 KB
/
mbxml.py
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# -*- coding: utf-8 -*-
#
# Picard, the next-generation MusicBrainz tagger
# Copyright (C) 2007 Lukáš Lalinský
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import re
from picard import config
from picard.util import format_time, translate_from_sortname, parse_amazon_url
from picard.const import RELEASE_FORMATS
_artist_rel_types = {
"composer": "composer",
"writer": "writer",
"conductor": "conductor",
"chorus master": "conductor",
"performing orchestra": "performer:orchestra",
"arranger": "arranger",
"orchestrator": "arranger",
"instrumentator": "arranger",
"lyricist": "lyricist",
"librettist": "lyricist",
"remixer": "remixer",
"producer": "producer",
"engineer": "engineer",
"audio": "engineer",
#"Mastering": "engineer",
"sound": "engineer",
"live sound": "engineer",
"mix": "mixer",
#"Recording": "engineer",
"mix-DJ": "djmixer",
}
def _decamelcase(text):
return re.sub(r'([A-Z])', r' \1', text).strip()
_REPLACE_MAP = {}
_EXTRA_ATTRS = ['guest', 'additional', 'minor']
_BLANK_SPECIAL_RELTYPES = {'vocal': 'vocals'}
def _parse_attributes(attrs, reltype):
attrs = [_decamelcase(_REPLACE_MAP.get(a, a)) for a in attrs]
prefix = ' '.join([a for a in attrs if a in _EXTRA_ATTRS])
attrs = [a for a in attrs if a not in _EXTRA_ATTRS]
if len(attrs) > 1:
attrs = '%s and %s' % (', '.join(attrs[:-1]), attrs[-1:][0])
elif len(attrs) == 1:
attrs = attrs[0]
else:
attrs = _BLANK_SPECIAL_RELTYPES.get(reltype, '')
return ' '.join([prefix, attrs]).strip().lower()
def _relations_to_metadata(relation_lists, m):
for relation_list in relation_lists:
if relation_list.target_type == 'artist':
for relation in relation_list.relation:
artist = relation.artist[0]
value, valuesort = _translate_artist_node(artist)
reltype = relation.type
attribs = []
if 'attribute_list' in relation.children:
attribs = [a.text for a in relation.attribute_list[0].attribute]
if reltype in ('vocal', 'instrument', 'performer'):
name = 'performer:' + _parse_attributes(attribs, reltype)
elif reltype == 'mix-DJ' and len(attribs) > 0:
if not hasattr(m, "_djmix_ars"):
m._djmix_ars = {}
for attr in attribs:
m._djmix_ars.setdefault(attr.split()[1], []).append(value)
continue
else:
try:
name = _artist_rel_types[reltype]
except KeyError:
continue
if value not in m[name]:
m.add(name, value)
if name == 'composer' and valuesort not in m['composersort']:
m.add('composersort', valuesort)
elif relation_list.target_type == 'work':
for relation in relation_list.relation:
if relation.type == 'performance':
performance_to_metadata(relation, m)
work_to_metadata(relation.work[0], m)
elif relation_list.target_type == 'url':
for relation in relation_list.relation:
if relation.type == 'amazon asin' and 'asin' not in m:
amz = parse_amazon_url(relation.target[0].text)
if amz is not None:
m['asin'] = amz['asin']
elif relation.type == 'license':
url = relation.target[0].text
m.add('license', url)
def _translate_artist_node(node):
transl, translsort = None, None
if config.setting['translate_artist_names']:
locale = config.setting["artist_locale"]
lang = locale.split("_")[0]
if "alias_list" in node.children:
found_primary = found_locale = False
for alias in node.alias_list[0].alias:
if alias.attribs.get("type") != "Search hint" and "locale" in alias.attribs:
if alias.locale == locale:
transl, translsort = alias.text, alias.attribs["sort_name"]
if alias.attribs.get("primary") == "primary":
return (transl, translsort)
found_locale = True
elif alias.locale == lang and not (found_locale or found_primary):
transl, translsort = alias.text, alias.attribs["sort_name"]
if alias.attribs.get("primary") == "primary":
found_primary = True
if not transl:
translsort = node.sort_name[0].text
transl = translate_from_sortname(node.name[0].text, translsort)
else:
transl, translsort = node.name[0].text, node.sort_name[0].text
return (transl, translsort)
def artist_credit_from_node(node):
artist = ""
artistsort = ""
artists = []
artistssort = []
standardize_artists = config.setting["standardize_artists"]
for credit in node.name_credit:
a = credit.artist[0]
translated, translated_sort = _translate_artist_node(a)
if translated != a.name[0].text:
name = translated
elif 'name' in credit.children and not standardize_artists:
name = credit.name[0].text
else:
name = a.name[0].text
artist += name
artistsort += translated_sort
artists.append(name)
artistssort.append(translated_sort)
if 'joinphrase' in credit.attribs:
artist += credit.joinphrase
artistsort += credit.joinphrase
return (artist, artistsort, artists, artistssort)
def artist_credit_to_metadata(node, m, release=False):
ids = [n.artist[0].id for n in node.name_credit]
artist, artistsort, artists, artistssort = artist_credit_from_node(node)
if release:
m["musicbrainz_albumartistid"] = ids
m["albumartist"] = artist
m["albumartistsort"] = artistsort
m["~albumartists"] = artists
m["~albumartists_sort"] = artistssort
else:
m["musicbrainz_artistid"] = ids
m["artist"] = artist
m["artistsort"] = artistsort
m["artists"] = artists
m["~artists_sort"] = artistsort
def label_info_from_node(node):
labels = []
catalog_numbers = []
if node.count != "0":
for label_info in node.label_info:
if 'label' in label_info.children:
label = label_info.label[0].name[0].text
if label not in labels:
labels.append(label)
if 'catalog_number' in label_info.children:
cat_num = label_info.catalog_number[0].text
if cat_num not in catalog_numbers:
catalog_numbers.append(cat_num)
return (labels, catalog_numbers)
def media_formats_from_node(node):
formats_count = {}
formats_order = []
for medium in node.medium:
if "format" in medium.children:
text = medium.format[0].text
else:
text = "(unknown)"
if text in formats_count:
formats_count[text] += 1
else:
formats_count[text] = 1
formats_order.append(text)
formats = []
for format in formats_order:
count = formats_count[format]
format = RELEASE_FORMATS.get(format, format)
if count > 1:
format = str(count) + u"×" + format
formats.append(format)
return " + ".join(formats)
def track_to_metadata(node, track):
m = track.metadata
recording_to_metadata(node.recording[0], track)
m.add_unique('musicbrainz_trackid', node.id)
# overwrite with data we have on the track
for name, nodes in node.children.iteritems():
if not nodes:
continue
if name == 'title':
m['title'] = nodes[0].text
elif name == 'position':
m['tracknumber'] = nodes[0].text
elif name == 'number':
m['~musicbrainz_tracknumber'] = nodes[0].text
elif name == 'length' and nodes[0].text:
m.length = int(nodes[0].text)
elif name == 'artist_credit':
artist_credit_to_metadata(nodes[0], m)
m['~length'] = format_time(m.length)
def recording_to_metadata(node, track):
m = track.metadata
m.length = 0
m.add_unique('musicbrainz_recordingid', node.id)
for name, nodes in node.children.iteritems():
if not nodes:
continue
if name == 'title':
m['title'] = nodes[0].text
m['~recordingtitle'] = nodes[0].text
elif name == 'length' and nodes[0].text:
m.length = int(nodes[0].text)
elif name == 'disambiguation':
m['~recordingcomment'] = nodes[0].text
elif name == 'artist_credit':
artist_credit_to_metadata(nodes[0], m)
elif name == 'relation_list':
_relations_to_metadata(nodes, m)
elif name == 'tag_list':
add_folksonomy_tags(nodes[0], track)
elif name == 'user_tag_list':
add_user_folksonomy_tags(nodes[0], track)
elif name == 'isrc_list':
add_isrcs_to_metadata(nodes[0], m)
elif name == 'user_rating':
m['~rating'] = nodes[0].text
m['~length'] = format_time(m.length)
def performance_to_metadata(relation, m):
if 'attribute_list' in relation.children:
if 'attribute' in relation.attribute_list[0].children:
atts = []
for attribute in relation.attribute_list[0].attribute:
m["~performance_%s" % attribute.text] = "1"
atts.append(attribute.text)
# sequence order is defined as:
# {live} {medley:medley including a} {partial} {instrumental} {cover} recording of
# see http://musicbrainz.org/relationship/a3005666-a872-32c3-ad06-98af558e99b0
workdesc = ''
if 'live' in atts:
workdesc += _('live') + ' '
if 'medley' in atts:
if 'partial' in atts \
or 'instrumental' in atts \
or 'cover' in atts:
workdesc += _('medley including a') + ' '
else:
workdesc += _('medley') + ' '
if 'partial' in atts:
workdesc += _('partial') + ' '
if 'instrumental' in atts:
workdesc += _('instrumental') + ' '
if 'cover' in atts:
workdesc += _('cover') + ' '
m["~workrelationship"] = workdesc[:1].upper() + workdesc[1:] + _('recording of')
def work_to_metadata(work, m):
m.add_unique("musicbrainz_workid", work.id)
if 'language' in work.children:
m.add_unique("language", work.language[0].text)
if 'title' in work.children:
m.add("work", work.title[0].text)
if 'relation_list' in work.children:
_relations_to_metadata(work.relation_list, m)
def medium_to_metadata(node, m):
for name, nodes in node.children.iteritems():
if not nodes:
continue
if name == 'position':
m['discnumber'] = nodes[0].text
elif name == 'track_list':
m['totaltracks'] = nodes[0].count
elif name == 'title':
m['discsubtitle'] = nodes[0].text
elif name == 'format':
m['media'] = nodes[0].text
def release_to_metadata(node, m, album=None):
"""Make metadata dict from a XML 'release' node."""
m.add_unique('musicbrainz_albumid', node.id)
for name, nodes in node.children.iteritems():
if not nodes:
continue
if name == 'status':
m['releasestatus'] = nodes[0].text.lower()
elif name == 'title':
m['album'] = nodes[0].text
elif name == 'disambiguation':
m['~releasecomment'] = nodes[0].text
elif name == 'asin':
m['asin'] = nodes[0].text
elif name == 'artist_credit':
artist_credit_to_metadata(nodes[0], m, release=True)
elif name == 'date':
m['date'] = nodes[0].text
elif name == 'country':
m['releasecountry'] = nodes[0].text
elif name == 'barcode':
m['barcode'] = nodes[0].text
elif name == 'relation_list':
_relations_to_metadata(nodes, m)
elif name == 'label_info_list' and nodes[0].count != '0':
m['label'], m['catalognumber'] = label_info_from_node(nodes[0])
elif name == 'text_representation':
if 'language' in nodes[0].children:
m['~releaselanguage'] = nodes[0].language[0].text
if 'script' in nodes[0].children:
m['script'] = nodes[0].script[0].text
elif name == 'tag_list':
add_folksonomy_tags(nodes[0], album)
elif name == 'user_tag_list':
add_user_folksonomy_tags(nodes[0], album)
def release_group_to_metadata(node, m, release_group=None):
"""Make metadata dict from a XML 'release-group' node taken from inside a 'release' node."""
m.add_unique('musicbrainz_releasegroupid', node.id)
for name, nodes in node.children.iteritems():
if not nodes:
continue
if name == 'title':
m['~releasegroup'] = nodes[0].text
elif name == 'disambiguation':
m['~releasegroupcomment'] = nodes[0].text
elif name == 'first_release_date':
m['originaldate'] = nodes[0].text
elif name == 'tag_list':
add_folksonomy_tags(nodes[0], release_group)
elif name == 'user_tag_list':
add_user_folksonomy_tags(nodes[0], release_group)
elif name == 'primary_type':
m['~primaryreleasetype'] = nodes[0].text.lower()
elif name == 'secondary_type_list':
add_secondary_release_types(nodes[0], m)
m['releasetype'] = m.getall('~primaryreleasetype') + m.getall('~secondaryreleasetype')
def add_secondary_release_types(node, m):
if 'secondary_type' in node.children:
for secondary_type in node.secondary_type:
m.add_unique('~secondaryreleasetype', secondary_type.text.lower())
def add_folksonomy_tags(node, obj):
if obj and 'tag' in node.children:
for tag in node.tag:
name = tag.name[0].text
count = int(tag.attribs['count'])
obj.add_folksonomy_tag(name, count)
def add_user_folksonomy_tags(node, obj):
if obj and 'user_tag' in node.children:
for tag in node.user_tag:
name = tag.name[0].text
obj.add_folksonomy_tag(name, 1)
def add_isrcs_to_metadata(node, metadata):
if 'isrc' in node.children:
for isrc in node.isrc:
metadata.add('isrc', isrc.id)