[SCM] morituri/master: * 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.
js at users.alioth.debian.org
js at users.alioth.debian.org
Sun Oct 19 20:09:03 UTC 2014
The following commit has been merged in the master branch:
commit 9710e3cc7c6370d81cf7e2eeff608855f08a6fd2
Author: Thomas Vander Stichele <thomas (at) apestaart (dot) org>
Date: Sat May 23 13:38:44 2009 +0000
* 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.
diff --git a/ChangeLog b/ChangeLog
index 6978a1f..22180be 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
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:
* morituri/rip/main.py:
* morituri/rip/cd.py (added):
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 <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
+
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)
--
morituri packaging
More information about the pkg-multimedia-commits
mailing list