From c227501a5d1c6810a7555f1928c5a6401b101799 Mon Sep 17 00:00:00 2001 From: David Logie Date: Sat, 20 Dec 2014 22:10:58 +0000 Subject: [PATCH 1/8] lastgenre: Optionally replace unicode characters when performing lookups. --- beetsplug/lastgenre/__init__.py | 6 ++++++ docs/plugins/lastgenre.rst | 3 +++ 2 files changed, 9 insertions(+) diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index ea0537988..2d502e076 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -25,6 +25,8 @@ import pylast import os import yaml +from unidecode import unidecode + from beets import plugins from beets import ui from beets.util import normpath, plurality @@ -133,6 +135,7 @@ class LastGenrePlugin(plugins.BeetsPlugin): 'force': True, 'auto': True, 'separator': u', ', + 'asciify': False, }) self.setup() @@ -244,6 +247,9 @@ class LastGenrePlugin(plugins.BeetsPlugin): if any(not s for s in args): return None + if self.config['asciify']: + args = [unidecode(arg) for arg in args] + key = u'{0}.{1}'.format(entity, u'-'.join(unicode(a) for a in args)) if key in self._genre_cache: return self._genre_cache[key] diff --git a/docs/plugins/lastgenre.rst b/docs/plugins/lastgenre.rst index 9d3242467..d874a4a72 100644 --- a/docs/plugins/lastgenre.rst +++ b/docs/plugins/lastgenre.rst @@ -108,6 +108,9 @@ configuration file. The available options are: - **whitelist**: The filename of a custom genre list, ``yes`` to use the internal whitelist, or ``no`` to consider all genres valid. Default: ``yes``. +- **asciify**: Convert all non-ASCII characters to ASCII equivalents + when performing lookups. + Default: ``no``. Running Manually ---------------- From c7b6f75ca8c95b7b547bde9a3d5bb39c53f0bcff Mon Sep 17 00:00:00 2001 From: David Logie Date: Sun, 21 Dec 2014 12:58:15 +0000 Subject: [PATCH 2/8] lastgenre: Automatically retry asciified lookups if the initial lookup fails. --- beetsplug/lastgenre/__init__.py | 12 ++++++------ docs/plugins/lastgenre.rst | 3 --- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index 2d502e076..7937db277 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -135,7 +135,6 @@ class LastGenrePlugin(plugins.BeetsPlugin): 'force': True, 'auto': True, 'separator': u', ', - 'asciify': False, }) self.setup() @@ -247,15 +246,16 @@ class LastGenrePlugin(plugins.BeetsPlugin): if any(not s for s in args): return None - if self.config['asciify']: - args = [unidecode(arg) for arg in args] - key = u'{0}.{1}'.format(entity, u'-'.join(unicode(a) for a in args)) if key in self._genre_cache: return self._genre_cache[key] else: - genre = self.fetch_genre(method(*args)) - self._genre_cache[key] = genre + args_ascii = [unidecode(arg) for arg in args] + for arglist in [args, args_ascii]: + genre = self.fetch_genre(method(*arglist)) + self._genre_cache[key] = genre + if genre: + break return genre def fetch_album_genre(self, obj): diff --git a/docs/plugins/lastgenre.rst b/docs/plugins/lastgenre.rst index d874a4a72..9d3242467 100644 --- a/docs/plugins/lastgenre.rst +++ b/docs/plugins/lastgenre.rst @@ -108,9 +108,6 @@ configuration file. The available options are: - **whitelist**: The filename of a custom genre list, ``yes`` to use the internal whitelist, or ``no`` to consider all genres valid. Default: ``yes``. -- **asciify**: Convert all non-ASCII characters to ASCII equivalents - when performing lookups. - Default: ``no``. Running Manually ---------------- From d9673ef9b5e1456cc5da2a600465e1587ba885df Mon Sep 17 00:00:00 2001 From: David Logie Date: Fri, 26 Dec 2014 14:08:22 +0000 Subject: [PATCH 3/8] lastgenre: Only replace a small set of unicode characters. --- beetsplug/lastgenre/__init__.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index 7937db277..8d75c7e4f 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # This file is part of beets. # Copyright 2013, Adrian Sampson. # @@ -25,8 +26,6 @@ import pylast import os import yaml -from unidecode import unidecode - from beets import plugins from beets import ui from beets.util import normpath, plurality @@ -43,6 +42,10 @@ PYLAST_EXCEPTIONS = ( pylast.NetworkError, ) +REPLACE = { + u'‐': '-', +} + def deduplicate(seq): """Remove duplicates from sequence wile preserving order. @@ -250,7 +253,12 @@ class LastGenrePlugin(plugins.BeetsPlugin): if key in self._genre_cache: return self._genre_cache[key] else: - args_ascii = [unidecode(arg) for arg in args] + args_ascii = [] + for arg in args: + for k, v in REPLACE.items(): + arg = arg.replace(k, v) + args_ascii.append(arg) + for arglist in [args, args_ascii]: genre = self.fetch_genre(method(*arglist)) self._genre_cache[key] = genre From 3015c22d403d93002813800911047137d456ca4a Mon Sep 17 00:00:00 2001 From: David Logie Date: Fri, 26 Dec 2014 14:17:49 +0000 Subject: [PATCH 4/8] Fix indent level. --- beetsplug/lastgenre/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index 8d75c7e4f..be853c55b 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -43,7 +43,7 @@ PYLAST_EXCEPTIONS = ( ) REPLACE = { - u'‐': '-', + u'‐': '-', } From 433d68724a915f5c5980168e7db987de4a0c9e2e Mon Sep 17 00:00:00 2001 From: David Logie Date: Sun, 28 Dec 2014 18:38:15 +0000 Subject: [PATCH 5/8] Escape Unicode characters so we don't need the encoding headers. --- beetsplug/lastgenre/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index be853c55b..5dbfaefb9 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # This file is part of beets. # Copyright 2013, Adrian Sampson. # @@ -43,7 +42,7 @@ PYLAST_EXCEPTIONS = ( ) REPLACE = { - u'‐': '-', + u'\u2010': '-', } From 148d9048d54cf030432f09d36face43c3a7a589e Mon Sep 17 00:00:00 2001 From: David Logie Date: Sun, 28 Dec 2014 18:38:42 +0000 Subject: [PATCH 6/8] Update _cached_lookup() docstring. --- beetsplug/lastgenre/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index 5dbfaefb9..e9dfe634c 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -242,7 +242,9 @@ class LastGenrePlugin(plugins.BeetsPlugin): def _cached_lookup(self, entity, method, *args): """Get a genre based on the named entity using the callable `method` whose arguments are given in the sequence `args`. The genre lookup - is cached based on the entity name and the arguments. + is cached based on the entity name and the arguments. Before the lookup, + each argument is has some Unicode characters replaced with rough ASCII + equivalents in order to return better results from the Last.fm database. """ # Shortcut if we're missing metadata. if any(not s for s in args): From f2704461cfd8ba6fe781c608e48ef995616397a3 Mon Sep 17 00:00:00 2001 From: David Logie Date: Sun, 28 Dec 2014 18:39:34 +0000 Subject: [PATCH 7/8] Go back to a single lookup with specific Unicode characters replaced. --- beetsplug/lastgenre/__init__.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index e9dfe634c..c4ec46af1 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -254,17 +254,14 @@ class LastGenrePlugin(plugins.BeetsPlugin): if key in self._genre_cache: return self._genre_cache[key] else: - args_ascii = [] + args_replaced = [] for arg in args: for k, v in REPLACE.items(): arg = arg.replace(k, v) - args_ascii.append(arg) + args_replaced.append(arg) - for arglist in [args, args_ascii]: - genre = self.fetch_genre(method(*arglist)) - self._genre_cache[key] = genre - if genre: - break + genre = self.fetch_genre(method(*args_replaced)) + self._genre_cache[key] = genre return genre def fetch_album_genre(self, obj): From 08d5b9c13860d942df7570186915c87533164ea1 Mon Sep 17 00:00:00 2001 From: David Logie Date: Mon, 29 Dec 2014 19:45:33 +0000 Subject: [PATCH 8/8] Fix line length in docstring. --- beetsplug/lastgenre/__init__.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index c4ec46af1..9a6c99585 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -242,9 +242,10 @@ class LastGenrePlugin(plugins.BeetsPlugin): def _cached_lookup(self, entity, method, *args): """Get a genre based on the named entity using the callable `method` whose arguments are given in the sequence `args`. The genre lookup - is cached based on the entity name and the arguments. Before the lookup, - each argument is has some Unicode characters replaced with rough ASCII - equivalents in order to return better results from the Last.fm database. + is cached based on the entity name and the arguments. Before the + lookup, each argument is has some Unicode characters replaced with + rough ASCII equivalents in order to return better results from the + Last.fm database. """ # Shortcut if we're missing metadata. if any(not s for s in args):