[gnome-music/wip/jfelder/tag-test: 12/22] musicbrainz: Initial musicbrainz support



commit 750aefdfa56bd2572c3d2de4cf7a85f9daecc9ae
Author: Sumaid Syed <sumaidsyed gmail com>
Date:   Wed Jul 17 16:35:18 2019 +0530

    musicbrainz: Initial musicbrainz support
    
    Add support for chromparint, acoustid and musicbrainz grilo plugins.
    
    Based on patches by Jean Felder
    
    https://gitlab.gnome.org/GNOME/gnome-music/issues/293

 gnomemusic/grilo.py       |  12 ++++
 gnomemusic/musicbrainz.py | 153 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 165 insertions(+)
---
diff --git a/gnomemusic/grilo.py b/gnomemusic/grilo.py
index 020167d8..a2a8dcad 100644
--- a/gnomemusic/grilo.py
+++ b/gnomemusic/grilo.py
@@ -28,6 +28,7 @@
 import gi
 gi.require_version('Grl', '0.3')
 from gi.repository import GLib, GObject
+from gnomemusic.musicbrainz import MusicBrainz
 from gnomemusic.query import Query
 from gnomemusic import log
 from gnomemusic.trackerwrapper import TrackerWrapper
@@ -137,6 +138,8 @@ class Grilo(GObject.GObject):
             GObject.BindingFlags.BIDIRECTIONAL |
             GObject.BindingFlags.SYNC_CREATE)
 
+        self._musicbrainz = MusicBrainz(self)
+
         self._find_sources()
 
     @log
@@ -492,6 +495,15 @@ class Grilo(GObject.GObject):
         GLib.idle_add(
             self._store_metadata, media, key)
 
+    @log
+    def get_tags_from_musicbrainz(self, media, callback):
+        """Retrieves the metadata keys of media
+
+        :param media: A Grilo media item
+        :param callback: Function which is called after metadata retrieval
+        """
+        return self._musicbrainz.get_song_tags(media, callback)
+
     @log
     def toggle_favorite(self, media):
         """Toggles favorite status for media item
diff --git a/gnomemusic/musicbrainz.py b/gnomemusic/musicbrainz.py
new file mode 100644
index 00000000..22c2f98c
--- /dev/null
+++ b/gnomemusic/musicbrainz.py
@@ -0,0 +1,153 @@
+# Copyright 2018 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
+
+from gi.repository import GObject, Grl
+
+logger = logging.getLogger(__name__)
+
+
+class MusicBrainz(GObject.GObject):
+
+    _acoustid_api_key = 'Nb8SVVtH1C'
+
+    _needed_sources = [
+        'grl-chromaprint',
+        'grl-acoustid'
+    ]
+
+    ACOUSTID_METADATA_KEYS = [
+        Grl.METADATA_KEY_ALBUM,
+        Grl.METADATA_KEY_MB_ARTIST_ID,
+        Grl.METADATA_KEY_ALBUM_DISC_NUMBER,
+        Grl.METADATA_KEY_ARTIST,
+        Grl.METADATA_KEY_PUBLICATION_DATE,
+        Grl.METADATA_KEY_MB_RELEASE_ID,
+        Grl.METADATA_KEY_MB_RELEASE_GROUP_ID,
+        Grl.METADATA_KEY_MB_RECORDING_ID,
+        Grl.METADATA_KEY_TITLE,
+        Grl.METADATA_KEY_TRACK_NUMBER
+    ]
+
+    def __repr__(self):
+        return '<MusicBrainz>'
+
+    def __init__(self, grilo):
+        super().__init__()
+
+        self._grilo = grilo
+        self._grilo.connect('new-resolve-source-found', self._add_new_source)
+        self._grilo.connect('new-browse-source-found', self._add_new_source)
+
+        config = Grl.Config.new('grl-lua-factory', 'grl-acoustid')
+        config.set_api_key(self._acoustid_api_key)
+        self._grilo.registry.add_config(config)
+
+        self._fingerprint_key = Grl.METADATA_KEY_INVALID
+        self._sources = {}
+
+    def _add_new_source(self, klass, source):
+        source_id = source.get_id()
+        print ("Add new source called with id", source_id)
+        if source_id in self._needed_sources:
+            self._sources[source_id] = source
+
+        if source_id == 'grl-chromaprint':
+            self._fingerprint_key = self._grilo.registry.lookup_metadata_key(
+                'chromaprint')
+            if self._fingerprint_key == Grl.METADATA_KEY_INVALID:
+                logger.warning("Error, cannot retrieve fingerprint key")
+
+    @GObject.Property(type=bool, default=False)
+    def chromaprint_available(self):
+        return ('grl-chromaprint' in self._sources.keys()
+                and self._fingerprint_key != Grl.METADATA_KEY_INVALID)
+
+    @GObject.Property(type=bool, default=False)
+    def acoustid_available(self):
+        return 'grl-acoustid' in self._sources.keys()
+
+    @GObject.Property(type=bool, default=False)
+    def available(self):
+        return (self.props.chromaprint_available
+                and self.props.acoustid_available)
+
+    def _acoustid_resolved(self, source, op_id, media, count, callback, error=None):
+        if error:
+            logger.warning("Error {}: {}".format(error.domain, error.message))
+            return callback(None, 0)
+
+        return callback(media, count)
+
+    def _get_tags_from_acoustid(self, media, callback):
+        if not self.acoustid_available:
+            callback(None)
+            return
+
+        options = Grl.OperationOptions()
+        options.set_resolution_flags(Grl.ResolutionFlags.NORMAL)
+
+        error = None
+        self._sources['grl-acoustid'].browse(
+            media, self.ACOUSTID_METADATA_KEYS, options,
+            self._acoustid_resolved, callback)
+
+    def _chromaprint_resolved(
+            self, source, op_id, media, callback, error=None):
+        if error:
+            logger.warning("Error {}: {}".format(error.domain, error.message))
+            callback(None)
+            return
+
+        callback(media)
+
+    def _get_song_chromaprint(self, media, callback):
+        if self._fingerprint_key == Grl.METADATA_KEY_INVALID:
+            callback(None)
+            return
+
+        chromaprint = media.get_string(self._fingerprint_key)
+        if chromaprint is not None:
+            callback(media)
+            return
+
+        if not self.props.chromaprint_available:
+            callback(None)
+
+        keys = [self._fingerprint_key, Grl.METADATA_KEY_DURATION]
+        self._sources['grl-chromaprint'].resolve(
+            media, keys, self._grilo.options, self._chromaprint_resolved,
+            callback)
+
+    def get_song_tags(self, media, callback):
+        def _fingerprint_finished(media):
+            if not media:
+                callback(None)
+                return
+            self._grilo.set_metadata_key(media, self._fingerprint_key)
+            self._grilo.set_metadata_key(media, Grl.METADATA_KEY_DURATION)
+            self._get_tags_from_acoustid(media, callback)
+
+        self._get_song_chromaprint(media, _fingerprint_finished)


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