1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-23 01:16:18 +00:00

Handle LastFM connection errors

This commit is contained in:
spl0k 2014-04-05 16:03:43 +02:00
parent 7a060c9186
commit 62e0ca69f4

View File

@ -35,7 +35,9 @@ class LastFm:
return False, 'No API key set' return False, 'No API key set'
res = self.__api_request(False, method = 'auth.getSession', token = token) res = self.__api_request(False, method = 'auth.getSession', token = token)
if 'error' in res: if not res:
return False, 'Error connecting to LastFM'
elif 'error' in res:
return False, 'Error %i: %s' % (res['error'], res['message']) return False, 'Error %i: %s' % (res['error'], res['message'])
else: else:
self.__user.lastfm_session = res['session']['key'] self.__user.lastfm_session = res['session']['key']
@ -52,14 +54,14 @@ class LastFm:
if not self.__enabled: if not self.__enabled:
return return
res = self.__api_request(True, method = 'track.updateNowPlaying', artist = track.album.artist.name, track = track.title, album = track.album.name, self.__api_request(True, method = 'track.updateNowPlaying', artist = track.album.artist.name, track = track.title, album = track.album.name,
trackNumber = track.number, duration = track.duration) trackNumber = track.number, duration = track.duration)
def scrobble(self, track, ts): def scrobble(self, track, ts):
if not self.__enabled: if not self.__enabled:
return return
res = self.__api_request(True, method = 'track.scrobble', artist = track.album.artist.name, track = track.title, album = track.album.name, self.__api_request(True, method = 'track.scrobble', artist = track.album.artist.name, track = track.title, album = track.album.name,
timestamp = ts, trackNumber = track.number, duration = track.duration) timestamp = ts, trackNumber = track.number, duration = track.duration)
def __api_request(self, write, **kwargs): def __api_request(self, write, **kwargs):
@ -84,10 +86,14 @@ class LastFm:
kwargs['api_sig'] = sig kwargs['api_sig'] = sig
kwargs['format'] = 'json' kwargs['format'] = 'json'
if write: try:
r = requests.post('http://ws.audioscrobbler.com/2.0/', data = kwargs) if write:
else: r = requests.post('http://ws.audioscrobbler.com/2.0/', data = kwargs)
r = requests.get('http://ws.audioscrobbler.com/2.0/', params = kwargs) else:
r = requests.get('http://ws.audioscrobbler.com/2.0/', params = kwargs)
except requests.exceptions.RequestException, e:
self.__logger.warn('Error while connecting to LastFM: ' + str(e))
return None
json = r.json() json = r.json()
if 'error' in json: if 'error' in json: