Fetching cover art for a list of albums

This Python script will fetch the cover images for all albums with ASINs in a MySQL table and write them to a folder (see the earlier script for looking up data from MusicBrainz).

#!/usr/bin/env python
import sys, time, MySQLdb, urllib, os
from path import path
db = MySQLdb.connect(db="amarok", user="YOUR_DB_USER", passwd="YOUR_DB_PASSWORD", use_unicode=True, charset="utf8")
c = db.cursor()
c.execute("""SELECT asin FROM `musicbrainz` WHERE asin IS NOT NULL""")
print "%d rows" % c.rowcount
for asin in c.fetchall():  
  imagefile = "images/%s.jpg" % asin
  if not os.path.exists(imagefile):
    print asin
    image = urllib.urlopen("http://images.amazon.com/images/P/%s.01._SCLZZZZZZZ_PU_PU-5_.jpg" % asin).read()
    if (image):
      out = open(imagefile, 'w')
      out.write(image)
      out.close()
    else:
      path.touch(imagefile)
c.close()
db.close()