Index: plugins/artdisplay/artdisplay.py
===================================================================
RCS file: /cvs/gnome/rhythmbox/plugins/artdisplay/artdisplay.py,v
retrieving revision 1.4
diff -u -r1.4 artdisplay.py
--- plugins/artdisplay/artdisplay.py	3 May 2006 12:11:09 -0000	1.4
+++ plugins/artdisplay/artdisplay.py	4 May 2006 08:21:02 -0000
@@ -142,17 +144,27 @@
 		else:
 			# Otherwise spawn (online) search-engine search
 			if entry != self.search_engine.entry:
+				self.blist = False
 				self.on_get_pixbuf_completed = on_get_pixbuf_completed
 				self.search_engine.search (db, entry, self.on_search_engine_results)
 
 	def on_search_engine_results (self, db, entry, results):
 		if results is None:
+			if self.blist == True:
+				blist_location = self.build_art_cache_filename(self.search_engine.st_album, self.search_engine.st_artist, "rb-blist")
+				f = file (blist_location, 'w')
+				f.close ()
+
 			self.on_get_pixbuf_completed (db, entry, None)
 			return
 
 		# Get best match from results
 		self.best_match = self.search_engine.get_best_match (results)
 
+		if self.best_match is None:
+			self.search_engine.search_next()
+			return
+
 		# Attempt to download image for best match
 		pic_url = str(self.best_match.ImageUrlLarge)
 		self.image_version = "large";
@@ -170,12 +182,11 @@
 				pic_url = str(self.best_match.ImageUrlMedium)
 				self.image_loader.get_url (pic_url, self.on_image_data_received)
 				return
-			blist_location = self.build_art_cache_filename(self.search_engine.st_album, self.search_engine.st_artist, "rb-list")
-			f = file (blist_location, 'w')
-			f.close ()
+			self.blist = True
 			self.search_engine.search_next()
 		else:
 			art_location = self.build_art_cache_filename(self.search_engine.st_album, self.search_engine.st_artist, "jpg")
+			self.blist = False
 			f = file (art_location, 'wb')
 			f.write (image_data)
 			f.close ()
@@ -187,7 +198,6 @@
 class AmazonCoverArtSearch (object):
 	def __init__(self, loader):
 		self.searching = False
-		self.cancel = False
 		self.loader = loader
 		self._supportedLocales = {
 			"en_US" : ("us", "xml.amazon.com"),
@@ -227,7 +237,6 @@
 			st_artist = st_artist.replace (char, '')
 			st_album = st_album.replace (char, '')
 
-		# Save current search's entry properties
 		self.st_album = st_album
 		self.st_artist = st_artist
 
@@ -241,7 +250,10 @@
 			p = re.compile (exp)
 			st_album_no_vol = p.sub ('', st_album_no_vol)
 
-		self.st_album_no_vol = st_album_no_vol
+		# Save current search's entry properties
+		self.search_album = st_album
+		self.search_artist = st_artist
+		self.search_album_no_vol = st_album_no_vol
 
 		# TODO: Improve to decrease wrong cover downloads, maybe add severity?
 		# Assemble list of search keywords (and thus search queries)
@@ -257,10 +269,11 @@
 				self.keywords.append ("%s" % (st_album_no_vol))
 			self.keywords.append ("Various %s" % (st_album))
 		else:
-			self.keywords.append ("%s %s" % (st_artist, st_album))
-			if st_album_no_vol != st_artist:
-				self.keywords.append ("%s %s" % (st_artist, st_album_no_vol))
-			self.keywords.append ("Various %s" % (st_album))
+			if st_album != st_artist:
+				self.keywords.append ("%s %s" % (st_artist, st_album))
+				if st_album_no_vol != st_artist:
+					self.keywords.append ("%s %s" % (st_artist, st_album_no_vol))
+				self.keywords.append ("%s" % (st_album))
 			self.keywords.append ("%s" % (st_artist))
 
 		# Initiate asynchronous search
@@ -346,20 +359,57 @@
 		self.on_search_completed_callback (self.db, self.entry, result)
 		self.searching = False
 
-	# TODO: Better overall matching algo - "==" is inaccurate on minor differences
+	def __tidy_up_string (self, s):
+		# Lowercase
+		s = s.lower ()
+		# Strip
+		s = s.strip ()
+
+		# TODO: Convert accented to unaccented (fixes matching Salomé vs Salome)
+		s = s.replace (" - "," ")	
+		s = s.replace (": "," ")
+
+		return s
+
+	# TODO: Improve. Add approximate search with matching weight
 	def get_best_match (self, search_results):
-		# default to the first match as a fallback
-		best_match = search_results[0]
+		# Default to "no match", our results must match our criteria
+		best_match = None
 
 		# 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 () == self.st_album.lower ().strip ():
-				best_match = item
-				break
-
-		# TODO: Also validate <Artist/> tag if it matched search
+		if self.search_album!="Unknown":
+			album_check = self.__tidy_up_string(self.search_album)
+			for item in search_results:
+			# Check for album name in ProductName
+				product_name = self.__tidy_up_string(item.ProductName)
+				if product_name == album_check:
+					# Found exact album, can not get better than that
+					best_match = item
+					break
+				# If we already found a best_match, just keep checking for exact one
+				elif (best_match is None) and (product_name.find (album_check) != -1):
+					best_match = item
+
+		# If we still have no definite hit, use first result where artist matches
+		if (self.search_artist!="Unknown"):
+			artist_check = self.__tidy_up_string(self.search_artist)
+			if best_match is None:
+				# Check if artist appears in the Artists list
+				hit = False
+				for item in search_results:
+					if type(item.Artists.Artist) <> type([]):
+						artists = [item.Artists.Artist]
+					else:
+						artists = item.Artists.Artist
+
+					for artist in artists:
+						artist = self.__tidy_up_string(artist)
+						if artist.find(artist_check) != -1:
+							best_match = item
+							hit = True						
+							break
+					if hit:
+						break
 
 		return best_match
 
