[maemo-commits] [maemo-commits] r16256 - projects/haf/branches/hildon-thumbnail/daemonize/tests

From: subversion at stage.maemo.org subversion at stage.maemo.org
Date: Mon Sep 29 13:22:27 EEST 2008
Author: pvanhoof
Date: 2008-09-29 13:22:23 +0300 (Mon, 29 Sep 2008)
New Revision: 16256

Modified:
   projects/haf/branches/hildon-thumbnail/daemonize/tests/google-images-art-downloader.vala
Log:
Some various fixes

Modified: projects/haf/branches/hildon-thumbnail/daemonize/tests/google-images-art-downloader.vala
===================================================================
--- projects/haf/branches/hildon-thumbnail/daemonize/tests/google-images-art-downloader.vala	2008-09-29 10:10:54 UTC (rev 16255)
+++ projects/haf/branches/hildon-thumbnail/daemonize/tests/google-images-art-downloader.vala	2008-09-29 10:22:23 UTC (rev 16256)
@@ -1,17 +1,25 @@
 using GLib;
 
+// This is the interface on DBus that you must implement
+
 [DBus (name = "com.nokia.albumart.Provider")]
 public interface Provider {
 	public abstract void Fetch (string artistalbum, string uri);
 }
 
+// Sample implementation of com.nokia.albumart.Provider that uses Google 
+// images's first result as album-art cover. There is of course no certainty 
+// that the first result on Google images is indeed the album's cover. But it's
+// a good estimate nonetheless.
+
 public class GoogleImages : Object, Provider {
+
 	public void Fetch (string artistalbum, string uri) {
 		uint u = 0, hread = 0;
 		string [] pieces = artistalbum.split (" ", -1);
 		string stitched = "";
 
-		print ("Fetch " + artistalbum + " " + uri + "\n");
+		// Convert the album-artist into something that will work for Google images
 
 		while (pieces[u] != null) {
 			if (u != 0)
@@ -20,19 +28,28 @@
 			u++;
 		}
 
+		// Start the query on Google images
+
 		File google_search = File.new_for_uri ("http://images.google.com/images?q=" + stitched);
+
 		try {
 			char [] buffer = new char [40000];
 			string asstring;
+
+			// Fetch the first page
+
 			InputStream stream = google_search.read (null);
-			if (stream != null)
-				stream.read_all (buffer, 40000, out hread, null);
+			stream.read_all (buffer, 40000, out hread, null);
 			buffer[hread] = 0;
 
 			asstring = (string) buffer;
+
+			// Find the first result
+
 			string found = asstring.str ("http://tbn0.google.com/images?q=tbn");
 
 			if (found != null) {
+
 				StringBuilder url = new StringBuilder ();
 				long y = found.len();
 				int i = 0;
@@ -45,19 +62,28 @@
 				string cache_path;
 
 				string cache_dir = Path.build_filename (Environment.get_home_dir(),
-												  ".album_art",
-												  null);
+								  ".album_art",
+								  null);
 
+				// Define cache path = ~/.album_art/MD5 (down (albumartist)).jpeg
+
 				cache_path = Path.build_filename (Environment.get_home_dir(),
-												  ".album_art",
-												  Checksum.compute_for_string (ChecksumType.MD5, artistalbum.down () + ".jpeg", -1),
-												  null);
+								  ".album_art",
+								  Checksum.compute_for_string (
+															   ChecksumType.MD5, 
+															   artistalbum.down () + ".jpeg", 
+															   -1),
+								  null);
 
+				// Make sure the directory .album_arts is available
+
 				DirUtils.create_with_parents (cache_dir, 0770);
 
 				File online_image = File.new_for_uri (url.str);
 				File cache_image = File.new_for_path (cache_path);
 
+				// Copy from Google images to local cache
+
 				online_image.copy (cache_image, 
 								   FileCopyFlags.NONE, 
 								   null, 
@@ -80,9 +106,11 @@
 			"org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus");
 
 		// try to register service in session bus
+
 		uint request_name_result = bus.request_name ("com.nokia.albumart.GoogleImages", (uint) 0);
 
 		if (request_name_result == DBus.RequestNameReply.PRIMARY_OWNER) {
+
 			// start server
 
 			var server = new GoogleImages ();


More information about the maemo-commits mailing list