====== 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 [[http://pypi.python.org/pypi/python-amazon-product-api/0.2.1|Python Amazon product api]]. That, in turn, requires the [[http://pypi.python.org/pypi/lxml/|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 [[https://www.amazon.com/ap/signin?openid.ns=http://specs.openid.net/auth/2.0&authCookies=1&openid.mode=checkid_setup&openid.identity=http://specs.openid.net/auth/2.0/identifier_select&openid.claimed_id=http://specs.openid.net/auth/2.0/identifier_select&openid.pape.max_auth_age=600&openid.return_to=https://www.amazon.com/gp/aws/ssop/handlers/auth-portal.html%3Fie%3DUTF8%26wreply%3Dhttps%253A%252F%252Faws-portal.amazon.com%252Fgp%252Faws%252Fdeveloper%252Fregistration%252Findex.html%26awsrequestchallenge%3Dfalse%26wtrealm%3Durn%253Aaws%253AawsAccessKeyId%253A1QQFCEAYKJXP0J7S2T02%26wctx%3D%26awsaccountstatuspolicy%3DP1%26wa%3Dwsignin1.0%26awsrequesttfa%3Dtrue&openid.assoc_handle=ssop&openid.pape.preferred_auth_policies=http://schemas.openid.net/pape/policies/2007/06/multi-factor-physical&openid.ns.pape=http://specs.openid.net/extensions/pape/1.0&accountStatusPolicy=P1&siteState=awsMode::signUp::&|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 {{:programming:python:getcovers.py.gz|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