fetchart: Add some error handling to prevent crashes
Today I had some network problems regarding dbpedia.org, which made
beets crash because a requests.exceptions.ConnectionError was raised
("[Errno 113] No route to host"). This commits adds some error handling
around network requests to prevent further crashes in the future.
@@ -365,12 +365,17 @@ class GoogleImages(RemoteArtSource):
if not (album.albumartist and album.album):
return
search_string = (album.albumartist + ',' + album.album).encode('utf-8')
+
+ try:
response = self.request(self.URL, params={
'key': self.key,
'cx': self.cx,
'q': search_string,
'searchType': 'image'
})
+ except requests.RequestException:
+ self._log.debug(u'google: error receiving response')
+ return
# Get results using JSON.
try:
@@ -406,10 +411,14 @@ class FanartTV(RemoteArtSource):
if not album.mb_releasegroupid:
return
+ try:
response = self.request(
self.API_ALBUMS + album.mb_releasegroupid,
headers={'api-key': self.PROJECT_KEY,
'client-key': self.client_key})
+ except requests.RequestException:
+ self._log.debug(u'fanart.tv: error receiving response')
+ return
try:
data = response.json()
@@ -545,6 +554,8 @@ class Wikipedia(RemoteArtSource):
# Find the name of the cover art filename on DBpedia
cover_filename, page_id = None, None
+
+ try:
dbpedia_response = self.request(
self.DBPEDIA_URL,
params={
@@ -555,6 +566,10 @@ class Wikipedia(RemoteArtSource):
},
headers={'content-type': 'application/json'},
)
+ except requests.RequestException:
+ self._log.debug(u'dbpedia: error receiving response')
+ return
+
try:
data = dbpedia_response.json()
results = data['results']['bindings']
@@ -584,6 +599,7 @@ class Wikipedia(RemoteArtSource):
lpart, rpart = cover_filename.rsplit(' .', 1)
# Query all the images in the page
+ try:
wikipedia_response = self.request(
self.WIKIPEDIA_URL,
params={
@@ -595,6 +611,9 @@ class Wikipedia(RemoteArtSource):
},
headers={'content-type': 'application/json'},
)
+ except requests.RequestException:
+ self._log.debug(u'wikipedia: error receiving response')
+ return
# Try to see if one of the images on the pages matches our
# incomplete cover_filename
@@ -613,6 +632,7 @@ class Wikipedia(RemoteArtSource):
return
# Find the absolute url of the cover art on Wikipedia
+ try:
wikipedia_response = self.request(
self.WIKIPEDIA_URL,
params={
@@ -625,6 +645,9 @@ class Wikipedia(RemoteArtSource):
},
headers={'content-type': 'application/json'},
)
+ except requests.RequestException:
+ self._log.debug(u'wikipedia: error receiving response')
+ return
try:
data = wikipedia_response.json()
