/
isrcsubmit-cdrdao.py
executable file
·147 lines (128 loc) · 5.08 KB
/
isrcsubmit-cdrdao.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
#!/usr/bin/env python2
# Copyright 2010 Johannes Dewender ( brainz at JonnyJD.net )
# Copyright 2011 Wieland Hoffmann
# 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/>.
"""Submits ISRCs to MusicBrainz
heavily inspired by http://kraehen.org/isrcsubmit.py
"""
import discid
import getpass
import musicbrainzngs
import optparse
import subprocess
from datetime import datetime
from os import remove
from sys import exit, version_info
def main():
parser = optparse.OptionParser()
parser.add_option("-u", "--user", type=str, help="Username")
parser.add_option("-p", "--password", type=str, help="Password")
parser.add_option("-d", "--device", type=str,
default=discid.get_default_device(),
help="Device name, the default is %s" % discid.get_default_device())
(args, options) = parser.parse_args()
if not args.user:
exit("No username given")
if not args.password:
password = getpass.getpass()
else:
password = args.password
try:
disc = discid.read(args.device)
except discid.DiscError:
exit("No discid could be calculated")
musicbrainzngs.auth(args.user, password)
musicbrainzngs.set_useragent("isrcsubmit-cdrdao", "0.2", "Mineo@Freenode")
results = musicbrainzngs.get_releases_by_discid(
disc.id, includes=["recordings", "isrcs",
"artist-credits"])["disc"]["release-list"]
if len(results) == 0:
print "The disc is not in the database"
print "Please submit it with: %s" % disc.submission_url
exit(1)
elif len(results) > 1:
print "This Disc ID is ambiguous:"
for i, release in enumerate(results):
print str(i)+":", release["artist-credit-phrase"]
print "-", release["title"]
print release["id"]
num = -1
while True:
try:
num = raw_input("Which one do you want? [0-%d] " % i)
release = results[int(num)]
except (IndexError, ValueError):
continue
break
else:
release = results[0]
print 'Artist: %s' % release["artist-credit-phrase"]
print 'Release: %s' % release["title"]
real_medium = None
for medium in release["medium-list"]:
for mdisc in medium["disc-list"]:
print mdisc
if mdisc["id"] == disc.id:
real_medium = medium
break
filename = "/tmp/cdrdao-%s.toc" % datetime.now()
try:
proc = subprocess.Popen(["cdrdao", "read-toc", "--fast-toc", "--device",
args.device, "-v", "0", filename], stderr=subprocess.PIPE,
stdout=subprocess.PIPE)
proc.wait()
except Exception, e:
exit("Exception while calling cdrdao: %s" % str(e))
if proc.returncode != 0:
exit("cdrdao returned with return code %i" % proc.returncode)
tracks = real_medium["track-list"]
tracks2isrcs = dict()
tracknum = 0
with open(filename, "r") as tocfile:
for line in tocfile:
sline = line.split(" ")
if sline[0] == "//":
tracknum = int(sline[2]) - 1
elif sline[0] == "ISRC":
isrc = sline[1][1:-2]
try:
if isrc not in tracks[tracknum]["recording"]["isrc-list"]:
tracks2isrcs[tracks[tracknum]["recording"]["id"]] = [isrc]
except KeyError:
tracks2isrcs[tracks[tracknum]["recording"]["id"]] = [isrc]
if len(tracks2isrcs) == 0:
print "No new ISRCs could be found."
else:
vals = tracks2isrcs.values()
print "\nISRC | Recording"
print "-" * 13 + "+" + "-" * 66
for key, val in sorted(tracks2isrcs.items(), key=lambda keyval: keyval[1]):
if vals.count(val) > 1:
print "The ISRC %s appears multiple times, I'm not going to submit it"\
% val
tracks2isrcs.pop(key)
continue
print "%s | %s" % (val, key)
if raw_input("Is this correct? [y/N]").lower() == "y":
if len(tracks2isrcs.keys()) > 0:
try:
musicbrainzngs.submit_isrcs(tracks2isrcs)
print "Successfully submitted", len(tracks2isrcs), "ISRCs."
except musicbrainzngs.WebServiceError, e:
print "Couldn't send ISRCs:", str(e)
else:
print "Nothing was submitted to the server."
remove(filename)
if __name__ == '__main__':
main()