[Muine] musicbrainz patch v2



Revised the patch to be a little smarter, now it goes through the list
of returned albums and picks just the right one.
Of course you still need MusicBrainz.cs in the src dir.

Yi
diff -ruN muine-0.8.3.orig/src/CoverGetter.cs muine-0.8.3/src/CoverGetter.cs
--- muine-0.8.3.orig/src/CoverGetter.cs	2005-11-20 13:18:12.000000000 -0800
+++ muine-0.8.3/src/CoverGetter.cs	2005-11-23 12:43:28.000000000 -0800
@@ -24,7 +24,7 @@
 using System.IO;
 using System.Threading;
 using System.Collections;
-
+using musicbrainz;
 using Gdk;
 
 namespace Muine
@@ -277,157 +277,43 @@
 		/// </returns>
 		public Pixbuf DownloadFromAmazon (Album album)
 		{
-			Amazon.AmazonSearchService search_service = new Amazon.AmazonSearchService ();
-
 			string sane_album_title = SanitizeString (album.Name);
-
+            string [] artists = album.Artists;
+            string asin = null;
+            string AmazonImageUri = "http://images.amazon.com/images/P/{0}.01._SCLZZZZZZZ_.jpg";;
 			// remove "disc 1" and family
 			//	TODO: Make the regex translatable? (e.g. "uno|dos|tres...")
 			sane_album_title =  Regex.Replace (sane_album_title, 
 				@"[,:]?\s*(cd|dis[ck])\s*(\d+|one|two|three|four|five|six|seven|eight|nine|ten)\s*$", "");
 
-			string [] album_title_array = sane_album_title.Split (' ');
-			Array.Sort (album_title_array);
-
-			// This assumes the right artist is always in Artists [0]
-			string sane_artist = SanitizeString (album.Artists [0]);
-			
-			// Prepare for handling multi-page results
-			int total_pages = 1;
-			int current_page = 1;
-			int max_pages = 2; // check no more than 2 pages
-			
-			// Create Encapsulated Request
-			Amazon.ArtistRequest asearch = new Amazon.ArtistRequest ();
-			asearch.devtag = "INSERT DEV TAG HERE"; // TODO: We really should have a devtag
-			asearch.artist = sane_artist;
-			asearch.keywords = sane_album_title;
-			asearch.type = "heavy";
-			asearch.mode = "music";
-			asearch.tag = "webservices-20";
-
-			// Use selected Amazon service
-			switch (amazon_locale) {
-			case "uk":
-				search_service.Url = "http://soap-eu.amazon.com/onca/soap3";;
-				asearch.locale = "uk";
-				break;
-
-			case "de":
-				search_service.Url = "http://soap-eu.amazon.com/onca/soap3";;
-				asearch.locale = "de";
-				break;
-
-			case "jp":
-				search_service.Url = "http://soap.amazon.com/onca/soap3";;
-				asearch.locale = "jp";
-				break;
-
-			default:
-				search_service.Url = "http://soap.amazon.com/onca/soap3";;
-				break;
-			}
-
-			double best_match_percent = 0.0;
-			Pixbuf best_match = null;
-
-			while (current_page <= total_pages && current_page <= max_pages) {
-			       asearch.page = Convert.ToString (current_page);
-
-				Amazon.ProductInfo pi;
-				
-				// Amazon API requires this
-				Thread.Sleep (1000);
-			
-				// Web service calls timeout after 30 seconds
-				search_service.Timeout = 30000;
-				if (proxy.Use)
-					search_service.Proxy = proxy.Proxy;
-				
-				// This may throw an exception, we catch it in the calling function
-				pi = search_service.ArtistSearchRequest (asearch);
-
-				int num_results = pi.Details.Length;
-				total_pages = Convert.ToInt32 (pi.TotalPages);
-
-				// Work out how many matches are on this page
-				if (num_results < 1)
-					return null;
-
-				for (int i = 0; i < num_results; i++) {
-					// Ignore bracketed text on the result from Amazon
-					string sane_product_name = SanitizeString (pi.Details[i].ProductName);
-
-					// Compare the two strings statistically
-					string [] product_name_array = sane_product_name.Split (' ');
-					Array.Sort (product_name_array);
-
-					int match_count = 0;
-					foreach (string s in album_title_array) {
-						if (Array.BinarySearch (product_name_array, s) < 0)
-							continue;
-
-						match_count++;
-					}
-
-					double match_percent;
-					match_percent = match_count / (double) album_title_array.Length;
-
-					if (match_percent < 0.6)
-						continue;
-
-					string url = pi.Details [i].ImageUrlMedium;
-
-					if (url == null || url.Length == 0)
-						continue;
-
-					double backward_match_percent = 0.0;
-					int backward_match_count = 0;
-
-					foreach (string s in product_name_array) {
-						if (Array.BinarySearch (album_title_array, s) < 0)
-							continue;
-
-						backward_match_count++;
-					}
-
-					backward_match_percent = backward_match_count / (double) product_name_array.Length;
-
-					double total_match_percent = match_percent + backward_match_percent;
-					if (total_match_percent <= best_match_percent)
-						continue; // look for a better match
-						
-					Pixbuf pix;
-								
-					try {
-						pix = Download (url);
-						if (pix == null && amazon_locale != "us") {
-							// Manipulate the image URL since Amazon sometimes return it wrong :(
-							// http://www.amazon.com/gp/browse.html/103-1953981-2427826?node=3434651#misc-image
-							url = Regex.Replace (url, "[.]0[0-9][.]", ".01.");
-							pix = Download (url);
-						}
-
-					} catch (WebException e) {
-						throw e;
-
-					} catch (Exception e) {
-						pix = null;
-					}
-
-					if (pix != null) {
-						best_match_percent = total_match_percent;
-						best_match = pix;
-
-						if (best_match_percent == 2.0)
-							return best_match;
-					}
-				}
-
-				current_page++;
-			}
-
-			return best_match;
+            MusicBrainz c = new MusicBrainz();
+            
+            // set the depth of the query (see http://wiki.musicbrainz.org/ClientHOWTO)
+            c.SetDepth(4);
+            string [] album_name = {sane_album_title};
+            if(c.Query(MusicBrainz.MBQ_FindAlbumByName, album_name)) {
+                int num_albums = c.GetResultInt(MusicBrainz.MBE_GetNumAlbums);
+
+                string artist_name;
+                for (int i = 1; i <= num_albums; i++) {
+                    c.Select(MusicBrainz.MBS_SelectAlbum, i);
+                    // gets the artist from the first track of the album
+                    c.GetResultData(MusicBrainz.MBE_AlbumGetArtistName, 1,  out artist_name);
+                    if(artist_name == artists[0]) {
+                        c.GetResultData(MusicBrainz.MBE_AlbumGetAmazonAsin, out asin);
+                        break;
+                    }
+                    c.Select(MusicBrainz.MBS_Back); // go back one level so we can select the next album
+                }
+            }
+                
+            // If we don't find a suitable asin...
+            if(asin==null) {
+               return null;
+            } 
+       
+            return Download(String.Format(AmazonImageUri,asin));
+					
 		}
 
 		// Methods :: Public :: Download
@@ -479,7 +365,7 @@
 
 			return cover;
 		}
-
+        
 		// Methods :: Public :: AddBorder
 		/// <summary>
 		///	Scale the image and add a black 1 pixel border to it.
diff -ruN muine-0.8.3.orig/src/Makefile.in muine-0.8.3/src/Makefile.in
--- muine-0.8.3.orig/src/Makefile.in	2005-11-20 13:18:12.000000000 -0800
+++ muine-0.8.3/src/Makefile.in	2005-11-23 12:42:52.000000000 -0800
@@ -276,7 +276,8 @@
 	$(srcdir)/Actions.cs			\
 	$(srcdir)/ImportDialog.cs		\
 	$(srcdir)/OpenDialog.cs			\
-	$(srcdir)/SaveDialog.cs
+	$(srcdir)/SaveDialog.cs			\
+	$(srcdir)/MusicBrainz.cs
 
 
 MUINE_GENERATED_CSFILES = \


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]