[gnome-music] Use album-artist if available



commit 17de084033fe659d63efa619a15026036f949bdb
Author: Marinus Schraal <mschraal src gnome org>
Date:   Thu May 26 17:55:13 2016 +0200

    Use album-artist if available
    
    Album songs can be tagged with an album-artist tag, which indicates the
    main artist of an album. Individual songs on an album can have a
    different artist tag: the performer(s) on that track. Using album-artist
    for display or sorting is preferrable in most cases.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=754700

 gnomemusic/grilo.py   |   14 +++++++++-----
 gnomemusic/mpris.py   |   20 ++++----------------
 gnomemusic/player.py  |   13 ++++---------
 gnomemusic/query.py   |   32 ++++++++++++++++----------------
 gnomemusic/utils.py   |   44 ++++++++++++++++++++++++++++++++++++++++++++
 gnomemusic/view.py    |   11 ++++++-----
 gnomemusic/widgets.py |   10 +++++-----
 7 files changed, 88 insertions(+), 56 deletions(-)
---
diff --git a/gnomemusic/grilo.py b/gnomemusic/grilo.py
index 19e5d50..120466c 100644
--- a/gnomemusic/grilo.py
+++ b/gnomemusic/grilo.py
@@ -46,13 +46,17 @@ class Grilo(GObject.GObject):
     }
 
     METADATA_KEYS = [
-        Grl.METADATA_KEY_ID, Grl.METADATA_KEY_TITLE,
-        Grl.METADATA_KEY_ARTIST, Grl.METADATA_KEY_ALBUM,
-        Grl.METADATA_KEY_DURATION,
+        Grl.METADATA_KEY_ALBUM,
+        Grl.METADATA_KEY_ALBUM_ARTIST,
+        Grl.METADATA_KEY_ARTIST,
         Grl.METADATA_KEY_CREATION_DATE,
-        Grl.METADATA_KEY_URL,
+        Grl.METADATA_KEY_DURATION,
+        Grl.METADATA_KEY_ID,
         Grl.METADATA_KEY_LYRICS,
-        Grl.METADATA_KEY_THUMBNAIL]
+        Grl.METADATA_KEY_THUMBNAIL,
+        Grl.METADATA_KEY_TITLE,
+        Grl.METADATA_KEY_URL
+    ]
 
     METADATA_THUMBNAIL_KEYS = [
         Grl.METADATA_KEY_ID,
diff --git a/gnomemusic/mpris.py b/gnomemusic/mpris.py
index 91b9c1c..67c6531 100644
--- a/gnomemusic/mpris.py
+++ b/gnomemusic/mpris.py
@@ -27,6 +27,7 @@ from gnomemusic.player import PlaybackStatus, RepeatType
 from gnomemusic.albumArtCache import AlbumArtCache
 from gnomemusic.grilo import grilo
 from gnomemusic.playlists import Playlists
+import gnomemusic.utils as utils
 
 from gettext import gettext as _
 from gi.repository import GLib
@@ -315,22 +316,9 @@ class MediaPlayer2Service(Server):
         finally:
             metadata['xesam:album'] = GLib.Variant('s', album)
 
-        try:
-            artist = media.get_artist()
-            assert artist is not None
-        except:
-            try:
-                artist = media.get_artist()
-                assert artist is not None
-            except:
-                try:
-                    artist = media.get_artist()
-                    assert artist is not None
-                except (AssertionError, ValueError):
-                    artist = _("Unknown Artist")
-        finally:
-            metadata['xesam:artist'] = GLib.Variant('as', [artist])
-            metadata['xesam:albumArtist'] = GLib.Variant('as', [artist])
+        artist = utils.get_artist_name(media)
+        metadata['xesam:artist'] = GLib.Variant('as', [artist])
+        metadata['xesam:albumArtist'] = GLib.Variant('as', [artist])
 
         try:
             genre = media.get_genre()
diff --git a/gnomemusic/player.py b/gnomemusic/player.py
index 2c2b500..6c870b0 100644
--- a/gnomemusic/player.py
+++ b/gnomemusic/player.py
@@ -44,6 +44,7 @@ from random import randint
 from collections import deque
 from gnomemusic.albumArtCache import AlbumArtCache
 from gnomemusic.playlists import Playlists
+import gnomemusic.utils as utils
 playlists = Playlists.get_default()
 
 from hashlib import md5
@@ -595,15 +596,9 @@ class Player(GObject.GObject):
         self.playBtn.set_sensitive(True)
         self._sync_prev_next()
 
-        artist = _("Unknown Artist")
-        try:
-            assert media.get_artist() is not None
-            artist = media.get_artist()
-        except:
-            pass
-        finally:
-            self.artistLabel.set_label(artist)
-            self._currentArtist = artist
+        artist = utils.get_artist_name(media)
+        self.artistLabel.set_label(artist)
+        self._currentArtist = artist
 
         album = _("Unknown Album")
         try:
diff --git a/gnomemusic/query.py b/gnomemusic/query.py
index 7bfc6fa..ca0d3fa 100644
--- a/gnomemusic/query.py
+++ b/gnomemusic/query.py
@@ -122,7 +122,8 @@ class Query():
     SELECT
         rdf:type(?album)
         tracker:id(?album) AS ?id
-        nmm:artistName(?albumArtist) AS ?artist
+        nmm:artistName(?albumArtist) AS ?album_artist
+        nmm:artistName(?performer) AS ?artist
         ?title
         COUNT(?song) AS ?childcount
         YEAR(MAX(nie:contentCreated(?song))) AS ?creation_date
@@ -156,7 +157,8 @@ class Query():
     SELECT
         rdf:type(?album)
         tracker:id(?album) AS ?id
-        ?artist
+        nmm:artistName(?performer) AS ?artist
+        nmm:artistName(?albumArtist) AS ?album_artist
         ?title
         COUNT(?song) AS ?childcount
         YEAR(MAX(nie:contentCreated(?song))) AS ?creation_date
@@ -165,14 +167,12 @@ class Query():
         ?album a nmm:MusicAlbum ;
                nmm:albumArtist ?albumArtist ;
                nie:title ?title .
-        ?albumArtist nmm:artistName ?artist .
         ?song nmm:musicAlbum ?album ;
               nmm:performer ?performer .
-        BIND(LCASE(?artist) AS ?artist_lower) .
+        BIND(LCASE(nmm:artistName(?albumArtist)) AS ?artist_lower) .
         BIND(LCASE(?title) AS ?title_lower) .
         BIND((%(artist_order)s) AS ?artist_collation) .
         BIND((%(album_order)s) AS ?title_collation) .
-
         FILTER(STRSTARTS(nie:url(?song), '%(music_dir)s/'))
     }
     GROUP BY ?album
