Index: plugins/artdisplay/artdisplay.py
===================================================================
RCS file: /cvs/gnome/rhythmbox/plugins/artdisplay/artdisplay.py,v
retrieving revision 1.2
diff -u -r1.2 artdisplay.py
--- plugins/artdisplay/artdisplay.py	20 Apr 2006 20:43:24 -0000	1.2
+++ plugins/artdisplay/artdisplay.py	27 Apr 2006 09:32:53 -0000
@@ -22,15 +22,15 @@
 from xml.dom import minidom
 import rhythmdb, rb
 import gobject, gtk
-import os
+import os, re
 import urllib
 
 LICENSE_KEY = "18C3VZN9HCECM5G3HQG2"
-LOCALE = "uk"
+LOCALE = "us"
 ASSOCIATE = "webservices-20"
 
 _supportedLocales = {
-		"us" : (None, "xml.amazon.com"),
+		"us" : ("us", "xml.amazon.com"),
 		"uk" : ("uk", "xml-eu.amazon.com"),
 		"de" : ("de", "xml-eu.amazon.com"),
 		"jp" : ("jp", "xml.amazon.co.jp")
@@ -70,6 +70,10 @@
 		self.art_widget.disconnect(self.sa_id)
 		self.art_widget = None
 		self.current_pixbuf = None
+
+		self.action = None
+		self.action_group = None
+
 		if self.resize_id != 0:
 			gobject.source_remove(self.resize_id)
 
@@ -104,7 +108,8 @@
 
 	def update_displayed_art(self, quick):
 		if self.current_pixbuf is None:
-			self.art_widget.clear()
+			# don't use  Image.clear(), not pygtk 2.6-compatible
+			self.art_widget.set_from_pixbuf(None)
 			self.art_widget.hide()
 		else:
 			width = self.art_widget.parent.allocation.width 
@@ -126,13 +131,18 @@
 		db = self.shell.get_property("db")
 		st_artist = db.entry_get(entry, rhythmdb.PROP_ARTIST)
 		st_album = db.entry_get(entry, rhythmdb.PROP_ALBUM)
-		
-		# the following block of code is messy and needs to be redone ASAP
+
+		# FIXME: the following block of code is messy and needs to be redone ASAP
 		art_location = art_folder + '/%s - %s.jpg' % (st_artist.replace('/', '-'), st_album.replace('/', '-'))
 		blist_location = art_folder + '/%s - %s.rb-blist' % (st_artist.replace('/', '-'), st_album.replace('/', '-'))
+		# remove variants of Disc/Volume/CD [1-9] from album title before search
+		for exp in ["\([Dd]isc *[1-9]+\)", "\([Vv]olume *[1-9]+\)", "\([Cc][Dd] *[1-9]+\)"]:
+			p = re.compile(exp)
+			st_album = p.sub('', st_album)
+			##KEYWORD = KEYWORD.replace(term, '')
+
 		KEYWORD = "%s %s" % (st_artist, st_album)
-		for term in ["(Disc 1)", "(Disc 2)", "(Volume 1)"]:
-			KEYWORD = KEYWORD.replace(term, '')
+		KEYWORD_VARIOUS = "Various %s" % (st_album)
 			
 		if os.path.exists(art_location):
 			pixbuf = gtk.gdk.pixbuf_new_from_file(art_location)
@@ -140,59 +150,80 @@
 			pixbuf = None
 		else:
 			try:
-				pic_url = self.search(KEYWORD.strip())[0].ImageUrlLarge
-				url_file = urllib.urlopen(pic_url)
-				if int(url_file.info()['Content-Length']) < 1000:
-					url_file.close()
+				search_results = self.search(KEYWORD.strip())
+				pixbuf = self.getPixBuf(search_results, art_location, blist_location, st_artist, st_album)
+			except AmazonError:
+				## look for various artist (compilation)
+				try:
+					search_results = self.search(KEYWORD_VARIOUS.strip())
+					pixbuf = self.getPixBuf(search_results, art_location, blist_location, st_artist, st_album)
+				except:
 					f = file(blist_location, 'w')
 					f.close()
-					pixbuf = None	
-				else:
-					f = file(art_location, 'wb')
-					f.write(url_file.read())
-					f.close()
-					url_file.close()		
-					pixbuf = gtk.gdk.pixbuf_new_from_file(art_location)
-			except AmazonError:
-				f = file(blist_location, 'w')
-				f.close()
-				pixbuf = None			
+					pixbuf = None
+				
+		return pixbuf
+
+	def getPixBuf(self, search_results, art_location, blist_location, artist, album):
+		# default to the first match as a fallback
+		best_match = search_results[0]
+		# search within the results to find the album name match
+		for item in search_results:
+			# make album title case insensitive and strip
+			# leading and trailing space
+			if item.ProductName.lower().strip() == album.lower().strip():
+				best_match = item
+				break
+		pic_url = best_match.ImageUrlLarge
+		url_file = urllib.urlopen(pic_url)
+		if int(url_file.info()['Content-Length']) < 1000:
+			url_file.close()
+			f = file(blist_location, 'w')
+			f.close()
+			pixbuf = None	
+		else:
+			f = file(art_location, 'wb')
+			f.write(url_file.read())
+			f.close()
+			url_file.close()		
+			pixbuf = gtk.gdk.pixbuf_new_from_file(art_location)
 		return pixbuf
+
 	
 	# Amazon part starts here - taken from pyamazon and edited
 	
-	def unmarshal(self, element):
-		rc = Bag()
-		if isinstance(element, minidom.Element) and (element.tagName == 'Details'):
-			rc.URL = element.attributes["url"].value
-		childElements = [e for e in element.childNodes if isinstance(e, minidom.Element)]
-		if childElements:
-			for child in childElements:
-				key = child.tagName
-				if hasattr(rc, key):
-					if type(getattr(rc, key)) <> type([]):
-						setattr(rc, key, [getattr(rc, key)])
-					setattr(rc, key, getattr(rc, key) + [self.unmarshal(child)])
-				elif isinstance(child, minidom.Element) and (child.tagName == 'Details'):
-					setattr(rc,key,[self.unmarshal(child)])
-				else:
-					setattr(rc, key, self.unmarshal(child))
-		else:
-			rc = "".join([e.data for e in element.childNodes if isinstance(e, minidom.Text)])
-			if element.tagName == 'SalesRank':
-				rc = rc.replace('.', '')
-				rc = rc.replace(',', '')
-				rc = int(rc)
+	def unmarshal(self, element):
+		rc = Bag()
+		if isinstance(element, minidom.Element) and (element.tagName == 'Details'):
+			rc.URL = element.attributes["url"].value
+		childElements = [e for e in element.childNodes if isinstance(e, minidom.Element)]
+		if childElements:
+			for child in childElements:
+				key = child.tagName
+				if hasattr(rc, key):
+					if type(getattr(rc, key)) <> type([]):
+						setattr(rc, key, [getattr(rc, key)])
+					setattr(rc, key, getattr(rc, key) + [self.unmarshal(child)])
+				elif isinstance(child, minidom.Element) and (child.tagName == 'Details'):
+					setattr(rc,key,[self.unmarshal(child)])
+				else:
+					setattr(rc, key, self.unmarshal(child))
+		else:
+			rc = "".join([e.data for e in element.childNodes if isinstance(e, minidom.Text)])
+			if element.tagName == 'SalesRank':
+				rc = rc.replace('.', '')
+				rc = rc.replace(',', '')
+				rc = int(rc)
 		return rc
 	
-	def buildURL(self, keyword):
-		url = "http://" + _supportedLocales[LOCALE][1] + "/onca/xml3?f=xml"
-		url += "&t=%s" % ASSOCIATE
-		url += "&dev-t=%s" % LICENSE_KEY
-		url += "&type=%s" % 'lite'
-		url += "&locale=%s" % _supportedLocales[LOCALE][0]
-		url += "&mode=%s" % 'music'
-		url += "&%s=%s" % ('KeywordSearch', urllib.quote(keyword))
+	def buildURL(self, keyword):
+		url = "http://" + _supportedLocales[LOCALE][1] + "/onca/xml3?f=xml"
+		url += "&t=%s" % ASSOCIATE
+		url += "&dev-t=%s" % LICENSE_KEY
+		url += "&type=%s" % 'lite'
+		url += "&locale=%s" % _supportedLocales[LOCALE][0]
+		url += "&mode=%s" % 'music'
+		url += "&%s=%s" % ('KeywordSearch', urllib.quote(keyword))
 		return url
 
 	def search(self, keyword):
