From 44c799320fcf1e3d10fd4368db776fa786f0cb1d Mon Sep 17 00:00:00 2001 From: Jack Wilsdon Date: Wed, 16 Mar 2016 20:46:25 +0000 Subject: [PATCH] Improve URL generation in lyrics plugin Allow custom replacements to be defined in subclasses of SymbolsReplaced. Replace spaces with a hyphens when the source is MusiXmatch, instead of (incorrectly) using underscores. This fixes #1880. --- beetsplug/lyrics.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/beetsplug/lyrics.py b/beetsplug/lyrics.py index 66b859f59..c7ce64584 100644 --- a/beetsplug/lyrics.py +++ b/beetsplug/lyrics.py @@ -200,18 +200,27 @@ class Backend(object): class SymbolsReplaced(Backend): + REPLACEMENTS = { + r'\s+': '_', + '<': 'Less_Than', + '>': 'Greater_Than', + '#': 'Number_', + r'[\[\{]': '(', + r'[\[\{]': ')' + } + @classmethod def _encode(cls, s): - s = re.sub(r'\s+', '_', s) - s = s.replace("<", "Less_Than") - s = s.replace(">", "Greater_Than") - s = s.replace("#", "Number_") - s = re.sub(r'[\[\{]', '(', s) - s = re.sub(r'[\]\}]', ')', s) + for old, new in cls.REPLACEMENTS.iteritems(): + s = re.sub(old, new, s) + return super(SymbolsReplaced, cls)._encode(s) - class MusiXmatch(SymbolsReplaced): + REPLACEMENTS = dict(SymbolsReplaced.REPLACEMENTS, **{ + r'\s+': '-' + }) + URL_PATTERN = 'https://www.musixmatch.com/lyrics/%s/%s' def fetch(self, artist, title):