From 224d782c2c2495120b767c34fb70a0bbf4679910 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Tue, 30 Jan 2018 22:37:44 -0500 Subject: [PATCH] Fix #2771: handle errors in genius lyrics source --- beetsplug/lyrics.py | 21 ++++++++++++++++++--- docs/changelog.rst | 2 ++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/beetsplug/lyrics.py b/beetsplug/lyrics.py index 3d5f38d9a..d9db8eadc 100644 --- a/beetsplug/lyrics.py +++ b/beetsplug/lyrics.py @@ -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"]: diff --git a/docs/changelog.rst b/docs/changelog.rst index 2c6833456..d8ca02e36 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -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.