All pastes #176269 Raw Edit

Miscellany

public python v1 · immutable
#176269 ·published 2006-09-19 15:10 UTC
rendered paste body
#! /usr/bin/env python# original script from http://printf.dk/itunes_7_fetching_artwork_for.html# and http://clintecker.com/i7AWF.pyimport urllib2, urllibfrom gzip import GzipFilefrom StringIO import StringIOfrom xml.dom import minidomclass i7AWF:    """    Retreive album artwork from the iTunes Music Store    Adapted from Jesper Noehr's Perl script @ printf.dk    Clint Ecker <me@clintecker.com>    Jesper Noehr <jesper@noehr.org>    Version 0.1 - Licensed under the Creative Commons share-alike license.        Usage:        from i7AWF import i7AWF    i = i7AWF()    i.artist = "Gnarls Barkley"    i.album = "St. Elsewhere"    url = i.payload()        """    itunes7_useragent = "iTunes/7.0 (Macintosh; U; PPC Mac OS X 10.4.7)"    itunes7_url = "http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZSearch.woa/wa/coverArtMatch?an=%s&pn=%s"    itunes_ns = "http://www.apple.com/itms/"    itunes7_store = 143457        def __init__(self):        self.artist = None        self.album = None        def payload(self):        if self.artist and self.album:            ## Quote our inputs            self.artist = urllib.quote(self.artist.encode('UTF-8', 'replace'))            self.album  = urllib.quote(self.album.encode('UTF-8', 'replace'))            ## Set up HTTP request            url = self.itunes7_url % (self.artist, self.album)            headers = {              "X-Apple-Tz" : "7200",              "X-Apple-Store-Front" : self.itunes7_store,              "Accept-Language" : "en-us, en;q=0.50",              "Accept-Encoding" : "gzip, x-aes-cbc",              "Connection" : "close",              "Host" : "ax.phobos.apple.com.edgesuite.net"            }                        ## Build Opener with Request            request = urllib2.Request(url, None, headers)              opener = urllib2.build_opener()                        ## Spoof UA            opener.addheaders = [('User-agent', self.itunes7_useragent)]                              ## Get compressed XML file            compressed = StringIO()                fp = opener.open(request)            r = fp.read()            while r:                compressed.write(r)                r = fp.read()            compressed.seek(0,0)            gz = GzipFile(fileobj = compressed)            s = ''            r = gz.read()            while r:                s += r                r = gz.read()            # close our utility files            compressed.close()            gz.close()            ## Parse XML file            dom = minidom.parse(StringIO(s))            image_url = dom.getElementsByTagNameNS(self.itunes_ns, 'string')[0]            if image_url and image_url.firstChild and image_url.firstChild.data:                return image_url.firstChild.data            else:                return False  def getText(node):    rc = ""    nodelist = node.childNodes    for node in nodelist:        if node.nodeType == node.TEXT_NODE:            rc = rc + node.data    return rcdom = minidom.parse('test.xml')entries = dom.getElementsByTagName("entry")albums = {}for entry in entries:	title  = entry.getElementsByTagName('title')[0]	artist = entry.getElementsByTagName('artist')[0]	album  = entry.getElementsByTagName('album')[0]	albums[getText(album)] = getText(artist)itunes = i7AWF()for album in albums:	itunes.artist = albums[album]	itunes.album  = album	name = itunes.artist + " - " + itunes.album + ".jpg"	print "Finding cover for album " + name	image = itunes.payload()	if image:		print "Downloading " + image		urllib.urlretrieve(image, name)	else:		print "Nothing, moving on"	print "\n"