[gnome-music/wip/process-media-art: 2/2] albumArtCache: Convert downloaded media covers to JPEG



commit 42cfb20836c9f11b82b40d8f83976954776fc0bf
Author: Arnel A. Borja <arnelborja src gnome org>
Date:   Wed Apr 16 23:02:53 2014 +0800

    albumArtCache: Convert downloaded media covers to JPEG
    
    Use libmediaart to save the media covers so that they will be converted
    to JPEG.

 gnomemusic/albumArtCache.py |   41 ++++++++++++++++++++++++++++++++++-------
 gnomemusic/grilo.py         |   13 ++++++++-----
 2 files changed, 42 insertions(+), 12 deletions(-)
---
diff --git a/gnomemusic/albumArtCache.py b/gnomemusic/albumArtCache.py
index 40c738b..8d59752 100644
--- a/gnomemusic/albumArtCache.py
+++ b/gnomemusic/albumArtCache.py
@@ -28,7 +28,7 @@
 # delete this exception statement from your version.
 
 
-from gi.repository import Gtk, GdkPixbuf, Gio, GLib, Gdk, MediaArt
+from gi.repository import Gtk, GdkPixbuf, Gio, GLib, Gdk, MediaArt, Grl
 from gettext import gettext as _
 import cairo
 from math import pi
@@ -151,8 +151,9 @@ class AlbumArtCache:
             height = height or -1
             path = MediaArt.get_path(artist, album, "album", None)[0]
             if not os.path.exists(path):
-                self.cached_thumb_not_found(item, album, artist, path, callback, itr)
-            self.read_cached_pixbuf(path, width, height, callback, itr)
+                self.cached_thumb_not_found(item, width, height, callback, itr, artist, album, path)
+            else:
+                self.read_cached_pixbuf(path, width, height, callback, itr)
         except Exception as e:
             logger.warn("Error: %s" % e)
 
@@ -172,7 +173,7 @@ class AlbumArtCache:
             logger.warn("Error: %s" % e)
 
     @log
-    def cached_thumb_not_found(self, item, album, artist, path, callback, itr):
+    def cached_thumb_not_found(self, item, width, height, callback, itr, artist, album, path):
         try:
             uri = item.get_thumbnail()
             if uri is None:
@@ -183,8 +184,34 @@ class AlbumArtCache:
                     self.finish(None, path, callback, itr)
                     return
 
-            src = Gio.File.new_for_uri(uri)
-            dest = Gio.File.new_for_path(path)
-            src.copy(dest, Gio.FileCopyFlags.OVERWRITE)
+            data = [item, width, height, callback, itr, artist, album, path, uri]
+            if item.get_url() is None:
+                # No URL, probably an album. Get a single song.
+                grilo.populate_album_songs(item.get_id(), self._on_album_item_found, 1, data)
+            else:
+                f = Gio.File.new_for_uri(uri)
+                f.load_contents_async(None, self._on_load_contents_finished, data)
+        except Exception as e:
+            logger.warn("Error: %s" % e)
+
+    @log
+    def _on_album_item_found(self, source, param, item, remaining, data):
+        try:
+            item_album, width, height, callback, itr, artist, album, path, uri = data
+            data[0] = item
+
+            f = Gio.File.new_for_uri(uri)
+            f.load_contents_async(None, self._on_load_contents_finished, data)
+        except Exception as e:
+            logger.warn("Error: %s" % e)
+
+    @log
+    def _on_load_contents_finished(self, f, res, data):
+        try:
+            success, contents, etag = f.load_contents_finish(res)
+            item, width, height, callback, itr, artist, album, path, uri = data
+            MediaArt.process(contents, 'image/png', MediaArt.Type.ALBUM,
+                             artist, album, item.get_url())
+            self.read_cached_pixbuf(path, width, height, callback, itr)
         except Exception as e:
             logger.warn("Error: %s" % e)
diff --git a/gnomemusic/grilo.py b/gnomemusic/grilo.py
index d3ff075..202aef9 100644
--- a/gnomemusic/grilo.py
+++ b/gnomemusic/grilo.py
@@ -158,19 +158,22 @@ class Grilo(GObject.GObject):
         self.populate_items(Query.SONGS, offset, callback, count)
 
     @log
-    def populate_album_songs(self, album_id, callback, count=-1):
-        self.populate_items(Query.album_songs(album_id), 0, callback, count)
+    def populate_album_songs(self, album_id, callback, count=-1, data=None):
+        self.populate_items(Query.album_songs(album_id), 0, callback, count, data)
 
     @log
-    def populate_items(self, query, offset, callback, count=50):
+    def populate_items(self, query, offset, callback, count=50, data=None):
         options = self.options.copy()
         options.set_skip(offset)
         if count != -1:
             options.set_count(count)
 
         def _callback(source, param, item, remaining, data, offset):
-            callback(source, param, item, remaining)
-        self.tracker.query(query, self.METADATA_KEYS, options, _callback, None)
+            if data:
+                callback(source, param, item, remaining, data)
+            else:
+                callback(source, param, item, remaining)
+        self.tracker.query(query, self.METADATA_KEYS, options, _callback, data)
 
     @log
     def _search_callback(self):


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