diff --git a/beetsplug/lyrics.py b/beetsplug/lyrics.py index eadb5d6b6..d75fd4fe0 100644 --- a/beetsplug/lyrics.py +++ b/beetsplug/lyrics.py @@ -102,6 +102,16 @@ def extract_text(html, starttag): lyrics = lyrics.strip() return lyrics +def _encode(s): + """Encode the string for inclusion in a URL (common to both + LyricsWiki and Lyrics.com). + """ + if isinstance(s, unicode): + # Replace "fancy" apostrophes with straight ones. + s = s.replace(u'\u2019', u"'") + s = s.encode('utf8', 'ignore') + return urllib.quote(s) + LYRICSWIKI_URL_PATTERN = 'http://lyrics.wikia.com/%s:%s' def _lw_encode(s): s = re.sub(r'\s+', '_', s) @@ -110,9 +120,7 @@ def _lw_encode(s): s = s.replace("#", "Number_") s = re.sub(r'[\[\{]', '(', s) s = re.sub(r'[\]\}]', ')', s) - if isinstance(s, unicode): - s = s.encode('utf8', 'ignore') - return urllib.quote(s) + return _encode(s) def fetch_lyricswiki(artist, title): """Fetch lyrics from LyricsWiki.""" url = LYRICSWIKI_URL_PATTERN % (_lw_encode(artist), _lw_encode(title)) @@ -131,9 +139,7 @@ LYRICSCOM_NOT_FOUND = ( ) def _lc_encode(s): s = re.sub(r'\s+', '-', s) - if isinstance(s, unicode): - s = s.encode('utf8', 'ignore') - return urllib.quote(s) + return _encode(s) def fetch_lyricscom(artist, title): """Fetch lyrics from Lyrics.com.""" url = LYRICSCOM_URL_PATTERN % (_lc_encode(title), _lc_encode(artist)) diff --git a/docs/changelog.rst b/docs/changelog.rst index 958093d61..c08c28316 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,6 +5,8 @@ Changelog ---------------------- * :doc:`/plugins/scrub`: Fix an incompatibility with Python 2.6. +* :doc:`/plugins/lyrics`: Fix an issue that failed to find lyrics when metadata + contained "real" apostrophes. 1.0rc2 (December 31, 2012) --------------------------