[gnome-music/wip/mschraal/searchview-rework: 10/15] Artist art retrieval & add to cache



commit 37a0412e5be97b2105668b8c2ec658204d7e357e
Author: Marinus Schraal <mschraal gnome org>
Date:   Fri Aug 2 14:50:03 2019 +0200

    Artist art retrieval & add to cache

 gnomemusic/albumartcache.py                   | 88 ++++++++++++++++++++++++++-
 gnomemusic/coreartist.py                      | 15 ++++-
 gnomemusic/grilowrappers/grltrackerwrapper.py | 13 ++--
 3 files changed, 110 insertions(+), 6 deletions(-)
---
diff --git a/gnomemusic/albumartcache.py b/gnomemusic/albumartcache.py
index bb7ec640..18e65532 100644
--- a/gnomemusic/albumartcache.py
+++ b/gnomemusic/albumartcache.py
@@ -172,17 +172,103 @@ class ArtistArt(GObject.GObject):
         super().__init__()
 
         self._coreartist = coreartist
+        self._artist = self._coreartist.props.artist
 
         if self._in_cache():
+            print("In cache!")
             return
 
         # FIXME: Ugly.
         grilo = self._coreartist._coremodel._grilo
 
+        self._coreartist.connect(
+            "notify::thumbnail", self._on_thumbnail_changed)
+
         grilo.get_artist_art(self._coreartist)
 
     def _in_cache(self):
-        return False
+        success, thumb_file = MediaArt.get_file(
+            self._artist, None, "artist")
+        if (not success
+                or not thumb_file.query_exists()):
+            self._coreartist.props.cached_thumbnail_uri = thumb_file.get_path()
+            return False
+
+        return True
+
+    def _on_thumbnail_changed(self, coreartist, thumbnail):
+        uri = coreartist.props.thumbnail
+        print("ArtistArt", uri)
+
+        if (uri is None
+                or uri == ""):
+            return
+
+        src = Gio.File.new_for_uri(uri)
+        src.read_async(
+            GLib.PRIORITY_LOW, None, self._read_callback, None)
+
+    def _read_callback(self, src, result, data):
+        try:
+            istream = src.read_finish(result)
+        except GLib.Error as error:
+            logger.warning("Error: {}, {}".format(error.domain, error.message))
+            return
+
+        try:
+            [tmp_file, iostream] = Gio.File.new_tmp()
+        except GLib.Error as error:
+            logger.warning("Error: {}, {}".format(error.domain, error.message))
+            return
+
+        ostream = iostream.get_output_stream()
+        # FIXME: Passing the iostream here, otherwise it gets
+        # closed. PyGI specific issue?
+        ostream.splice_async(
+            istream, Gio.OutputStreamSpliceFlags.CLOSE_SOURCE
+            | Gio.OutputStreamSpliceFlags.CLOSE_TARGET, GLib.PRIORITY_LOW,
+            None, self._splice_callback, [tmp_file, iostream])
+
+    def _delete_callback(self, src, result, data):
+        try:
+            src.delete_finish(result)
+        except GLib.Error as error:
+            logger.warning("Error: {}, {}".format(error.domain, error.message))
+
+    def _splice_callback(self, src, result, data):
+        tmp_file, iostream = data
+
+        iostream.close_async(
+            GLib.PRIORITY_LOW, None, self._close_iostream_callback, None)
+
+        try:
+            src.splice_finish(result)
+        except GLib.Error as error:
+            logger.warning("Error: {}, {}".format(error.domain, error.message))
+            return
+
+        success, cache_path = MediaArt.get_path(self._artist, None, "artist")
+
+        if not success:
+            return
+
+        try:
+            # FIXME: I/O blocking
+            MediaArt.file_to_jpeg(tmp_file.get_path(), cache_path)
+        except GLib.Error as error:
+            logger.warning("Error: {}, {}".format(error.domain, error.message))
+            return
+
+        self._in_cache()
+
+        tmp_file.delete_async(
+            GLib.PRIORITY_LOW, None, self._delete_callback, None)
+
+    def _close_iostream_callback(self, src, result, data):
+        try:
+            src.close_finish(result)
+        except GLib.Error as error:
+            logger.warning("Error: {}, {}".format(error.domain, error.message))
 
 class Art(GObject.GObject):
     """Retrieves art for an album or song
diff --git a/gnomemusic/coreartist.py b/gnomemusic/coreartist.py
index f6b58724..7e24f8db 100644
--- a/gnomemusic/coreartist.py
+++ b/gnomemusic/coreartist.py
@@ -42,6 +42,7 @@ class CoreArtist(GObject.GObject):
     def __init__(self, media, coremodel):
         super().__init__()
 
+        self._cached_thumbnail_uri = None
         self._coremodel = coremodel
         self._model = None
         self._selected = False
@@ -88,7 +89,19 @@ class CoreArtist(GObject.GObject):
     @GObject.Property(type=str, default=None)
     def thumbnail(self):
         if self._thumbnail is None:
+            self._thumbnail = ""
             ArtistArt(self)
 
-        return ""
+        return self._thumbnail
 
+    @thumbnail.setter
+    def thumbnail(self, value):
+        self._thumbnail = value
+
+    @GObject.Property(type=str, default=None)
+    def cached_thumbnail_uri(self):
+        return self._cached_thumbnail_uri
+
+    @cached_thumbnail_uri.setter
+    def cached_thumbnail_uri(self, value):
+        self._cached_thumbnail_uri = value
diff --git a/gnomemusic/grilowrappers/grltrackerwrapper.py b/gnomemusic/grilowrappers/grltrackerwrapper.py
index a44f014c..d18dea75 100644
--- a/gnomemusic/grilowrappers/grltrackerwrapper.py
+++ b/gnomemusic/grilowrappers/grltrackerwrapper.py
@@ -870,11 +870,16 @@ class GrlTrackerWrapper(GObject.GObject):
     def get_artist_art(self, coreartist):
         media = coreartist.props.media
 
-        def _resolve_cb(source, op_id, media, data, error):
+        def _resolve_cb(source, op_id, resolved_media, data, error):
             print("operation finished")
-            print(media.get_artist())
-            print(media.get_thumbnail())
-            coreartist.props.media.set_thumbnail(media.get_thumbnail())
+            print(resolved_media.get_artist())
+            print(resolved_media.get_thumbnail())
+            if resolved_media.get_thumbnail() is None:
+                coreartist.props.thumbnail = ""
+                return
+
+            media.set_thumbnail(resolved_media.get_thumbnail())
+            coreartist.props.thumbnail = media.get_thumbnail()
 
         full_options = Grl.OperationOptions()
         full_options.set_resolution_flags(


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