* 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.
This commit is contained in:
Thomas Vander Stichele
2009-05-23 13:38:44 +00:00
parent da8145798d
commit 9710e3cc7c
4 changed files with 109 additions and 13 deletions

View File

@@ -1,3 +1,11 @@
2009-05-23 Thomas Vander Stichele <thomas at apestaart dot org>
* 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 <thomas at apestaart dot org>
* morituri/rip/Makefile.am:

View File

@@ -4,6 +4,7 @@ morituridir = $(PYTHONLIBDIR)/morituri/common
morituri_PYTHON = \
__init__.py \
accurip.py \
checksum.py \
common.py \
log.py \

View File

@@ -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 <http://www.gnu.org/licenses/>.
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

View File

@@ -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)