[gnome-music/wip/mschraal/core: 12/42] coregrilo: Fill model



commit 1e5925a08af14a087e2e391d8ee4bc4cfb5b1d78
Author: Marinus Schraal <mschraal gnome org>
Date:   Wed May 8 16:12:35 2019 +0200

    coregrilo: Fill model

 gnomemusic/coregrilo.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++-
 gnomemusic/coremodel.py |  2 +-
 2 files changed, 62 insertions(+), 2 deletions(-)
---
diff --git a/gnomemusic/coregrilo.py b/gnomemusic/coregrilo.py
index b35939d1..c7b1ed50 100644
--- a/gnomemusic/coregrilo.py
+++ b/gnomemusic/coregrilo.py
@@ -5,12 +5,33 @@ from gi.repository import Grl, GObject
 
 class CoreGrilo(GObject.GObject):
 
+    METADATA_KEYS = [
+        Grl.METADATA_KEY_ALBUM,
+        Grl.METADATA_KEY_ALBUM_ARTIST,
+        Grl.METADATA_KEY_ALBUM_DISC_NUMBER,
+        Grl.METADATA_KEY_ARTIST,
+        Grl.METADATA_KEY_CREATION_DATE,
+        Grl.METADATA_KEY_COMPOSER,
+        Grl.METADATA_KEY_DURATION,
+        Grl.METADATA_KEY_FAVOURITE,
+        Grl.METADATA_KEY_ID,
+        Grl.METADATA_KEY_LYRICS,
+        Grl.METADATA_KEY_PLAY_COUNT,
+        Grl.METADATA_KEY_THUMBNAIL,
+        Grl.METADATA_KEY_TITLE,
+        Grl.METADATA_KEY_TRACK_NUMBER,
+        Grl.METADATA_KEY_URL
+    ]
+
     def __repr__(self):
         return "<CoreGrilo>"
 
-    def __init__(self):
+    def __init__(self, model, table):
         super().__init__()
 
+        self._model = model
+        self._table = table
+
         Grl.init(None)
 
         self._fast_options = Grl.OperationOptions()
@@ -29,7 +50,46 @@ class CoreGrilo(GObject.GObject):
         print(source.props.source_id)
         if source.props.source_id == "grl-tracker-source":
             self._tracker_source = source
+            self._tracker_initial_fill(source)
             print(self._tracker_source, "added")
 
     def _on_source_removed(self, registry, source):
         print("removed", source.props.source_id)
+
+    def _tracker_initial_fill(self, source):
+        query = """
+            SELECT
+                rdf:type(?song)
+                ?song AS ?tracker_urn
+                nie:title(?song) AS ?title
+                tracker:id(?song) AS ?id
+                ?song
+                nie:url(?song) AS ?url
+                nie:title(?song) AS ?title
+                nmm:artistName(nmm:performer(?song)) AS ?artist
+                nie:title(nmm:musicAlbum(?song)) AS ?album
+                nfo:duration(?song) AS ?duration
+                nie:usageCounter(?song) AS ?play_count
+            WHERE { ?song a nmm:MusicPiece . }
+        """.replace('\n', ' ').strip()
+
+        print(query)
+
+        options = self._fast_options.copy()
+
+        self._tracker_source.query(
+            query, self.METADATA_KEYS, options, self._add_to_model)
+
+    def _add_to_model(self, source, op_id, media, user_data, error):
+        if error:
+            print("ERROR", error)
+            return
+
+        if not media:
+            print("NO MEDIA", source, op_id, media, error)
+            return
+
+        self._model.append(media)
+        self._table[media.get_id()] = media
+
+        # print(media.get_url(), media.get_title())
diff --git a/gnomemusic/coremodel.py b/gnomemusic/coremodel.py
index 4d6fe887..95c46a00 100644
--- a/gnomemusic/coremodel.py
+++ b/gnomemusic/coremodel.py
@@ -13,4 +13,4 @@ class CoreModel(GObject.GObject):
         self._model = Gio.ListStore()
         self._hash = {}
 
-        self._grilo = CoreGrilo()
+        self._grilo = CoreGrilo(self._model, self._hash)


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