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.
This commit is contained in:
Jack Wilsdon 2016-03-16 20:46:25 +00:00
parent 8a0b18c960
commit 44c799320f

View file

@ -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):