/
isrcsubmit.py
executable file
·917 lines (822 loc) · 34.7 KB
/
isrcsubmit.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
#!/usr/bin/env python2
# Copyright (C) 2010-2012 Johannes Dewender
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
"""This is a tool to submit ISRCs from a disc to MusicBrainz.
Icedax is used to gather the ISRCs and python-musicbrainz2 to submit them.
The project is hosted on
https://github.com/JonnyJD/musicbrainz-isrcsubmit
and the script is als available on
http://kraehen.org/isrcsubmit.py
"""
isrcsubmitVersion = "0.5"
agentName = "isrcsubmit-jonnyjd-" + isrcsubmitVersion
# starting with highest priority
backends = ["mediatools", "media_info", "cdrdao", "cd-info",
"cdda2wav", "icedax", "drutil"]
packages = {"cd-info": "libcdio", "cdda2wav": "cdrtools", "icedax": "cdrkit"}
import os
import re
import sys
import getpass
import tempfile
from datetime import datetime
from optparse import OptionParser
from subprocess import Popen, PIPE, call
from distutils.version import StrictVersion
from musicbrainz2 import __version__ as musicbrainz2_version
from musicbrainz2.disc import readDisc, DiscError, getSubmissionUrl
from musicbrainz2.model import Track
from musicbrainz2.webservice import WebService, Query
from musicbrainz2.webservice import ReleaseFilter, ReleaseIncludes
from musicbrainz2.webservice import RequestError, AuthenticationError
from musicbrainz2.webservice import ConnectionError, WebServiceError
# using a shellscript to get the correct python version (2.5 - 2.7)
shellname = "isrcsubmit.sh"
if os.path.isfile(shellname):
scriptname = shellname
else:
scriptname = os.path.basename(sys.argv[0])
def scriptVersion(option=None, opt=None, value=None, parser=None):
return "isrcsubmit %s by JonnyJD for MusicBrainz" % isrcsubmitVersion
def printHelp(option=None, opt=None, value=None, parser=None):
print \
"""
This python script extracts ISRCs from audio cds and submits them to MusicBrainz (musicbrainz.org).
You need to have a MusicBrainz account, specify the username and will be asked for your password every time you execute the script.
Isrcsubmit will warn you if there are any problems and won't actually submit anything to MusicBrainz without giving a final choice.
Isrcsubmit will warn you if any duplicate ISRCs are detected and help you fix priviously inserted duplicate ISRCs.
The ISRC-track relationship we found on our disc is taken as our correct evaluation.
A note on Multi-disc-releases:
Isrcsubmit uses the MusicBrainz web service version 1.
This api is not tailored for MusicBrainz NGS (Next Generation Schema) and expects to have one release per disc. So it does not know which tracks are on a specific disc and lists all tracks in the overall release.
In order to attach the ISRCs to the correct tracks an offset is necessary for multi-disc-releases. For the first disc and last disc this can be guessed easily. Starting with 3 discs irscsubmit will ask you for the offset of the "middle discs".
The offset is the sum of track counts on all previous discs.
Example:
disc 1: (13 tracks)
disc 2: (17 tracks)
disc 3: 19 tracks (current disc)
disc 4: (23 tracks)
number of tracks altogether: 72
The offset we have to use is 30 (= 13 + 17)
Isrcsubmit only knows how many tracks the current disc has and the total number of tracks on the release given by the web service. So the offset must be between 0 and 53 (= 72 - 19), which is the range isrcsubmit lets you choose from.
The number of discs in the release and the position of this disc give by isrcsubmit is not necessarily correct. There can be multiple disc IDs per actual disc. You should only count tracks on your actual discs.
Isrcsubmit can give you a link for an overview of the disc IDs for your release.
"""
parser.print_usage()
print """\
Please report bugs on https://github.com/JonnyJD/musicbrainz-isrcsubmit"""
sys.exit(0)
class Isrc(object):
def __init__(self, isrc, track=None):
self._id = isrc
self._tracks = []
if track is not None:
self._tracks.append(track)
def addTrack(self, track):
if track not in self._tracks:
self._tracks.append(track)
def getTracks(self):
return self._tracks
def getTrackNumbers(self):
numbers = []
for track in self._tracks:
numbers.append(track.getNumber())
return ", ".join(map(str, numbers))
class EqTrack(Track):
"""track with equality checking
This makes it easy to check if this track is already in a collection.
Only the element already in the collection needs to be hashable.
"""
def __init__(self, track):
self._track = track
def __eq__(self, other):
return self.getId() == other.getId()
def getId(self):
return self._track.getId()
def getArtist(self):
return self._track.getArtist()
def getTitle(self):
return self._track.getTitle()
def getISRCs(self):
return self._track.getISRCs()
class NumberedTrack(EqTrack):
"""A track found on an analyzed (own) disc
"""
def __init__(self, track, number):
self._track = track
self._number = number
def getNumber(self):
"""The track number on the analyzed disc"""
return self._number
class OwnTrack(NumberedTrack):
"""A track found on an analyzed (own) disc
"""
pass
def gatherOptions(argv):
if os.name == "nt":
defaultDevice = "D:"
# cdrdao is not given a device and will try 0,1,0
# this default is only for libdiscid and mediatools
else:
defaultDevice = "/dev/cdrom"
defaultBrowser = "firefox"
prog = scriptname
parser = OptionParser(version=scriptVersion(), add_help_option=False)
parser.set_usage("%s [options] [user] [device]\n %s -h" % (prog, prog))
parser.add_option("-h", action="help",
help="Short usage help")
parser.add_option("--help", action="callback", callback=printHelp,
help="Complete help for the script")
parser.add_option("-u", "--user", metavar="USERNAME",
help="MusicBrainz username, if not given as argument.")
# note that -d previously stand for debug
parser.add_option("-d", "--device", metavar="DEVICE",
help="CD device with a loaded audio cd, if not given as argument."
+ " The default is " + defaultDevice + " (and '1' for mac)")
parser.add_option("-b", "--backend", choices=backends, metavar="PROGRAM",
help="Force using a specifig backend to extract ISRCs from the"
+ " disc. Possible backends are: %s." % ", ".join(backends)
+ " They are tried in this order otherwise." )
parser.add_option("--browser", metavar="BROWSER",
help="Program to open urls. The default is " + defaultBrowser)
parser.add_option("--debug", action="store_true", default=False,
help="Show debug messages."
+ " Currently shows some backend messages.")
(options, args) = parser.parse_args(argv[1:])
# "optional" positional arguments
# only optional when the data is already added as option..
# not sure if that is convenience or makes it impossible to understand..
if options.user is None:
if len(args) > 0:
options.user = args[0]
args = args[1:]
else:
options.user = None
if options.device is None:
if len(args) > 0:
options.device = args[0]
args = args[1:]
else:
# Mac: device is changed again, when we know the final backend
# Win: cdrdao is not given a device and will try 0,1,0
options.device = defaultDevice
if options.browser is None:
options.browser = defaultBrowser
if len(args) > 0:
print "WARNING: Superfluous arguments:", ", ".join(args)
if options.backend and not hasBackend(options.backend, strict=True):
printError("Chosen backend not found. No ISRC extraction possible!")
printError2("Make sure that %s is installed." % options.backend)
sys.exit(-1)
return options
def getProgVersion(prog):
if prog == "icedax":
return Popen([prog, "--version"], stderr=PIPE).communicate()[1].strip()
elif prog == "cdda2wav":
outdata = Popen([prog, "-version"], stdout=PIPE).communicate()[0]
return " ".join(outdata.splitlines()[0].split()[0:2])
elif prog == "cdrdao":
outdata = Popen([prog], stderr=PIPE).communicate()[1]
return " ".join(outdata.splitlines()[0].split()[::2][0:2])
elif prog == "cd-info":
outdata = Popen([prog, "--version"], stdout=PIPE).communicate()[0]
return " ".join(outdata.splitlines()[0].split()[::2][0:2])
elif prog == "drutil":
outdata = Popen([prog, "version"], stdout=PIPE).communicate()[0]
version = prog
for line in outdata.splitlines():
if len(line) > 0: version += " " + line.split(":")[1].strip()
return version
else:
return prog
def hasBackend(backend, strict=False):
devnull = open(os.devnull, "w")
try:
p_which = Popen(["which", backend], stdout=PIPE, stderr=devnull)
except WindowsError:
# windows normally has no "which"
# we just try to start these non-interactive console apps
try:
call([backend], stdout=devnull, stderr=devnull)
except WindowsError:
return False
else:
return True
backend_path = p_which.communicate()[0].strip()
if p_which.returncode == 0:
# check if it is only a symlink to another backend
real_backend = os.path.basename(os.path.realpath(backend_path))
if backend != real_backend and real_backend in backends:
if strict:
print "WARNING: %s is a symlink to %s" % (backend, real_backend)
return True
else:
return False # use real backend instead, or higher priority
return True
else:
return False
def getRealMacDevice(optionDevice):
p = Popen(["drutil", "status", "-drive", optionDevice], stdout=PIPE)
try:
given = p.communicate()[0].splitlines()[3].split("Name:")[1].strip()
except IndexError:
printError("could not find real device")
printError2("maybe no disc in the drive?")
sys.exit(-1)
# libdiscid needs the "raw" version
return given.replace("/disk", "/rdisk")
def askForOffset(discTrackCount, releaseTrackCount):
limit = releaseTrackCount - discTrackCount
while True:
print
print "How many tracks are on the previous (actual) discs altogether?"
try:
choice = raw_input("[0-%d] " % limit)
except KeyboardInterrupt:
print
print "exiting.."
sys.exit(1)
try:
num = int(choice)
except ValueError:
printError("Not a number")
else:
if num in range(0, limit + 1):
return num
def printError(*args):
stringArgs = tuple(map(str, args))
msg = " ".join(("ERROR:",) + stringArgs)
sys.stderr.write(msg + "\n")
def printError2(*args):
stringArgs = tuple(map(str, args))
msg = " ".join((" ",) + stringArgs)
sys.stderr.write(msg + "\n")
def backendError(backend, e):
printError("Couldn't gather ISRCs with %s: %i - %s"
% (backend, e.errno, e.strerror))
sys.exit(1)
class DemandQuery():
"""A Query object that opens an actual query on first use
"""
def __init__(self, username, agent):
self._query = None
self.auth = False
self.username = username
self.agent = agent
def create(self, auth=False):
if auth:
print
if self.username is None:
print "Please input your Musicbrainz username:",
self.username = raw_input()
print "Please input your Musicbrainz password: ",
password = getpass.getpass("")
print
if StrictVersion(musicbrainz2_version) >= "0.7.4":
# There is a warning printed above, when < 0.7.4
service = WebService(username=self.username, password=password,
userAgent=self.agent)
else:
# standard userAgent: python-musicbrainz/__version__
service = WebService(username=self.username, password=password)
self.auth = True
else:
if StrictVersion(musicbrainz2_version) >= "0.7.4":
service = WebService(userAgent=self.agent)
else:
service = WebService()
# This clientId is currently only used for submitPUIDs and submitCDStub
# which we both don't do directly.
self._query = Query(service, clientId=self.agent)
def getReleases(self, filter):
if self._query is None: self.create()
return self._query.getReleases(filter=filter)
def getReleaseById(self, releaseId, include):
if self._query is None: self.create()
return self._query.getReleaseById(releaseId, include=include)
def submitISRCs(self, tracks2isrcs):
if not self.auth: self.create(auth=True)
self._query.submitISRCs(tracks2isrcs)
class Disc(object):
def __init__(self, device, submit=False):
try:
# calculate disc ID from disc
self._disc = readDisc(deviceName=device)
self._release = None
self._submit = submit
except DiscError, e:
printError("DiscID calculation failed:", str(e))
sys.exit(1)
@property
def id(self):
return self._disc.getId()
@property
def trackCount(self):
return len(self._disc.getTracks())
@property
def submissionUrl(self):
return getSubmissionUrl(self._disc)
@property
def release(self):
"""The corresponding MusicBrainz release, chosen by the user"""
if self._release is None:
self._release = self.getRelease(self._submit)
# can still be None
return self._release
def getRelease(self, submit=False):
"""Find the corresponding MusicBrainz release
"""
discId_filter = ReleaseFilter(discId=self.id)
try:
results = query.getReleases(filter=discId_filter)
except ConnectionError, e:
printError("Couldn't connect to the Server:", str(e))
sys.exit(1)
except WebServiceError, e:
printError("Couldn't fetch release:", str(e))
sys.exit(1)
if len(results) == 0:
print "This Disc ID is not in the Database."
self._release = None
elif len(results) > 1:
print "This Disc ID is ambiguous:"
for i in range(len(results)):
release = results[i].release
print str(i)+":", release.getArtist().getName(),
print "-", release.getTitle(),
print "(" + release.getTypes()[1].rpartition('#')[2] + ")"
events = release.getReleaseEvents()
for event in events:
country = (event.getCountry() or "").ljust(2)
date = (event.getDate() or "").ljust(10)
barcode = (event.getBarcode() or "").rjust(13)
print "\t", country, "\t", date, "\t", barcode
try:
num = raw_input("Which one do you want? [0-%d] " % i)
self._release = results[int(num)].getRelease()
except (ValueError, IndexError):
printError("Invalid Choice")
sys.exit(1)
except KeyboardInterrupt:
print
print "exiting.."
sys.exit(1)
else:
self._release = results[0].getRelease()
if self._release and self._release.getId() is None:
# a "release" that is only a stub has no musicbrainz id
print
print "There is only a stub in the database:"
print self._release.getArtist().getName(),
print "-", self._release.getTitle()
print
self._release = None # don't use stub
submit = True # the id is verified by the stub
if self._release is None:
if submit:
url = self.submissionUrl
print "Would you like to open the browser to submit the disc?",
if raw_input("[y/N] ") == "y":
try:
os.execlp(options.browser, options.browser, url)
except OSError, e:
printError("Couldn't open the url in %s:"
% (options.browser, str(e)))
printError2("Please submit it via:", url)
sys.exit(1)
else:
print "Please submit the Disc ID with this url:"
print url
sys.exit(1)
else:
print "recalculating to re-check.."
self.getRelease(submit=True)
return self._release
def getDisc(device, submit=False):
disc = Disc(device, submit)
print
print 'DiscID:\t\t', disc.id
print 'Tracks on Disc:\t', disc.trackCount
return disc
def gatherIsrcs(backend, device):
backend_output = []
devnull = open(os.devnull, "w")
if backend in ["cdda2wav", "icedax"]:
pattern = \
'T:\s+([0-9]+)\sISRC:\s+([A-Z]{2})-?([A-Z0-9]{3})-?(\d{2})-?(\d{5})'
try:
p1 = Popen([backend, '-J', '-H', '-D', device], stderr=PIPE)
p2 = Popen(['grep', 'ISRC'], stdin=p1.stderr, stdout=PIPE)
isrcout = p2.stdout
except OSError, e:
backendError(backend, e)
for line in isrcout:
# there are \n and \r in different places
if debug: print line,
for text in line.splitlines():
if text.startswith("T:"):
m = re.search(pattern, text)
if m == None:
print "can't find ISRC in:", text
continue
trackNumber = int(m.group(1))
isrc = m.group(2) + m.group(3) + m.group(4) + m.group(5)
backend_output.append((trackNumber, isrc))
elif backend == "cd-info":
pattern = \
'TRACK\s+([0-9]+)\sISRC:\s+([A-Z]{2})-?([A-Z0-9]{3})-?(\d{2})-?(\d{5})'
try:
p = Popen([backend, '-T', '-A', '--no-device-info', '--no-cddb',
'-C', device], stdout=PIPE)
isrcout = p.stdout
except OSError, e:
backendError(backend, e)
for line in isrcout:
if debug: print line,
if line.startswith("TRACK"):
m = re.search(pattern, line)
if m == None:
print "can't find ISRC in:", line
continue
trackNumber = int(m.group(1))
isrc = m.group(2) + m.group(3) + m.group(4) + m.group(5)
backend_output.append((trackNumber, isrc))
elif backend in ["mediatools", "media_info"]:
pattern = \
'ISRC\s+([0-9]+)\s+([A-Z]{2})-?([A-Z0-9]{3})-?(\d{2})-?(\d{5})'
if backend == "mediatools":
args = [backend, "drive", device, "isrc"]
else:
args = [backend, device]
try:
p = Popen(args, stdout=PIPE)
isrcout = p.stdout
except OSError, e:
backendError(backend, e)
for line in isrcout:
if debug: print line,
if line.startswith("ISRC") and not line.startswith("ISRCS"):
m = re.search(pattern, line)
if m == None:
print "can't find ISRC in:", line
continue
trackNumber = int(m.group(1))
isrc = m.group(2) + m.group(3) + m.group(4) + m.group(5)
backend_output.append((trackNumber, isrc))
elif backend == "cdrdao":
pattern = '[A-Z]{2}[A-Z0-9]{3}\d{2}\d{5}'
tmpname = "cdrdao-%s.toc" % datetime.now()
tmpname = tmpname.replace(":", "-") # : is invalid on windows
tmpfile = os.path.join(tempfile.gettempdir(), tmpname)
if debug: print "Saving toc in %s.." % tmpfile
if os.name == "nt":
printError("cdrdao can only use the default device!")
args = [backend, "read-toc", "--fast-toc", "-v", "0", tmpfile]
else:
args = [backend, "read-toc", "--fast-toc", "--device", device,
"-v", "0", tmpfile]
try:
p = Popen(args ,stdout=devnull, stderr=devnull)
if p.wait() != 0:
printError("%s returned with %i" % (backend, p.returncode))
sys.exit(1)
except OSError, e:
backendError(backend, e)
else:
with open(tmpfile, "r") as toc:
trackNumber = None
for line in toc:
if debug: print line,
words = line.split()
if len(words) > 0:
if words[0] == "//":
trackNumber = int(words[2])
elif words[0] == "ISRC" and trackNumber is not None:
isrc = "".join(words[1:]).strip('"- ')
m = re.match(pattern, isrc)
if m is None:
print "no valid ISRC:", isrc
elif isrc:
backend_output.append((trackNumber, isrc))
# safeguard against missing trackNumber lines
# or duplicated ISRC tags (like in CD-Text)
trackNumber = None
finally:
try:
os.unlink(tmpfile)
except:
pass
elif backend == "drutil":
pattern = \
'Track\s+([0-9]+)\sISRC:\s+([A-Z]{2})-?([A-Z0-9]{3})-?(\d{2})-?(\d{5})'
try:
p1 = Popen([backend, 'subchannel', '-drive', device], stdout=PIPE)
p2 = Popen(['grep', 'ISRC'], stdin=p1.stdout, stdout=PIPE)
isrcout = p2.stdout
except OSError, e:
backendError(backend, e)
for line in isrcout:
if debug: print line,
if line.startswith("Track") and line.find("block") > 0:
m = re.search(pattern, line)
if m == None:
print "can't find ISRC in:", line
continue
trackNumber = int(m.group(1))
isrc = m.group(2) + m.group(3) + m.group(4) + m.group(5)
backend_output.append((trackNumber, isrc))
return backend_output
def cleanupIsrcs(isrcs):
for isrc in isrcs:
tracks = isrcs[isrc].getTracks()
if len(tracks) > 1:
print
print "ISRC", isrc, "attached to:"
for track in tracks:
print "\t",
artist = track.getArtist()
string = ""
if artist:
string += artist.getName() + " - "
string += track.getTitle()
print string,
# tab alignment
if len(string) >= 32:
print
print " " * 40,
else:
if len(string) < 7:
print "\t",
if len(string) < 15:
print "\t",
if len(string) < 23:
print "\t",
if len(string) < 31:
print "\t",
# append track# and evaluation, if available
if isinstance(track, NumberedTrack):
print "\t track", track.getNumber(),
if isinstance(track, OwnTrack):
print " [OUR EVALUATION]"
else:
print
url = "http://musicbrainz.org/isrc/" + isrc
if raw_input("Open ISRC in the browser? [Y/n] ") != "n":
Popen([options.browser, url])
raw_input("(press <return> when done with this ISRC) ")
# "main" + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
print scriptVersion()
print
# - - - - "global" variables - - - -
# gather chosen options
options = gatherOptions(sys.argv)
# we set the device after we know which backen we will use
backend = options.backend
debug = options.debug
# the actuall query will be created when it is used the first time
query = DemandQuery(options.user, agentName)
disc = None
print "using python-musicbrainz2", musicbrainz2_version
if StrictVersion(musicbrainz2_version) < "0.7.0":
printError("Your version of python-musicbrainz2 is outdated")
printError2("You WILL NOT be able to even check ISRCs")
printError2("Please use AT LEAST python-musicbrainz2 0.7.0")
sys.exit(-1) # the script can't do anything useful
if StrictVersion(musicbrainz2_version) < "0.7.3":
printError("Cannot use AUTH DIGEST")
printError2("You WILL NOT be able to submit ISRCs -> check-only")
printError2("Please use python-musicbrainz2 0.7.3 or higher")
# do not exit, check-only is what happens most of the times anyways
# We print two warnings for clients between 0.7.0 and 0.7.3,
# because 0.7.4 is important. (-> no elif)
if StrictVersion(musicbrainz2_version) < "0.7.4":
print "WARNING: Cannot set userAgent"
print " You WILL have random connection problems due to throttling"
print " Please use python-musicbrainz2 0.7.4 or higher"
print
# search for backend
if backend is None:
for prog in backends:
if hasBackend(prog):
backend = prog
break
# (still) no backend available?
if backend is None:
verbose_backends = []
for program in backends:
if program in packages:
verbose_backends.append(program + " (" + packages[program] + ")")
else:
verbose_backends.append(program)
printError("Cannot find a backend to extract the ISRCS!")
printError2("Isrcsubmit can work with one of the following:")
printError2(" " + ", ".join(verbose_backends))
sys.exit(-1)
else:
print "using", getProgVersion(backend)
if backend == "drutil":
# drutil (Mac OS X) expects 1,2,..
# convert linux default
if options.device == "/dev/cdrom":
options.device = "1"
# libdiscid needs to know what disk that corresponds to
# drutil will tell us
device = getRealMacDevice(options.device)
if debug:
print "CD drive #%s corresponds to %s internally" % (
options.device, device)
else:
# for linux the real device is the same as given in the options
device = options.device
disc = getDisc(device, submit=False)
releaseId = disc.release.getId() # implicitly fetches release
include = ReleaseIncludes(artist=True, tracks=True, isrcs=True, discs=True)
try:
release = query.getReleaseById(releaseId, include=include)
except ConnectionError, e:
printError("Couldn't connect to the Server:", str(e))
sys.exit(1)
except WebServiceError, e:
printError("Couldn't fetch release:", str(e))
sys.exit(1)
tracks = release.getTracks()
releaseTrackCount = len(tracks)
discs = release.getDiscs()
# discCount is actually the count of DiscIDs
# there can be multiple DiscIDs for a single disc
discIdCount = len(discs)
print 'Artist:\t\t', release.getArtist().getName()
print 'Release:\t', release.getTitle()
if releaseTrackCount != disc.trackCount:
# a track count mismatch probably due to
# multiple discs in the release
print "Tracks in Release:", releaseTrackCount
# Handling of multiple discs in the release:
# We can only get the overall release from MB
# and not the Medium itself.
# This changed with NGS. Before there was one MB release per disc.
print
print "WARNING: Multi-disc-release given by web service."
print "See '" + scriptname, "-h' for help"
if discIdCount == 1:
# This is actually a weird case
# Having only 1 disc, but not matching trackCounts
# Possibly some data/video track.
# but also possible that there is a bonus DVD (no disc ID possible)
print "Track count mismatch!"
print "There are", disc.trackCount, "tracks on the disc,"
print "but", releaseTrackCount,"tracks"
print "given for just one DiscID."
print
discIdNumber = 1
else:
print "Discs (or disc IDs) in Release: ", discIdCount
for i in range(discIdCount):
print "\t", discs[i].getId(),
if discs[i].getId() == disc.id:
discIdNumber = i + 1
print "[THIS DISC]"
else:
print
print "There might be multiple disc IDs per disc or none,"
print "so the number of actual discs could be lower or even higher."
print
print "This is disc (ID)", discIdNumber, "of", discIdCount
if discIdNumber == 1 and discIdCount > 1:
# the first disc never needs an offset
# unless we have a track count mismatch and only one disc id given
trackOffset = 0
print "Guessing track offset as", trackOffset
elif discIdCount == 1 and disc.trackCount < releaseTrackCount:
# bonus DVD (without disc ID) given in MB?
# better handling in version 2 api
trackOffset = 0
print "This release probably has a bonus DVD without a discID."
print "Guessing track offset as", trackOffset
else:
# cannot guess fully automatically
if discIdCount > 1 and discIdNumber == discIdCount:
# It is easy to guess the offset when this is the last disc,
# because we have no unknown track counts after this.
trackOffset = releaseTrackCount - disc.trackCount
print "It looks like the last disc of the release."
print "This might be wrong when a bonus DVD is part of the release."
print "Our offset guess would be:", trackOffset
print
else:
# For "middle" discs we have unknown track numbers
# before and after the current disc.
# The same when we have only one disc ID but a track mismatch
# -> the user has to tell us an offset to use
print "Cannot guess the track offset."
# There can also be multiple discIds for one disc of the release
# so we give a MB-link to help which IDs
# belong to which disc of the release.
# We can't provide that ourselves without making
# many requests to MB or using the new web-api 2.
url = releaseId + "/discids" # The "releaseId" is an url itself
print "This url would provide some info about the disc IDs:"
print url
print "Would you like to open it the browser?",
if raw_input("[y/N] ") == "y":
try:
Popen([options.browser, url])
except OSError, e:
printError("Couldn't open the url with %s: %s"
% (options.browser, str(e)))
trackOffset = askForOffset(disc.trackCount, releaseTrackCount)
else:
# the track count matches
trackOffset = 0
print
# Extract ISRCs
backend_output = gatherIsrcs(backend, options.device) # (track, isrc)
# prepare to add the ISRC we found to the corresponding track
# and check for local duplicates now and server duplicates later
isrcs = dict() # isrcs found on disc
tracks2isrcs = dict() # isrcs to be submitted
errors = 0
for (trackNumber, isrc) in backend_output:
if isrc not in isrcs:
isrcs[isrc] = Isrc(isrc)
# check if we found this ISRC for multiple tracks
with_isrc = filter(lambda item: item[1] == isrc, backend_output)
if len(with_isrc) > 1:
listOfTracks = map(str, map(lambda l: l[0], with_isrc))
printError(backend + " gave the same ISRC for multiple tracks!")
printError2("ISRC:", isrc, "\ttracks:", ", ".join(listOfTracks))
errors += 1
try:
track = tracks[trackNumber + trackOffset - 1]
ownTrack = OwnTrack(track, trackNumber)
isrcs[isrc].addTrack(ownTrack)
# check if the ISRC was already added to the track
if isrc not in track.getISRCs():
tracks2isrcs[track.getId()] = isrc
print "found new ISRC for track",
print str(trackNumber) + ":", isrc
else:
print isrc, "is already attached to track", trackNumber
except IndexError, e:
printError("ISRC", isrc, "found for unknown track", trackNumber)
errors += 1
for isrc in isrcs:
for track in isrcs[isrc].getTracks():
trackNumber = track.getNumber()
print
# try to submit the ISRCs
update_intention = True
if len(tracks2isrcs) == 0:
print "No new ISRCs could be found."
else:
if errors > 0:
printError(errors, "problems detected")
if raw_input("Do you want to submit? [y/N] ") == "y":
try:
query.submitISRCs(tracks2isrcs)
print "Successfully submitted", len(tracks2isrcs), "ISRCs."
except RequestError, e:
printError("Invalid Request:", str(e))
except AuthenticationError, e:
printError("Invalid Credentials:", str(e))
except WebServiceError, e:
printError("Couldn't send ISRCs:", str(e))
else:
update_intention = False
print "Nothing was submitted to the server."
# check for overall duplicate ISRCs, including server provided
if update_intention:
duplicates = 0
# add already attached ISRCs
for i in range(0, len(tracks)):
track = tracks[i]
if i in range(trackOffset, trackOffset + disc.trackCount):
trackNumber = i - trackOffset + 1
track = NumberedTrack(track, trackNumber)
for isrc in track.getISRCs():
# only check ISRCS we also found on our disc
if isrc in isrcs:
isrcs[isrc].addTrack(track)
# check if we have multiple tracks for one ISRC
for isrc in isrcs:
if len(isrcs[isrc].getTracks()) > 1:
duplicates += 1
if duplicates > 0:
print
print "There were", duplicates, "ISRCs",
print "that are attached to multiple tracks on this release."
if raw_input("Do you want to help clean those up? [y/N] ") == "y":
cleanupIsrcs(isrcs)
# vim:set shiftwidth=4 smarttab expandtab: