catch URL fetch exceptions in lyrics plugin

This commit is contained in:
Adrian Sampson 2012-04-10 21:05:01 -07:00
parent ccaf0031cd
commit 2c11855b1e

View file

@ -23,6 +23,11 @@ from beets import ui
from beets.ui import commands
# Global logger.
log = logging.getLogger('beets')
# Lyrics scrapers.
COMMENT_RE = re.compile(r'<!--.*-->', re.S)
@ -30,6 +35,16 @@ DIV_RE = re.compile(r'<(/?)div>?')
TAG_RE = re.compile(r'<[^>]*>')
BREAK_RE = re.compile(r'<br\s*/?>')
def fetch_url(url):
"""Retrieve the content at a given URL, or return None if the source
is unreachable.
"""
try:
return urllib.urlopen(url).read()
except IOError as exc:
log.debug('failed to fetch: {} ({})'.format(url, str(exc)))
return None
def unescape(text):
"""Resolves &#xxx; HTML entities (and some others)."""
out = text.replace('&nbsp;', ' ')
@ -97,7 +112,9 @@ def _lw_encode(s):
def fetch_lyricswiki(artist, title):
"""Fetch lyrics from LyricsWiki."""
url = LYRICSWIKI_URL_PATTERN % (_lw_encode(artist), _lw_encode(title))
html = urllib.urlopen(url).read()
html = fetch_url(url)
if not html:
return
lyrics = extract_text(html, "<div class='lyricbox'>")
if lyrics and 'Unfortunately, we are not licensed' not in lyrics:
@ -112,7 +129,9 @@ def _lc_encode(s):
def fetch_lyricscom(artist, title):
"""Fetch lyrics from Lyrics.com."""
url = LYRICSCOM_URL_PATTERN % (_lc_encode(title), _lc_encode(artist))
html = urllib.urlopen(url).read()
html = fetch_url(url)
if not html:
return
lyrics = extract_text(html, '<div id="lyric_space">')
if lyrics and 'Sorry, we do not have the lyric' not in lyrics:
@ -133,8 +152,6 @@ def get_lyrics(artist, title):
# Plugin logic.
log = logging.getLogger('beets')
def fetch_item_lyrics(lib, loglevel, item, write):
"""Fetch and store lyrics for a single item. If ``write``, then the
lyrics will also be written to the file itself. The ``loglevel``