forked from philipmat/discogs-xml2db
-
Notifications
You must be signed in to change notification settings - Fork 1
/
discogsreleaseparser.py
330 lines (283 loc) · 10.4 KB
/
discogsreleaseparser.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# 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 St, Fifth Floor, Boston, MA 02110-1301 USA
import xml.sax.handler
import xml.sax
import sys
import model
import re
releaseCounter = 0
class ReleaseHandler(xml.sax.handler.ContentHandler):
def __init__(self, exporter, stop_after=0, ignore_missing_tags=False):
self.knownTags = (
'id',
'identifiers',
'identifier',
'videos',
'video',
'companies',
'company',
'catno',
'entity_type',
'entity_type_name',
'resource_url',
'anv',
'artist',
'artists',
'country',
'data_quality',
'description',
'descriptions',
'duration',
'extraartists',
'format',
'formats',
'genre',
'genres',
#'indentifiers', 'identifier',
'image',
'images',
'join',
'label',
'labels',
'master_id',
'name',
'notes',
'position',
'release',
'released',
'releases',
'role',
'style',
'styles',
'sub_tracks',
'title',
'track',
'tracklist',
'tracks',
'url',
'urls',
)
self.release = None
self.buffer = ''
self.unknown_tags = []
self.exporter = exporter
self.stop_after = stop_after
self.ignore_missing_tags = ignore_missing_tags
self.stack = []
def startElement(self, name, attrs):
if not name in self.knownTags:
if not self.ignore_missing_tags:
print "Error: Unknown Release element '%s'." % name
sys.exit()
elif not name in self.unknown_tags:
self.unknown_tags.append(name)
self.stack.append(name)
if name == 'release':
self.release = model.Release()
self.release.id = int(attrs['id'])
self.release.status = attrs['status']
elif name == 'track' and self.stack[-2] == 'tracklist':
self.release.tracklist.append(model.Track())
elif name == "image":
img = model.ImageInfo()
img.height = attrs["height"]
img.imageType = attrs["type"]
img.uri = attrs["uri"]
img.uri150 = attrs["uri150"]
img.width = attrs["width"]
self.release.images.append(img)
if len(attrs) != 5:
print "ATTR ERROR"
print attrs
sys.exit()
elif name == 'format':
fmt = model.Format()
fmt.name = attrs['name']
fmt.qty = attrs['qty']
self.release.formats.append(fmt)
elif name == 'label':
lbl = model.ReleaseLabel()
lbl.name = attrs['name']
lbl.catno = attrs['catno']
self.release.labels.append(lbl)
# Barcode
elif name == 'identifier' and attrs['type'] == 'Barcode':
self.release.barcode = attrs['value']
def characters(self, data):
self.buffer += data
def endElement(self, name):
self.buffer = self.buffer.strip()
# Track title
if name == 'title' and self.stack[-2] == 'track' and 'sub_track' not in self.stack:
if len(self.buffer) != 0:
self.release.tracklist[-1].title = self.buffer
# Release title
if name == 'title' and self.stack[-2] == 'release':
if len(self.buffer) != 0:
self.release.title = self.buffer
# Release Country
elif name == 'country':
if len(self.buffer) != 0:
self.release.country = self.buffer
# Release Date
elif name == 'released':
if len(self.buffer) != 0:
self.release.released = self.buffer
# Release Notes
elif name == 'notes':
if len(self.buffer) != 0:
self.release.notes = self.buffer
# Release Genre
elif name == 'genre':
if len(self.buffer) != 0:
self.release.genres.append(self.buffer)
# Release Style
elif name == 'style':
if len(self.buffer) != 0:
self.release.styles.append(self.buffer)
# Release Format Description
elif name == 'description' and 'formats' in self.stack:
if len(self.buffer) != 0:
self.release.formats[-1].descriptions.append(self.buffer)
# Release Quality
elif name == 'data_quality':
if len(self.buffer) != 0:
self.release.data_quality = self.buffer
# Track extra artist id
elif name == 'id' and 'artist' in self.stack and 'track' in self.stack and 'sub_track' not in self.stack and 'extraartists' in self.stack:
if len(self.buffer) != 0:
teaj = model.Extraartist()
teaj.artist_id = self.buffer
self.release.tracklist[-1].extraartists.append(teaj)
# Release extra artist id
elif name == 'id' and 'artist' in self.stack and 'track' not in self.stack and 'extraartists' in self.stack:
if len(self.buffer) != 0:
eaj = model.Extraartist()
eaj.artist_id = self.buffer
self.release.extraartists.append(eaj)
# Track artist id
elif name == 'id' and 'artist' in self.stack and 'track' in self.stack and 'sub_track' not in self.stack and 'extraartists' not in self.stack:
if len(self.buffer) != 0:
taj = model.ArtistJoin()
taj.artist_id = self.buffer
self.release.tracklist[-1].artistJoins.append(taj)
# Release artist id
elif name == 'id' and 'artist' in self.stack and 'track' not in self.stack and 'extraartists' not in self.stack:
if len(self.buffer) != 0:
aj = model.ArtistJoin()
aj.artist_id = self.buffer
self.release.artistJoins.append(aj)
# Track extra artist name
elif name == 'name' and 'artist' in self.stack and 'track' in self.stack and 'sub_track' not in self.stack and 'extraartists' in self.stack:
if len(self.buffer) != 0:
self.release.tracklist[-1].extraartists[-1].artist_name = self.buffer
# Release extra artist name
elif name == 'name' and 'artist' in self.stack and 'track' not in self.stack and 'extraartists' in self.stack:
if len(self.buffer) != 0:
self.release.extraartists[-1].artist_name = self.buffer
# Track artist name
elif name == 'name' and 'artist' in self.stack and 'track' in self.stack and 'sub_track' not in self.stack and 'extraartists' not in self.stack:
if len(self.buffer) != 0:
self.release.tracklist[-1].artistJoins[-1].artist_name = self.buffer
# Release artist name
elif name == 'name' and 'artist' in self.stack and 'track' not in self.stack and 'extraartists' not in self.stack:
if len(self.buffer) != 0:
self.release.artistJoins[-1].artist_name = self.buffer
# Track artist anv
elif name == 'anv' and 'artist' in self.stack and 'track' in self.stack and 'sub_track' not in self.stack and 'extraartists' not in self.stack:
if len(self.buffer) != 0:
self.release.tracklist[-1].artistJoins[-1].anv = self.buffer
# Track extra artist anv
elif name == 'anv' and 'artist' in self.stack and 'track' in self.stack and 'extraartists' in self.stack:
if len(self.buffer) != 0:
self.release.tracklist[-1].extraartists[-1].anv = self.buffer
# Release artist anv
elif name == 'anv' and 'artist' in self.stack and 'track' not in self.stack and 'extraartists' not in self.stack:
if len(self.buffer) != 0:
self.release.artistJoins[-1].anv = self.buffer
# Release extra artist anv
elif name == 'anv' and 'artist' in self.stack and 'track' not in self.stack and 'extraartists' in self.stack:
if len(self.buffer) != 0:
self.release.extraartists[-1].anv = self.buffer
# Track artist join
elif name == 'join' and 'artist' in self.stack and 'track' in self.stack and 'sub_track' not in self.stack:
if len(self.buffer) != 0:
self.release.tracklist[-1].artistJoins[-1].join_relation = self.buffer
# Release artist join
elif name == 'join' and 'artist' in self.stack and 'track' not in self.stack:
if len(self.buffer) != 0:
self.release.artistJoins[-1].join_relation = self.buffer
# Track extra artist role
elif name == 'role' and 'artist' in self.stack and 'track' in self.stack and 'sub_track' not in self.stack and 'extraartists' in self.stack:
if len(self.buffer) != 0:
roles_list = re.findall('([^[,]+(?:\[[^]]+])?)+', self.buffer) # thanks to jlatour
for role in roles_list:
role = role.strip()
lIndex = role.find('[')
if lIndex != -1:
rIndex = role.find(']')
description = role[lIndex + 1: rIndex]
role = (role[:lIndex].strip(), description)
self.release.tracklist[-1].extraartists[-1].roles.append(role)
# Release extra artist role
elif name == 'role' and 'artist' in self.stack and 'track' not in self.stack and 'extraartists' in self.stack:
if len(self.buffer) != 0:
roles_list = re.findall('([^[,]+(?:\[[^]]+])?)+', self.buffer) # thanks to jlatour
for role in roles_list:
role = role.strip()
lIndex = role.find('[')
if lIndex != -1:
rIndex = role.find(']')
description = role[lIndex + 1: rIndex]
role = (role[:lIndex].strip(), description)
self.release.extraartists[-1].roles.append(role)
# Track Duration
elif name == 'duration' and 'sub_track' not in self.stack:
self.release.tracklist[-1].duration = self.buffer
# Track Position
elif name == 'position' and 'sub_track' not in self.stack:
self.release.tracklist[-1].position = self.buffer
# Release Master
elif name == 'master_id':
self.release.master_id = int(self.buffer)
# End of Release
elif name == 'release':
# end of tag
len_a = len(self.release.artistJoins)
if len_a == 0:
sys.stderr.writelines("Ignoring Release %s with no artist. Dictionary: %s\n" % (self.release.id, self.release.__dict__))
else:
global releaseCounter
releaseCounter += 1
self.exporter.storeRelease(self.release)
releaseCounter += 1
if self.stop_after > 0 and releaseCounter >= self.stop_after:
self.endDocument()
if self.ignore_missing_tags and len(self.unknown_tags) > 0:
print 'Encountered some unknown Release tags: %s' % (self.unknown_tags)
raise model.ParserStopError(releaseCounter)
if self.stack[-1] == name:
self.stack.pop()
self.buffer = ''
def endDocument(self):
#print [genre for genre in genres]
#print [style for style in styles]
#print [format for format in formats]
#print [dsc for dsc in descriptions]
#print [j for j in joins]
#print [(role, roles[role]) for role in roles]
#print len(roles)
self.exporter.finish()