Fix #2771: handle errors in genius lyrics source

This commit is contained in:
Adrian Sampson 2018-01-30 22:37:44 -05:00
parent 277d81b4d6
commit 224d782c2c
2 changed files with 20 additions and 3 deletions

View file

@ -359,7 +359,12 @@ class Genius(Backend):
# Gotta go regular html scraping... come on Genius.
page_url = "https://genius.com" + path
page = requests.get(page_url)
try:
page = requests.get(page_url)
except requests.RequestException as exc:
self._log.debug(u'Genius page request for {0} failed: {1}',
page_url, exc)
return None
html = BeautifulSoup(page.text, "html.parser")
# Remove script tags that they put in the middle of the lyrics.
@ -374,8 +379,18 @@ class Genius(Backend):
def fetch(self, artist, title):
search_url = self.base_url + "/search"
data = {'q': title}
response = requests.get(search_url, data=data, headers=self.headers)
json = response.json()
try:
response = requests.get(search_url, data=data,
headers=self.headers)
except requests.RequestException as exc:
self._log.debug(u'Genius API request failed: {0}', exc)
return None
try:
json = response.json()
except ValueError:
self._log.debug(u'Genius API request returned invalid JSON')
return None
song_info = None
for hit in json["response"]["hits"]:

View file

@ -27,6 +27,8 @@ Fixes:
* :doc:`/plugins/lyrics`: The plugin no longer crashes in the Genius source
when BeautifulSoup is not found. Instead, it just logs a message and
disables the source.
* :doc:`/plugins/lyrics`: Handle network and API errors when communicating
with Genius. :bug:`2771`
* :doc:`/plugins/lyrics`: The ``lyrics`` command previously write ReST files
by default. This default has been fixed.