Merge pull request #1156 from djl/lastgenreunicode

lastgenre: Optionally replace unicode characters when performing lookups
This commit is contained in:
Adrian Sampson 2014-12-30 15:25:40 -04:00
commit e547426b8c

View file

@ -41,6 +41,10 @@ PYLAST_EXCEPTIONS = (
pylast.NetworkError,
)
REPLACE = {
u'\u2010': '-',
}
def deduplicate(seq):
"""Remove duplicates from sequence wile preserving order.
@ -238,7 +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.
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):
@ -248,7 +255,13 @@ class LastGenrePlugin(plugins.BeetsPlugin):
if key in self._genre_cache:
return self._genre_cache[key]
else:
genre = self.fetch_genre(method(*args))
args_replaced = []
for arg in args:
for k, v in REPLACE.items():
arg = arg.replace(k, v)
args_replaced.append(arg)
genre = self.fetch_genre(method(*args_replaced))
self._genre_cache[key] = genre
return genre