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



commit 1d76546d6512063f99849b3c728ee087c14a6644
Author: Arnel A. Borja <arnelborja src gnome org>
Date:   Thu Apr 17 01:47:30 2014 +0800

    albumArtCache: Convert downloaded media covers to JPEG
    
    Use libmediaart to save the media covers so that they will be converted
    to JPEG.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=728338

 gnomemusic/albumArtCache.py |   42 +++++++++++++++++++++++++++++++++++-------
 gnomemusic/application.py   |    4 +++-
 gnomemusic/grilo.py         |   13 ++++++++-----
 3 files changed, 46 insertions(+), 13 deletions(-)
---
diff --git a/gnomemusic/albumArtCache.py b/gnomemusic/albumArtCache.py
index 254d7ae..64b25c2 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,35 @@ 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:
+                self.process_media_art(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
+            self.process_media_art(data)
+        except Exception as e:
+            logger.warn("Error: %s" % e)
+
+    @log
+    def process_media_art(self, data):
+        try:
+            item, width, height, callback, itr, artist, album, path, uri = data
+            f = Gio.File.new_for_uri(uri)
+            success, contents, etag = f.load_contents(None)
+            streamInfo = f.query_info('standard::content-type', Gio.FileQueryInfoFlags.NONE, None)
+            contentType = streamInfo.get_content_type()
+
+            MediaArt.process(contents, contentType, 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/application.py b/gnomemusic/application.py
index a4cafff..31a5b4e 100644
--- a/gnomemusic/application.py
+++ b/gnomemusic/application.py
@@ -31,7 +31,7 @@
 # delete this exception statement from your version.
 
 
-from gi.repository import Gtk, Gio, GLib, Gdk, Notify
+from gi.repository import Gtk, Gio, GLib, Gdk, Notify, MediaArt
 from gettext import gettext as _
 from gnomemusic.window import Window
 from gnomemusic.mpris import MediaPlayer2Service
@@ -108,6 +108,7 @@ class Application(Gtk.Application):
     @log
     def do_startup(self):
         Gtk.Application.do_startup(self)
+        MediaArt.init()
 
         Notify.init(_("Music"))
 
@@ -116,6 +117,7 @@ class Application(Gtk.Application):
     @log
     def quit(self, action=None, param=None):
         self._window.destroy()
+        MediaArt.shutdown()
 
     def do_activate(self):
         if not self._window:
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]