User Tools

Site Tools


Action disabled: register
programming:python:albumcoverdl

Album Art Cover Downloader

Overview

This is just a simple script that will recursively download album art for your mp3 collection stored in an Artist/Album folder structure, or at the very least …/Album folder structure. This script uses Amazon and it's product advertising api to get the art. The script will recurse through your folder(s) and add a folder.jpg file at every leaf directory that it finds mp3s in. Most players are smart enough to use this file for the album art.

Requirements

The only requirement to run this script is that you download and install the Python Amazon product api. That, in turn, requires the Python lxml library. If you use the easy_install method (or have that installed) when you install the amazon product api, it will automatically pull in the lxml library for you.

The Script

The script itself is actually quite simple since all the heavy lifting is done by the Amazon product api library. Make sure you change the API_KEY, and SECRET_KEY to whatever your key values are that you obtained from Amazon. If you do not have a key pair, you can get them for free here. You will also want to set your own BASEDIRS to the directories where you have your music. Once you have that all done, just run the script: python getcovers.py.

#!/usr/bin/env python
 
from amazonproduct import API
from urllib2 import urlopen
import os.path
import sys
import re
 
API_KEY = '--- PUT YOUR KEY HERE ---'
SECRET_KEY = '--- PUT YOUR SECRET KEY HERE ---'
SEARCH_GROUP = 'Music'
BASEDIRS = ('/files/mp3s/', '/files/othermp3s')
OUTNAME = 'folder.jpg'
REPL_REGS = (re.compile('\s*\(.*?\)\s*') , re.compile('\s*Dis[ck].*$'))
 
def getArtistAlb(d):
    return tuple(d.split('/')[-2:])
 
def getImage(api , d):
    artist , album = getArtistAlb(d)
    for r in REPL_REGS:
        album = r.sub('' , album)
    try:
        node = api.item_search(SEARCH_GROUP , ResponseGroup='Images' , 
            Keywords='%s %s' % (artist , album))
    except Exception , e:
        # try just the album
        node = api.item_search(SEARCH_GROUP , ResponseGroup='Images' , 
            Keywords='%s' % album)
    outfile = os.path.join(d , OUTNAME)
    try:
        url = str(node.Items.Item.LargeImage.URL)
    except:
        url = str(node.Items.Item.ImageSets.ImageSet.LargeImage.URL)
    fh = urlopen(url)
    outh = open(outfile , 'w')
    outh.write(fh.read())
    fh.close()
    outh.close()
    return (artist , album)
 
def main():
    api = API(API_KEY , SECRET_KEY)
    for bd in BASEDIRS:
        for root , dirs , files in os.walk(bd):
            if not dirs:
                # We should be in a leaf dir
                jExists = False
                for f in files:
                    ext = os.path.splitext(f)[1]
                    if ext.lower() == '.jpg':
                        jExists = True
                        break
                if not jExists:
                    try:
                        artist , album = getImage(api , root)
                    except Exception , e:
                        print >> sys.stderr , \
                            'Error getting image for path %s: %s' % (root , e)
                        continue
                    print 'Got image for "%s: %s"' % (artist , album)
 
if __name__ == '__main__':
    main()

You can also download this script here. This is a bit of a quick and dirty 10 minute script. I will probably add some command-line opts later when I have time, but for now it functions well.

Problems

If you run into any problems, or you have any questions, please email me at admin@splitstreams.com

programming/python/albumcoverdl.txt · Last modified: 2010/05/11 20:59 by crustymonkey