@@ -194,10 +194,10 @@ class Query():
         tracker:id (?song) AS ?id
         ?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
-        IF (BOUND (?tag), 'b', '') AS ?lyrics
+        nmm:artistName(nmm:performer(?song)) AS ?artist
+        nie:title(nmm:musicAlbum(?song)) AS ?album
+        nfo:duration(?song) AS ?duration
+        IF (BOUND(?tag), 'b', '') AS ?lyrics
     {
         %(where_clause)s
         ?song a nmm:MusicPiece ;
@@ -209,7 +209,7 @@ class Query():
             nie:title ?title .
         OPTIONAL { ?song nao:hasTag ?tag .
                    FILTER (?tag = nao:predefined-tag-favorite) } .
-        FILTER (STRSTARTS (?url, '%(music_dir)s/'))
+        FILTER(STRSTARTS(?url, '%(music_dir)s/'))
     }
     ORDER BY ?artist ?album nmm:setNumber(?disc) nmm:trackNumber(?song)
     '''.replace('\n', ' ').strip() % {
@@ -330,12 +330,12 @@ class Query():
         tracker:id(?album) AS ?id
         (
             SELECT
-                nmm:artistName(?artist)
+                nmm:artistName(?album_artist)
             WHERE {
-                ?album nmm:albumArtist ?artist
+                ?album nmm:albumArtist ?album_artist
             }
             LIMIT 1
-        ) AS ?artist
+        ) AS ?album_artist
         nie:title(?album) AS ?title
         nie:title(?album) AS ?album
     WHERE {
@@ -358,12 +358,12 @@ class Query():
         tracker:id(?album) AS ?id
         (
             SELECT
-                nmm:artistName(?artist)
+                nmm:artistName(?album_artist)
             WHERE {
-                ?album nmm:albumArtist ?artist
+                ?album nmm:albumArtist ?album_artist
             }
             LIMIT 1
-        ) AS ?artist
+        ) AS ?album_artist
         nie:title(?album) AS ?title
         nie:title(?album) AS ?album
     WHERE {
diff --git a/gnomemusic/utils.py b/gnomemusic/utils.py
new file mode 100644
index 0000000..3fd8322
--- /dev/null
+++ b/gnomemusic/utils.py
@@ -0,0 +1,44 @@
+# Copyright (c) 2016 Marinus Schraal <mschraal src gnome org>
+#
+# 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.
+
+from gettext import gettext as _
+
+
+def get_artist_name(item):
+    """Returns the preferred artist for a media item.
+
+    The artist name for a particular media item can be either
+    the main artist of the full album (album artist), the
+    artist of the song (artist) or possibly it is not known at
+    all. The first is preferred in most cases, because it is
+    the most accurate in an album setting.
+
+    :param item: A Grilo Media object
+    :return: The artist name
+    :rtype: string
+    """
+
+    return (item.get_album_artist()
+            or item.get_artist()
+            or _("Unknown Artist"))
diff --git a/gnomemusic/view.py b/gnomemusic/view.py
index 14315c1..37d7bc5 100644
--- a/gnomemusic/view.py
+++ b/gnomemusic/view.py
@@ -47,6 +47,7 @@ from gnomemusic.toolbar import ToolbarState
 import gnomemusic.widgets as Widgets
 from gnomemusic.player import DiscoveryStatus
 from gnomemusic.playlists import Playlists, StaticPlaylists
+import gnomemusic.utils as utils
 from gnomemusic.albumArtCache import AlbumArtCache as albumArtCache
 from gnomemusic import log
 import logging
@@ -214,7 +215,7 @@ class ViewContainer(Gtk.Stack):
             return
 
         self._offset += 1
-        artist = item.get_artist() or _("Unknown Artist")
+        artist = utils.get_artist_name(item)
         title = albumArtCache.get_media_title(item)
         # item.set_title(title)
 
@@ -472,7 +473,7 @@ class Songs(ViewContainer):
             return
         self._offset += 1
         item.set_title(albumArtCache.get_media_title(item))
-        artist = item.get_artist() or _("Unknown Artist")
+        artist = utils.get_artist_name(item)
         if item.get_url() is None:
             return
         self.model.insert_with_valuesv(
@@ -724,7 +725,7 @@ class Artists (ViewContainer):
                 self.view.show()
             return
         self._offset += 1
-        artist = item.get_artist() or _("Unknown Artist")
+        artist = utils.get_artist_name(item)
         if not artist.casefold() in self._artists:
             _iter = self.model.insert_with_valuesv(-1, [2], [artist])
             self._artists[artist.casefold()] = {'iter': _iter, 'albums': [], 'widget': None}
@@ -1445,7 +1446,7 @@ class Search(ViewContainer):
         if data != self.model:
             return
 
-        artist = item.get_artist() or _("Unknown Artist")
+        artist = utils.get_artist_name(item)
         album = item.get_album() or _("Unknown Album")
 
         key = '%s-%s' % (artist, album)
@@ -1494,7 +1495,7 @@ class Search(ViewContainer):
         self._offset += 1
         title = albumArtCache.get_media_title(item)
         item.set_title(title)
-        artist = item.get_artist() or _("Unknown Artist")
+        artist = utils.get_artist_name(item)
 
         group = 3
         try:
diff --git a/gnomemusic/widgets.py b/gnomemusic/widgets.py
index 1401f9b..59de954 100644
--- a/gnomemusic/widgets.py
+++ b/gnomemusic/widgets.py
@@ -37,6 +37,7 @@ from gnomemusic.grilo import grilo
 from gnomemusic.albumArtCache import AlbumArtCache
 from gnomemusic.player import DiscoveryStatus
 from gnomemusic.playlists import Playlists, StaticPlaylists
+import gnomemusic.utils as utils
 from gnomemusic import log
 import logging
 logger = logging.getLogger(__name__)
@@ -243,7 +244,7 @@ class AlbumWidget(Gtk.EventBox):
         self.selection_toolbar = selection_toolbar
         self.header_bar = header_bar
         self.album = album
-        real_artist = item.get_artist() or _("Unknown Artist")
+        real_artist = utils.get_artist_name(item)
         self.ui.get_object('cover').set_from_pixbuf(self.loadingIcon)
         ALBUM_ART_CACHE.lookup(item, 256, 256, self._on_look_up, None, real_artist, album)
 
@@ -653,10 +654,9 @@ class ArtistAlbumWidget(Gtk.Box):
 
     @log
     def _update_album_art(self):
-        real_artist = self.album.get_artist() or _("Unknown Artist")
-        ALBUM_ART_CACHE.lookup(
-            self.album, 128, 128, self._get_album_cover, None,
-            real_artist, self.album.get_title())
+        artist = utils.get_artist_name(self.album)
+        ALBUM_ART_CACHE.lookup(self.album, 128, 128, self._get_album_cover,
+                               None, artist, self.album.get_title())
 
     @log
     def _get_album_cover(self, pixbuf, path, data=None):


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