"""
Local Cover Art
Plugin by Pier Angelo Vendrame
This plugin checks if you have a covert art image in the directory
of the album. It checks for Front.jpg and Front.png files.
The plugin is based on the Cover Art Downloader by Oliver Charles
and Philipp Wolfer.
"""
PLUGIN_NAME = 'Local Cover Art'
PLUGIN_AUTHOR = 'Pier Angelo Vendrame'
PLUGIN_DESCRIPTION = '''Look for a cover art in the directory of the album.
Valid file names are front, cover, folder and, for albums with more than a disc,
front-discnumber, front_dicnumber, front discnumber.
Valid formats are PNG and JPEG.'''
PLUGIN_VERSION = "0.3"
PLUGIN_API_VERSIONS = ["0.16"]
from picard.metadata import register_track_metadata_processor
import os, mimetypes
# Add an image to metadata
def localCoverArt_addImage(metadata, imagefile):
# Load image as data
in_file = open(imagefile, 'r')
data = in_file.read()
in_file.close()
# Add it
mime = mimetypes.guess_type(imagefile)[0]
metadata.add_image(mime, data)
# Get directories of files
def localCoverArt_getDirs(tagger):
dirs = []
for f in tagger.iterfiles():
path = os.path.abspath(f.filename)
path = os.path.dirname(path)
if not path in dirs:
dirs.append(path)
return dirs
# Get a valid image filename
def localCoverArt_checkFile(dirs, discNumber = ''):
# Possible extensions
exts = ['.png', '.jpg', '.jpeg']
# Build possible paths
paths = []
# First check if the disc number is set
discNumber = str(discNumber)
if discNumber != '':
paths = [
'Front-' + discNumber,
'Front ' + discNumber,
'Front_' + discNumber,
]
# In all cases check also normal images
paths += ['Front', 'Cover', 'Folder']
# Add also the lower-cased version of strings
lowercased = []
for i in paths:
lowercased.append(i.lower())
paths += lowercased
del lowercased
# Check the paths and return the first valid one
for i in dirs:
for j in paths:
for k in exts:
checkPath = os.path.join(i, j + k)
if os.path.exists(checkPath):
return checkPath
return ''
def localCoverArt_processor(tagger, metadata, trackNode, releaseNode):
dirs = localCoverArt_getDirs(tagger)
imagefile = localCoverArt_checkFile(dirs, metadata['discnumber'])
localCoverArt_addImage(metadata, imagefile)
register_track_metadata_processor(localCoverArt_processor)