From 9710e3cc7c6370d81cf7e2eeff608855f08a6fd2 Mon Sep 17 00:00:00 2001 From: Thomas Vander Stichele Date: Sat, 23 May 2009 13:38:44 +0000 Subject: [PATCH] * morituri/common/Makefile.am: * morituri/rip/cd.py: * morituri/common/accurip.py (added): Add a module for handling a cache of AccurateRip results. Use it. --- ChangeLog | 8 ++++ morituri/common/Makefile.am | 1 + morituri/common/accurip.py | 89 +++++++++++++++++++++++++++++++++++++ morituri/rip/cd.py | 24 +++++----- 4 files changed, 109 insertions(+), 13 deletions(-) create mode 100644 morituri/common/accurip.py diff --git a/ChangeLog b/ChangeLog index 6978a1f..22180be 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2009-05-23 Thomas Vander Stichele + + * morituri/common/Makefile.am: + * morituri/rip/cd.py: + * morituri/common/accurip.py (added): + Add a module for handling a cache of AccurateRip results. + Use it. + 2009-05-23 Thomas Vander Stichele * morituri/rip/Makefile.am: diff --git a/morituri/common/Makefile.am b/morituri/common/Makefile.am index 1961c83..ba4b57b 100644 --- a/morituri/common/Makefile.am +++ b/morituri/common/Makefile.am @@ -4,6 +4,7 @@ morituridir = $(PYTHONLIBDIR)/morituri/common morituri_PYTHON = \ __init__.py \ + accurip.py \ checksum.py \ common.py \ log.py \ diff --git a/morituri/common/accurip.py b/morituri/common/accurip.py new file mode 100644 index 0000000..23bc5f0 --- /dev/null +++ b/morituri/common/accurip.py @@ -0,0 +1,89 @@ +# -*- Mode: Python -*- +# vi:si:et:sw=4:sts=4:ts=4 + +# Morituri - for those about to RIP + +# Copyright (C) 2009 Thomas Vander Stichele + +# This file is part of morituri. +# +# morituri 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. +# +# morituri 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 morituri. If not, see . + +import os +import urlparse +import urllib2 + +from morituri.common import log +from morituri.image import image + +_CACHE_DIR = os.path.join(os.path.expanduser('~'), '.morituri', 'cache') + +class AccuCache(log.Loggable): + def __init__(self): + if not os.path.exists(_CACHE_DIR): + self.debug('Creating cache directory %s', _CACHE_DIR) + os.makedirs(_CACHE_DIR) + + def _getPath(self, url): + # split path starts with / + return os.path.join(_CACHE_DIR, urlparse.urlparse(url)[2][1:]) + + def retrieve(self, url, force=False): + self.debug("Retrieving AccurateRip URL %s", url) + path = self._getPath(url) + self.debug("Cached path: %s", path) + if not os.path.exists(path): + self.debug("%s does not exist, downloading", path) + self.download(url) + + if not os.path.exists(path): + self.debug("%s does not exist, not in database", path) + return None + + data = self._read(url) + + return image.getAccurateRipResponses(data) + + def download(self, url): + # FIXME: download url as a task too + responses = [] + import urllib2 + try: + handle = urllib2.urlopen(url) + data = handle.read() + + except urllib2.HTTPError, e: + if e.code == 404: + return None + else: + raise + + self._cache(url, data) + return data + + def _cache(self, url, data): + path = self._getPath(url) + os.makedirs(os.path.dirname(path)) + handle = open(path, 'wb') + handle.write(data) + handle.close() + + def _read(self, url): + self.debug("Reading %s from cache", url) + path = self._getPath(url) + handle = open(path, 'rb') + data = handle.read() + handle.close() + return data + diff --git a/morituri/rip/cd.py b/morituri/rip/cd.py index 39f710c..4eda333 100644 --- a/morituri/rip/cd.py +++ b/morituri/rip/cd.py @@ -29,7 +29,7 @@ import shutil import gobject gobject.threads_init() -from morituri.common import logcommand, task, checksum, common +from morituri.common import logcommand, task, checksum, common, accurip from morituri.image import image, cue, table from morituri.program import cdrdao, cdparanoia @@ -201,6 +201,12 @@ class Rip(logcommand.LogCommand): print "CDDB disc id", ittoc.getCDDBDiscId() metadata = musicbrainz(ittoc.getMusicBrainzDiscId()) + url = ittoc.getAccurateRipURL() + print "AccurateRip URL", url + + cache = accurip.AccuCache() + responses = cache.retrieve(url) + # now, read the complete index table, which is slower ptable = common.Persister(self.options.table_pickle or None) if not ptable.object: @@ -309,18 +315,10 @@ class Rip(logcommand.LogCommand): url = itable.getAccurateRipURL() print "AccurateRip URL", url - # FIXME: download url as a task too - 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 + cache = accurip.AccuCache() + responses = cache.retrieve(url) + if not responses: + print 'Album not found in AccurateRip database' if responses: print '%d AccurateRip reponses found' % len(responses)