From f865e915265afdd55f925f3beb9698e75acc8982 Mon Sep 17 00:00:00 2001 From: Thomas Vander Stichele Date: Wed, 15 Apr 2009 09:35:58 +0000 Subject: [PATCH] * morituri/image/image.py: * morituri/test/test_image_image.py: AccurateRip binary files actually contain multiple responses, so parse all of them. * examples/ARcue.py: Handle case of not having a response, and having multiple responses. --- ChangeLog | 9 +++++ examples/ARcue.py | 60 +++++++++++++++++++++++++------ morituri/image/image.py | 13 +++++++ morituri/test/test_image_image.py | 7 +++- 4 files changed, 78 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index 515bfb4..762a57b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2009-04-15 Thomas Vander Stichele + + * morituri/image/image.py: + * morituri/test/test_image_image.py: + AccurateRip binary files actually contain multiple responses, so + parse all of them. + * examples/ARcue.py: + Handle case of not having a response, and having multiple responses. + 2009-04-14 Thomas Vander Stichele * morituri/common/crc.py: diff --git a/examples/ARcue.py b/examples/ARcue.py index 8ebfca8..1e7e006 100644 --- a/examples/ARcue.py +++ b/examples/ARcue.py @@ -81,21 +81,61 @@ def main(argv): print "AccurateRip URL", url # FIXME: download url as a task too - import urllib - (filename, headers) = urllib.urlretrieve(url) - data = open(filename, 'rb').read() - os.unlink(filename) - response = image.AccurateRipResponse(data) + responses = [] + import urllib2 + try: + handle = urllib2.urlopen(url) + data = handle.read() + responses = image.getAccurateRipResponses(data) + except urllib2.HTTPError, e: + if e.code == 404: + print 'Album not found in AccurateRip database' + else: + raise + + if responses: + print '%d AccurateRip reponses found' % len(responses) + + if responses[0].cddbDiscId != cueImage.getCDDBDiscId(): + print "AccurateRip response discid different: %s" % \ + responses[0].cddbDiscId function(runner, verifytask) function(runner, cuetask) + response = None + for i, crc in enumerate(cuetask.crcs): - status = ' rip accurate ' - if "%08x" % crc != response.crcs[i]: - status = '** rip not accurate **' - print "Track %2d: %s (confidence %3d) mine [%08x] AR [%s]" % ( - i + 1, status, response.confidences[i], crc, response.crcs[i]) + status = '** rip not accurate **' + + confidence = None + arcrc = None + + for j, r in enumerate(responses): + if "%08x" % crc == r.crcs[i]: + if not response: + response = r + else: + assert r == response, \ + "CRC %s for %d matches wrong response %d, crc %s" % ( + crc, i + 1, j + 1, response.crcs[i]) + status = ' rip accurate ' + arcrc = crc + confidence = response.confidences[i] + + c = "(not found)" + ar = "" + if responses: + maxConfidence = max(r.confidences[i] for r in responses) + + c = "(confidence %3d)" % maxConfidence + if confidence is not None: + if confidence < maxConfidence: + c = "(confidence %3d of %3d)" % (confidence, maxConfidence) + + ar = " AR [%s]" % response.crcs[i] + print "Track %2d: %s %s mine [%08x] %s" % ( + i + 1, status, c, crc, ar) main(sys.argv) diff --git a/morituri/image/image.py b/morituri/image/image.py index fb3f3cd..c18fac4 100644 --- a/morituri/image/image.py +++ b/morituri/image/image.py @@ -319,6 +319,19 @@ class ImageVerifyTask(MultiTask): MultiTask.stop(self) +# FIXME: move this method to a different module ? +def getAccurateRipResponses(data): + ret = [] + + while data: + trackCount = struct.unpack("B", data[0])[0] + bytes = 1 + 12 + trackCount * (1 + 8) + + ret.append(AccurateRipResponse(data[:bytes])) + data = data[bytes:] + + return ret + class AccurateRipResponse(object): """ I represent the response of the AccurateRip online database. diff --git a/morituri/test/test_image_image.py b/morituri/test/test_image_image.py index 7519a5a..ac45738 100644 --- a/morituri/test/test_image_image.py +++ b/morituri/test/test_image_image.py @@ -88,7 +88,12 @@ class AccurateRipResponseTestCase(unittest.TestCase): path = os.path.join(os.path.dirname(__file__), 'dBAR-011-0010e284-009228a3-9809ff0b.bin') data = open(path, "rb").read() - response = image.AccurateRipResponse(data) + + responses = image.getAccurateRipResponses(data) + self.assertEquals(len(responses), 3) + + + response = responses[0] self.assertEquals(response.trackCount, 11) self.assertEquals(response.discId1, "0010e284")