Skip to content

Commit

Permalink
add libdiscid as (direct) backend
Browse files Browse the repository at this point in the history
We now depend on pyton-discid >= 0.5.0
That is where ISRC support was added and the API changed.
  • Loading branch information
JonnyJD committed Apr 26, 2013
1 parent ec854cd commit 795b198
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 30 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Isrcsubmit 2.0.0-beta.1 for MusicBrainz
Isrcsubmit 2.0.0-dev for MusicBrainz
==============================
(Linux/Mac OS X/Windows)
------------------------
Expand All @@ -11,7 +11,7 @@ and submits them to [MusicBrainz](http://musicbrainz.org).
This script uses
[python-musicbrainzngs](http://musicbrainz.org/doc/python-musicbrainz-ngs)
to access the MusicBrainz API
and [python-discid](https://python-discid.readthedocs.org/)
and [python-discid](https://python-discid.readthedocs.org/) >= 0.5.0
to create an identifier for the disc.
You need Python 2 >= 2.6 or Python 3 >= 3.1.

Expand Down Expand Up @@ -61,7 +61,7 @@ Others don't, for the same physical disc.
For me my dvd writer worked better.
On Windows the mediatools backend should give correct results either way.

Isrcsubmit checks for problems with duplicate ISRCs and prints a warning.
Isrcsubmit 2.0.0-dev for problems with duplicate ISRCs and prints a warning.
You will always have the choice to cancel the submission if something
seems to be wrong.

Expand Down
67 changes: 41 additions & 26 deletions isrcsubmit.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
agent_name = "isrcsubmit.py"
musicbrainz_server = "musicbrainz.org"
# starting with highest priority
backends = ["mediatools", "media_info", "discisrc", "cdrdao", "cd-info",
"cdda2wav", "icedax", "drutil"]
backends = ["mediatools", "media_info", "libdiscid", "discisrc", "cdrdao",
"cd-info", "cdda2wav", "icedax", "drutil"]
packages = {"cd-info": "libcdio", "cdda2wav": "cdrtools", "icedax": "cdrkit"}

import os
Expand Down Expand Up @@ -238,6 +238,8 @@ def get_prog_version(prog):
for line in outdata.splitlines():
if line:
version += b" " + line.split(":")[1].strip()
elif prog == "libdiscid":
version = discid.LIBDISCID_VERSION_STRING
else:
version = prog

Expand All @@ -247,6 +249,9 @@ def has_backend(backend, strict=False):
"""When the backend is only a symlink to another backend,
we will return False, unless we strictly want to use this backend.
"""
if backend == "libdiscid":
return "isrc" in discid.FEATURES

devnull = open(os.devnull, "w")
if options.sane_which:
p_which = Popen(["which", backend], stdout=PIPE, stderr=devnull)
Expand Down Expand Up @@ -430,16 +435,16 @@ class Disc(object):
def read_disc(self):
try:
# calculate disc ID from disc
with discid.DiscId() as disc:
disc.read(self._device)
self._id = disc.id
self._submission_url = disc.submission_url
self._track_count = len(disc.track_offsets) - 1
if self._backend == "libdiscid":
disc = discid.read(self._device, features=["mcn", "isrc"])
else:
disc = discid.read(self._device)
self._disc = disc
except DiscError as err:
print_error("DiscID calculation failed: %s" % err)
sys.exit(1)

def __init__(self, device, verified=False):
def __init__(self, device, backend, verified=False):
if sys.platform == "darwin":
self._device = get_real_mac_device(device)
if debug:
Expand All @@ -448,20 +453,21 @@ def __init__(self, device, verified=False):
else:
self._device = device
self._release = None
self._backend = backend
self._verified = verified
self.read_disc() # sets self._id etc.

@property
def id(self):
return self._id
return self._disc.id

@property
def track_count(self):
return self._track_count
def tracks(self):
return self._disc.tracks

@property
def submission_url(self):
return self._submission_url
return self._disc.submission_url

@property
def release(self):
Expand Down Expand Up @@ -566,22 +572,31 @@ def getRelease(self, verified=False):

return self._release

def get_disc(device, verified=False):
def get_disc(device, backend, verified=False):
"""This creates a Disc object, which also calculates the id of the disc
"""
disc = Disc(device, verified)
disc = Disc(device, backend, verified)
print('\nDiscID:\t\t%s' % disc.id)
print('Tracks on disc:\t%d' % disc.track_count)
print('Tracks on disc:\t%d' % len(disc.tracks))
return disc


def gather_isrcs(backend, device):
def gather_isrcs(disc, backend, device):
"""read the disc in the device with the backend and extract the ISRCs
"""
backend_output = []
devnull = open(os.devnull, "w")

if backend == "discisrc":
if backend == "libdiscid":
pattern = r'[A-Z]{2}[A-Z0-9]{3}\d{2}\d{5}'
for track in disc.tracks:
if track.isrc:
m = re.match(pattern, track.isrc)
if m is None:
print("no valid ISRC: %s" % track.isrc)
else:
backend_output.append((track.number, track.isrc))
elif backend == "discisrc":
pattern = \
br'Track\s+([0-9]+)\s+:\s+([A-Z]{2})-?([A-Z0-9]{3})-?(\d{2})-?(\d{5})'
try:
Expand All @@ -596,7 +611,7 @@ def gather_isrcs(backend, device):
printf(line) # already includes a newline
if line.startswith(b"Track") and len(line) > 12:
m = re.search(pattern, line)
if m == None:
if m is None:
print("can't find ISRC in: %s" % line)
continue
track_number = int(m.group(1))
Expand All @@ -621,7 +636,7 @@ def gather_isrcs(backend, device):
for text in line.splitlines():
if text.startswith(b"T:"):
m = re.search(pattern, text)
if m == None:
if m is None:
print("can't find ISRC in: %s" % text)
continue
track_number = int(m.group(1))
Expand All @@ -643,7 +658,7 @@ def gather_isrcs(backend, device):
printf(line) # already includes a newline
if line.startswith(b"TRACK"):
m = re.search(pattern, line)
if m == None:
if m is None:
print("can't find ISRC in: %s" % line)
continue
track_number = int(m.group(1))
Expand All @@ -669,7 +684,7 @@ def gather_isrcs(backend, device):
printf(line) # already includes a newline
if line.startswith(b"ISRC") and not line.startswith(b"ISRCS"):
m = re.search(pattern, line)
if m == None:
if m is None:
print("can't find ISRC in: %s" % line)
continue
track_number = int(m.group(1))
Expand Down Expand Up @@ -716,7 +731,7 @@ def gather_isrcs(backend, device):
m = re.match(pattern, isrc)
if m is None:
print("no valid ISRC: %s" % isrc)
elif isrc:
else:
backend_output.append((track_number, isrc))
# safeguard against missing trackNumber lines
# or duplicated ISRC tags (like in CD-Text)
Expand All @@ -743,7 +758,7 @@ def gather_isrcs(backend, device):
printf(line) # already includes a newline
if line.startswith(b"Track") and line.find(b"block") > 0:
m = re.search(pattern, line)
if m == None:
if m is None:
print("can't find ISRC in: %s" % line)
continue
track_number = int(m.group(1))
Expand Down Expand Up @@ -833,7 +848,7 @@ def cleanup_isrcs(isrcs):
else:
print("using %s" % get_prog_version(backend))

disc = get_disc(options.device)
disc = get_disc(options.device, backend)
release_id = disc.release["id"] # implicitly fetches release

print("")
Expand All @@ -853,7 +868,7 @@ def cleanup_isrcs(isrcs):

print("")
# Extract ISRCs
backend_output = gather_isrcs(backend, options.device) # (track, isrc)
backend_output = gather_isrcs(disc, 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
Expand Down Expand Up @@ -905,7 +920,7 @@ def cleanup_isrcs(isrcs):
# add already attached ISRCs
for i in range(0, len(tracks)):
track = tracks[i]
if i in range(0, disc.track_count):
if i in range(0, len(disc.tracks)):
track_number = i + 1
track = Track(track, track_number)
for isrc in track.get("isrc-list", []):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from distutils.core import setup

setup(name="isrcsubmit",
version="2.0.0-beta.1",
version="2.0.0-dev",
description="submit ISRCs from disc to MusicBrainz",
long_description=open("README.md").read(),
author="Johannes Dewender",
Expand Down

0 comments on commit 795b198

Please sign in to comment.