From 4eabc1c614afd4df9ad09e3218668b1d3545e018 Mon Sep 17 00:00:00 2001 From: Ade Date: Fri, 14 Apr 2017 08:47:09 +1200 Subject: [PATCH 1/3] opus format fixes https://github.com/rembo10/headphones/issues/2909 --- headphones/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/headphones/__init__.py b/headphones/__init__.py index 700caf47..20476169 100644 --- a/headphones/__init__.py +++ b/headphones/__init__.py @@ -86,7 +86,7 @@ CURRENT_VERSION = None LATEST_VERSION = None COMMITS_BEHIND = None -LOSSY_MEDIA_FORMATS = ["mp3", "aac", "ogg", "ape", "m4a", "asf", "wma"] +LOSSY_MEDIA_FORMATS = ["mp3", "aac", "ogg", "ape", "m4a", "asf", "wma", "opus"] LOSSLESS_MEDIA_FORMATS = ["flac", "aiff"] MEDIA_FORMATS = LOSSY_MEDIA_FORMATS + LOSSLESS_MEDIA_FORMATS From 61a3abbf20373a1eb1e094fd53032db156c21079 Mon Sep 17 00:00:00 2001 From: Ade Date: Thu, 20 Apr 2017 20:10:34 +1200 Subject: [PATCH 2/3] last.fm series info --- headphones/cache.py | 33 +++++++++++++++++++++++++++------ headphones/helpers.py | 23 +++++++++++++++++++++++ headphones/mb.py | 24 ++++++++++++++++++++++++ headphones/metacritic.py | 3 ++- 4 files changed, 76 insertions(+), 7 deletions(-) diff --git a/headphones/cache.py b/headphones/cache.py index 202b7802..0e946677 100644 --- a/headphones/cache.py +++ b/headphones/cache.py @@ -16,7 +16,7 @@ import os import headphones -from headphones import db, helpers, logger, lastfm, request +from headphones import db, helpers, logger, lastfm, request, mb LASTFM_API_KEY = "690e1ed3bc00bc91804cd8f7fe5ed6d4" @@ -290,6 +290,14 @@ class Cache(object): data = lastfm.request_lastfm("artist.getinfo", mbid=self.id, api_key=LASTFM_API_KEY) + # Try with name if not found + if not data: + dbartist = myDB.action('SELECT ArtistName, Type FROM artists WHERE ArtistID=?', [self.id]).fetchone() + if dbartist: + data = lastfm.request_lastfm("artist.getinfo", + artist=helpers.clean_musicbrainz_name(dbartist['ArtistName']), + api_key=LASTFM_API_KEY) + if not data: return @@ -315,18 +323,31 @@ class Cache(object): else: dbalbum = myDB.action( - 'SELECT ArtistName, AlbumTitle, ReleaseID FROM albums WHERE AlbumID=?', + 'SELECT ArtistName, AlbumTitle, ReleaseID, Type FROM albums WHERE AlbumID=?', [self.id]).fetchone() if dbalbum['ReleaseID'] != self.id: data = lastfm.request_lastfm("album.getinfo", mbid=dbalbum['ReleaseID'], api_key=LASTFM_API_KEY) if not data: - data = lastfm.request_lastfm("album.getinfo", artist=dbalbum['ArtistName'], - album=dbalbum['AlbumTitle'], + data = lastfm.request_lastfm("album.getinfo", + artist=helpers.clean_musicbrainz_name(dbalbum['ArtistName']), + album=helpers.clean_musicbrainz_name(dbalbum['AlbumTitle']), api_key=LASTFM_API_KEY) else: - data = lastfm.request_lastfm("album.getinfo", artist=dbalbum['ArtistName'], - album=dbalbum['AlbumTitle'], api_key=LASTFM_API_KEY) + if dbalbum['Type'] != "part of": + data = lastfm.request_lastfm("album.getinfo", + artist=helpers.clean_musicbrainz_name(dbalbum['ArtistName']), + album=helpers.clean_musicbrainz_name(dbalbum['AlbumTitle']), + api_key=LASTFM_API_KEY) + else: + + # Series, use actual artist for the release-group + artist = mb.getArtistForReleaseGroup(self.id) + if artist: + data = lastfm.request_lastfm("album.getinfo", + artist=helpers.clean_musicbrainz_name(artist), + album=helpers.clean_musicbrainz_name(dbalbum['AlbumTitle']), + api_key=LASTFM_API_KEY) if not data: return diff --git a/headphones/helpers.py b/headphones/helpers.py index 2ba72101..86a60a10 100644 --- a/headphones/helpers.py +++ b/headphones/helpers.py @@ -257,6 +257,12 @@ _XLATE_SPECIAL = { u'&': ' and ', # expand & to ' and ' } +_XLATE_MUSICBRAINZ = { + # Translation table for Musicbrainz. + u"…": '...', # HORIZONTAL ELLIPSIS (U+2026) + u"’": "'", # APOSTROPHE (U+0027) + u"‐": "-", # EN DASH (U+2013) +} def _translate(s, dictionary): # type: (basestring,Mapping[basestring,basestring])->basestring @@ -325,6 +331,23 @@ def clean_name(s): return u +def clean_musicbrainz_name(s, return_as_string=True): + # type: (basestring)->unicode + """Substitute special Musicbrainz characters. + :param s: string to clean up, probably unicode. + :return: cleaned-up version of input string. + """ + if not isinstance(s, unicode): + u = unicode(s, 'ascii', 'replace') + else: + u = s + u = _translate(u, _XLATE_MUSICBRAINZ) + if return_as_string: + return u.encode('utf-8') + else: + return u + + def cleanTitle(title): title = re.sub('[\.\-\/\_]', ' ', title).lower() diff --git a/headphones/mb.py b/headphones/mb.py index faba6b8a..951675a2 100644 --- a/headphones/mb.py +++ b/headphones/mb.py @@ -770,3 +770,27 @@ def findAlbumID(artist=None, album=None): return False rgid = unicode(results[0]['id']) return rgid + + +def getArtistForReleaseGroup(rgid): + """ + Returns artist name for a release group + Used for series where we store the series instead of the artist + """ + releaseGroup = None + try: + with mb_lock: + releaseGroup = musicbrainzngs.get_release_group_by_id( + rgid, ["artists"]) + releaseGroup = releaseGroup['release-group'] + except musicbrainzngs.WebServiceError as e: + logger.warn( + 'Attempt to retrieve information from MusicBrainz for release group "%s" failed (%s)' % ( + rgid, str(e))) + mb_lock.snooze(5) + + if not releaseGroup: + return False + else: + return releaseGroup['artist-credit'][0]['artist']['name'] + diff --git a/headphones/metacritic.py b/headphones/metacritic.py index d482786f..4ff20140 100644 --- a/headphones/metacritic.py +++ b/headphones/metacritic.py @@ -27,8 +27,9 @@ def update(artistid, artist_name, release_groups): # cut down on api calls. If it's ineffective then we'll switch to search replacements = {" & ": " ", ".": ""} + mc_artist_name = helpers.clean_musicbrainz_name(artist_name, return_as_string=False) + mc_artist_name = mc_artist_name.replace("'", " ") mc_artist_name = helpers.replace_all(artist_name.lower(), replacements) - mc_artist_name = mc_artist_name.replace(" ", "-") headers = { From 50851a4953916b0ca6e0330c63ff8e919ed9a47a Mon Sep 17 00:00:00 2001 From: Ade Date: Thu, 20 Apr 2017 20:42:04 +1200 Subject: [PATCH 3/3] pep8 --- headphones/helpers.py | 1 + headphones/mb.py | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/headphones/helpers.py b/headphones/helpers.py index 86a60a10..4c815ad7 100644 --- a/headphones/helpers.py +++ b/headphones/helpers.py @@ -264,6 +264,7 @@ _XLATE_MUSICBRAINZ = { u"‐": "-", # EN DASH (U+2013) } + def _translate(s, dictionary): # type: (basestring,Mapping[basestring,basestring])->basestring return ''.join(dictionary.get(x, x) for x in s) diff --git a/headphones/mb.py b/headphones/mb.py index 951675a2..c087d6b9 100644 --- a/headphones/mb.py +++ b/headphones/mb.py @@ -793,4 +793,3 @@ def getArtistForReleaseGroup(rgid): return False else: return releaseGroup['artist-credit'][0]['artist']['name'] -