summaryrefslogtreecommitdiff
path: root/plugins/AlbumCover.py
diff options
context:
space:
mode:
authorjerous <jerous@gmail.com>2008-10-31 00:39:19 +0100
committerjerous <jerous@gmail.com>2008-10-31 00:39:19 +0100
commit2f08c0d78c80758dcb6c1982e09519d3cb63cc73 (patch)
tree23832ca5b197b96172d7fe59cfc81f015070c6ef /plugins/AlbumCover.py
parent30c157d5a16e8fae9e63335d7beaa444e48529ee (diff)
more flexible system for downloading album covers
Diffstat (limited to 'plugins/AlbumCover.py')
-rw-r--r--plugins/AlbumCover.py124
1 files changed, 51 insertions, 73 deletions
diff --git a/plugins/AlbumCover.py b/plugins/AlbumCover.py
index 70ce544..943dceb 100644
--- a/plugins/AlbumCover.py
+++ b/plugins/AlbumCover.py
@@ -1,29 +1,26 @@
from clPlugin import *
from traceback import print_exc
from thread import start_new_thread
+import format
# FETCH MODES
AC_NO_FETCH='0'
AC_FETCH_LOCAL_DIR='1'
-AC_FETCH_ALBUM_DIR='2'
-AC_FETCH_INTERNET='3'
-
-# DOWNLOAD TO MODES
-AC_DL_LOCAL_DIR='0'
-AC_DL_ALBUM_DIR='1'
+AC_FETCH_INTERNET='2'
AC_DEFAULT_GFX_EXTS='jpg,jpeg,bmp,gif,png'
AC_DEFAULT_FILES='cover,album'
+AC_DEFAULT_DOWNTO='$dirname($file)/$cover'
class wgAlbumCover(QtGui.QWidget):
" container for the image"
img=None
imgLoaded=False
p=None # plugin
+ acFormat=None
def __init__(self,parent=None):
QtGui.QWidget.__init__(self,parent)
self.img=QtGui.QImage()
-
self.setMinimumSize(64,64)
def mousePressEvent(self, event):
@@ -55,19 +52,41 @@ class wgAlbumCover(QtGui.QWidget):
# set default cover, in case all else fails!
self.imgLoaded=False
self.img.load('gfx/no-cd-cover.png')
- for i in xrange(3):
+ self.acFormat=format.compile(settings.get('albumcover.downloadto', AC_DEFAULT_DOWNTO))
+ for i in xrange(2):
src=settings.get('albumcover.fetch%i'%(i), str(i))
- if src!=AC_NO_FETCH:
- # (download and) load in self.img from a source!
- if self.fetchCoverSrc(song, src):
- # ACK!
- self.imgLoaded=True
- break
+ if src!=AC_NO_FETCH and self.fetchCoverSrc(song, src):
+ # ACK!
+ self.imgLoaded=True
+ break
self.update()
+ def getLocalACPath(self, song, probe, covers, exts):
+ """Get the local path of an albumcover. If $probe, then try covers*exts for existing file."""
+ params={'music_dir': mpdSettings.get('music_directory'), 'cover':'%s.%s'%(covers[0], exts[0])}
+ if probe:
+ self.p.debug("probing ...")
+ for cover in covers:
+ for ext in exts:
+ params['cover']='%s.%s'%(cover, ext)
+ path=self.acFormat(format.params(song, params))
+ self.p.debug(" path: %s"%(path))
+ fInfo=QtCore.QFileInfo(path)
+ if fInfo.exists():
+ self.p.debug(" OK!")
+ return path
+ self.p.debug("done probing: no matching albumcover found")
+ return None
+ else:
+ self.p.debug("no probing")
+ path=self.acFormat(format.params(song, params))
+ self.p.debug(" path: %s"%(path))
+ return path
+
+
def fetchCoverSrc(self, song, src):
"""Fetch the album cover for $song from $src."""
- if not src in [AC_FETCH_INTERNET, AC_FETCH_LOCAL_DIR, AC_FETCH_ALBUM_DIR]:
+ if not src in [AC_FETCH_INTERNET, AC_FETCH_LOCAL_DIR]:
print "wgAlbumCover::fetchCover - invalid source "+str(src)
return False
@@ -79,11 +98,6 @@ class wgAlbumCover(QtGui.QWidget):
coverTitles=settings.get('albumcover.files', AC_DEFAULT_FILES).split(',')
coverTitles=map(lambda title: title.strip(), coverTitles)
- # song absolute path
- fInfo=QtCore.QFileInfo(song.getFilepath())
-
- songAbsDir='%s/%s/'%(mpdSettings.get('music_directory'), toAscii(unicode(fInfo.path())))
-
if src==AC_FETCH_INTERNET:
# look on the internetz!
try:
@@ -98,10 +112,7 @@ class wgAlbumCover(QtGui.QWidget):
# read the url, i.e. retrieve image data
img=urllib.urlopen(coverURL)
# where do we save to?
- if settings.get('albumcover.downloadto', AC_DL_LOCAL_DIR)==AC_DL_LOCAL_DIR:
- file='%s/%s.jpg'%(songAbsDir,coverTitles[0])
- elif settings.get('albumcover.downloadto', AC_DL_LOCAL_DIR)==AC_DL_ALBUM_DIR:
- file='%s/%s - %s.jpg'%(settings.get('albumcover.dir'),song.getArtist(),song.getAlbum())
+ file=self.getLocalACPath(song, False, coverTitles, exts)
# open file, and write the read of img!
f=open(file,'wb')
f.write(img.read())
@@ -114,40 +125,15 @@ class wgAlbumCover(QtGui.QWidget):
self.p.normal("failed to download cover from Amazon")
print_exc()
return False
- if src==AC_FETCH_LOCAL_DIR:
- self.p.extended("looking in local directory")
- # look in local directory
- # create all possible album cover filenames ...
- coverFiles=[]
- for title in coverTitles:
- for ext in exts:
- coverFiles.append('%s.%s'%(title,ext))
- path=songAbsDir
-
-
- elif src==AC_FETCH_ALBUM_DIR:
- self.p.extended("looking in album directory")
- # look in album directory
- path=settings.get('albumcover.dir')
- fInfo=QtCore.QFileInfo(path)
- if not fInfo.isDir():
- return False
- coverFiles=[]
- for ext in exts:
- coverFiles.append('%s - %s.%s'%(song.getArtist(),song.getAlbum(),ext))
- # set path
-
- # now we have to look in $path for the files in $coverFiles!
- for coverFile in coverFiles:
- file='%s/%s'%(path, coverFile)
- if QtCore.QFileInfo(file).exists():
- try:
- self.img.load(file)
- self.p.extended("cover set!")
- return True
- except:
- self.p.normal("failed to load %s"%(file))
- return False
+
+ file=self.getLocalACPath(song, True, coverTitles, exts)
+ if file:
+ try:
+ self.img.load(file)
+ self.p.extended("cover set!")
+ return True
+ except:
+ self.p.normal("failed to load %s"%(path))
class pluginAlbumCover(Plugin):
@@ -171,14 +157,11 @@ class pluginAlbumCover(Plugin):
return "Displays the album cover of the currently playing album in a widget.\n" \
"This album cover can be fetched from various locations:\n" \
" local dir: the directory in which the album is located;\n" \
- " albums dir: a directory containing all album covers. The album covers are named $artist - $album;\n" \
- " internet: fetch from amazon.com\n\n" \
+ " internet: look on amazon for the album and corresponding cover\n" \
"Settings:\n" \
- " albumcover.fetch$i: what source to fetch from on step $i. If step $i fails, move on to step $i+1. If step0==no fetching, then no cover will be looked for;\n" \
- " albumcover.dir: location of the directory containing the album cover;\n" \
- " albumcover.downloadTo: where to download album covers from internet to. The filename will be $artist - $album. If this directory doesn't exist, showing the album cover will fail.\n" \
- " albumcover.files: comma separated list of filenames (without extension)to be considered an album cover. The extensions from albumcover.gfx.ext are used.\n" \
- " albumcover.gfx.ext: comma separated list of gfx-file extensions.\n"
+ " albumcover.fetch$i: what source to fetch from on step $i. If step $i fails, move on to step $i+1;\n" \
+ " albumcover.downloadto: where to download album covers from internet to. This string can contain the normal tags of the current playing song, plus $music_dir and $cover.\n" \
+ " albumcover.files: comma separated list of filenames (without extension)to be considered an album cover. Extensions jpg, jpeg, png, gif and bmp are used.\n"
def getWidget(self):
return self.o
@@ -191,23 +174,18 @@ class pluginAlbumCover(Plugin):
def _getSettings(self):
ret=[]
- nums=['first', 'second', 'third']
- actions=[QtGui.QComboBox(), QtGui.QComboBox(), QtGui.QComboBox()]
+ nums=['first', 'second']
+ actions=[QtGui.QComboBox(), QtGui.QComboBox()]
for i,action in enumerate(actions):
setting='albumcover.fetch%i'%(i)
action.addItem("On the %s action, Monty rested."%(nums[i]))
action.addItem("Local dir")
- action.addItem("Albums dir")
action.addItem("Internet")
action.setCurrentIndex(int(settings.get(setting, str(i+1))))
ret.append([setting, 'Action %i'%(i+1), 'What to do on the %s step.'%(nums[i]), action])
- downloadTo=QtGui.QComboBox()
- downloadTo.addItem("Local dir")
- downloadTo.addItem("Albums dir")
- ret.append(['albumcover.downloadto', 'Download album covers to', 'Specifies where to save album covers fetched from internet to.\nIf Albums dir is selected, and the albums dir is not set properly, album covers are not downloaded, nor shown.', downloadTo])
+ ret.append(['albumcover.downloadto', 'Local dir', 'Specifies where to save album covers fetched from internet to.\nPossible tags: music_dir, cover, file, artist, title, album', QtGui.QLineEdit(settings.get('albumcover.downloadto', AC_DEFAULT_DOWNTO))])
ret.append(['albumcover.files', 'Album cover files', 'Comma separated list of titles that are to be considered album covers. E.g. cover,album.', QtGui.QLineEdit(settings.get('albumcover.files', AC_DEFAULT_FILES))])
- ret.append(['albumcover.dir', 'Album directory', 'Directory containing the album covers.\nIf empty, or not existing, album covers won\'t be downloaded or retrieved from this directory.', QtGui.QLineEdit(settings.get('albumcover.dir'))])
return ret