[gnome-music/wip/mschraal/tageditor-13feb2020: 15/25] grilowrappers: Introduce support for acoustid source



commit c721c997441c20000cdea98db279f24abb2c6f46
Author: Sumaid Syed <sumaidsyed gmail com>
Date:   Sat Nov 23 13:43:12 2019 +0100

    grilowrappers: Introduce support for acoustid source
    
    This source enables song authentication based on its fingerprint.
    
    Based on patches by Jean Felder and Marinus Schraal.

 gnomemusic/coregrilo.py                        |  9 +++
 gnomemusic/grilowrappers/grlacoustidwrapper.py | 92 ++++++++++++++++++++++++++
 2 files changed, 101 insertions(+)
---
diff --git a/gnomemusic/coregrilo.py b/gnomemusic/coregrilo.py
index f8ff42c0..fd0e6a5b 100644
--- a/gnomemusic/coregrilo.py
+++ b/gnomemusic/coregrilo.py
@@ -26,6 +26,7 @@ import gi
 gi.require_version('Grl', '0.3')
 from gi.repository import Grl, GLib, GObject
 
+from gnomemusic.grilowrappers.grlacoustidwrapper import GrlAcoustIDWrapper
 from gnomemusic.grilowrappers.grlchromaprintwrapper import (
     GrlChromaprintWrapper)
 from gnomemusic.grilowrappers.grlsearchwrapper import GrlSearchWrapper
@@ -48,6 +49,7 @@ class CoreGrilo(GObject.GObject):
                          "grl-lastfm-cover:2,"
                          "grl-theaudiodb-cover:1")
 
+    _acoustid_api_key = "Nb8SVVtH1C"
     _theaudiodb_api_key = "195003"
 
     cover_sources = GObject.Property(type=bool, default=False)
@@ -89,6 +91,10 @@ class CoreGrilo(GObject.GObject):
         config.set_api_key(self._theaudiodb_api_key)
         self._registry.add_config(config)
 
+        config = Grl.Config.new("grl-lua-factory", "grl-acoustid")
+        config.set_api_key(self._acoustid_api_key)
+        self._registry.add_config(config)
+
         self._registry.connect('source-added', self._on_source_added)
         self._registry.connect('source-removed', self._on_source_removed)
 
@@ -148,6 +154,9 @@ class CoreGrilo(GObject.GObject):
             self._search_wrappers[source.props.source_id] = GrlSearchWrapper(
                 source, self._coremodel, self._application, self)
             self._log.debug("Adding search source {}".format(source))
+        elif source.props.source_id == "grl-acoustid":
+            self._mb_wrappers[source.props.source_id] = GrlAcoustIDWrapper(
+                source, self)
         elif source.props.source_id == "grl-chromaprint":
             self._mb_wrappers[source.props.source_id] = GrlChromaprintWrapper(
                 source, self)
diff --git a/gnomemusic/grilowrappers/grlacoustidwrapper.py b/gnomemusic/grilowrappers/grlacoustidwrapper.py
new file mode 100644
index 00000000..5ebc4d55
--- /dev/null
+++ b/gnomemusic/grilowrappers/grlacoustidwrapper.py
@@ -0,0 +1,92 @@
+# Copyright 2020 The GNOME Music developers
+#
+# GNOME Music is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# GNOME Music is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with GNOME Music; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# The GNOME Music authors hereby grant permission for non-GPL compatible
+# GStreamer plugins to be used and distributed together with GStreamer
+# and GNOME Music.  This permission is above and beyond the permissions
+# granted by the GPL license by which GNOME Music is covered.  If you
+# modify this code, you may extend this exception to your version of the
+# code, but you are not obligated to do so.  If you do not wish to do so,
+# delete this exception statement from your version.
+
+import logging
+
+import gi
+gi.require_version("Grl", "0.3")
+from gi.repository import Grl, GObject
+
+
+logger = logging.getLogger(__name__)
+
+
+class GrlAcoustIDWrapper(GObject.GObject):
+    """Wrapper for the Grilo AcoustID source.
+    """
+
+    _ACOUSTID_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_MB_ARTIST_ID,
+        Grl.METADATA_KEY_MB_RECORDING_ID,
+        Grl.METADATA_KEY_MB_RELEASE_GROUP_ID,
+        Grl.METADATA_KEY_MB_RELEASE_ID,
+        Grl.METADATA_KEY_MB_TRACK_ID,
+        Grl.METADATA_KEY_TITLE,
+        Grl.METADATA_KEY_TRACK_NUMBER
+    ]
+
+    def __init__(self, source, grilo):
+        """Initialize the AcoustID wrapper
+
+        :param Grl.TrackerSource source: The Tracker source to wrap
+        :param CoreGrilo grilo: The CoreGrilo instance
+        """
+        super().__init__()
+
+        self._source = source
+        self._grilo = grilo
+
+        registry = self._grilo.props.registry
+        self._fingerprint_key = registry.lookup_metadata_key("chromaprint")
+
+    def get_tags(self, coresong, callback):
+        """Retrieve Musicbrainz tag set for the given song
+
+        :param CoreSong coresong: The song to retrieve tags for
+        :param callback: Metadata retrieval callback
+        """
+        options = Grl.OperationOptions()
+        options.set_resolution_flags(Grl.ResolutionFlags.NORMAL)
+
+        query = "duration={}&fingerprint={}".format(
+            str(coresong.props.media.get_duration()),
+            coresong.props.media.get_string(self._fingerprint_key))
+
+        def _acoustid_resolved(source, op_id, media, count, callback, error):
+            if error:
+                logger.warning(
+                    "Error {}: {}".format(error.domain, error.message))
+                callback(None, 0)
+                return
+
+            callback(media, count)
+
+        self._source.query(
+            query, self._ACOUSTID_METADATA_KEYS, options, _acoustid_resolved,
+            callback)


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