Made connection to subsonic more safe by handling exceptions occuring from python-pysonic.
This commit is contained in:
parent
0eb8795263
commit
b1fa8920c4
1 changed files with 64 additions and 17 deletions
|
@ -42,17 +42,24 @@ class SubsonicApi():
|
||||||
return template % (self.url, song_id, self.username, self.password)
|
return template % (self.url, song_id, self.username, self.password)
|
||||||
|
|
||||||
def find_raw(self, query, exclude_artists=False, exclude_albums=False, exclude_songs=False):
|
def find_raw(self, query, exclude_artists=False, exclude_albums=False, exclude_songs=False):
|
||||||
|
try:
|
||||||
response = self.connection.search2(
|
response = self.connection.search2(
|
||||||
query.encode('utf-8'),
|
query.encode('utf-8'),
|
||||||
MAX_SEARCH_RESULTS if not exclude_artists else 0, 0,
|
MAX_SEARCH_RESULTS if not exclude_artists else 0, 0,
|
||||||
MAX_SEARCH_RESULTS if not exclude_albums else 0, 0,
|
MAX_SEARCH_RESULTS if not exclude_albums else 0, 0,
|
||||||
MAX_SEARCH_RESULTS if not exclude_songs else 0, 0)
|
MAX_SEARCH_RESULTS if not exclude_songs else 0, 0)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning('Connecting to subsonic failed when searching.')
|
||||||
|
return None
|
||||||
if response.get('status') != RESPONSE_OK:
|
if response.get('status') != RESPONSE_OK:
|
||||||
|
logger.warning('Got non-okay status code from subsonic: %s' % response.get('status'))
|
||||||
return None
|
return None
|
||||||
return response.get('searchResult2')
|
return response.get('searchResult2')
|
||||||
|
|
||||||
def find_as_search_result(self, query, exclude_artists=False, exclude_albums=False, exclude_songs=False):
|
def find_as_search_result(self, query, exclude_artists=False, exclude_albums=False, exclude_songs=False):
|
||||||
result = self.find_raw(query)
|
result = self.find_raw(query)
|
||||||
|
if result is None:
|
||||||
|
return None
|
||||||
return SearchResult(
|
return SearchResult(
|
||||||
uri=uri.get_search_uri(query),
|
uri=uri.get_search_uri(query),
|
||||||
artists=[self.raw_artist_to_artist(artist) for artist in result.get('artist') or []],
|
artists=[self.raw_artist_to_artist(artist) for artist in result.get('artist') or []],
|
||||||
|
@ -61,48 +68,88 @@ class SubsonicApi():
|
||||||
|
|
||||||
|
|
||||||
def get_raw_artists(self):
|
def get_raw_artists(self):
|
||||||
|
try:
|
||||||
response = self.connection.getIndexes()
|
response = self.connection.getIndexes()
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning('Connecting to subsonic failed when loading list of artists.')
|
||||||
|
return []
|
||||||
if response.get('status') != RESPONSE_OK:
|
if response.get('status') != RESPONSE_OK:
|
||||||
return None
|
logger.warning('Got non-okay status code from subsonic: %s' % response.get('status'))
|
||||||
|
return []
|
||||||
letters = response.get('indexes').get('index')
|
letters = response.get('indexes').get('index')
|
||||||
if letters is not None:
|
if letters is not None:
|
||||||
artists = [artist for letter in letters for artist in letter.get('artist')]
|
artists = [artist for letter in letters for artist in letter.get('artist') or []]
|
||||||
return artists
|
return artists
|
||||||
return None
|
logger.warning('Subsonic does not seem to have any artists in it\'s library.')
|
||||||
|
return []
|
||||||
|
|
||||||
def get_song_by_id(self, song_id):
|
def get_song_by_id(self, song_id):
|
||||||
|
try:
|
||||||
response = self.connection.getSong(song_id)
|
response = self.connection.getSong(song_id)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning('Connecting to subsonic failed when loading song by id.')
|
||||||
|
return None
|
||||||
if response.get('status') != RESPONSE_OK:
|
if response.get('status') != RESPONSE_OK:
|
||||||
|
logger.warning('Got non-okay status code from subsonic: %s' % response.get('status'))
|
||||||
return None
|
return None
|
||||||
return self.raw_song_to_track(response.get('song')) if response.get('song') is not None else None
|
return self.raw_song_to_track(response.get('song')) if response.get('song') is not None else None
|
||||||
|
|
||||||
def get_album_by_id(self, album_id):
|
def get_album_by_id(self, album_id):
|
||||||
|
try:
|
||||||
response = self.connection.getAlbum(album_id)
|
response = self.connection.getAlbum(album_id)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning('Connecting to subsonic failed when loading album by id.')
|
||||||
|
return None
|
||||||
if response.get('status') != RESPONSE_OK:
|
if response.get('status') != RESPONSE_OK:
|
||||||
|
logger.warning('Got non-okay status code from subsonic: %s' % response.get('status'))
|
||||||
return None
|
return None
|
||||||
return self.raw_album_to_album(response.get('album')) if response.get('album') is not None else None
|
return self.raw_album_to_album(response.get('album')) if response.get('album') is not None else None
|
||||||
|
|
||||||
def get_artist_by_id(self, artist_id):
|
def get_artist_by_id(self, artist_id):
|
||||||
|
try:
|
||||||
response = self.connection.getArtist(artist_id)
|
response = self.connection.getArtist(artist_id)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning('Connecting to subsonic failed when loading artist by id.')
|
||||||
|
return None
|
||||||
if response.get('status') != RESPONSE_OK:
|
if response.get('status') != RESPONSE_OK:
|
||||||
|
logger.warning('Got non-okay status code from subsonic: %s' % response.get('status'))
|
||||||
return None
|
return None
|
||||||
return self.raw_artist_to_artist(response.get('artist')) if response.get('artist') is not None else None
|
return self.raw_artist_to_artist(response.get('artist')) if response.get('artist') is not None else None
|
||||||
|
|
||||||
def get_raw_playlists(self):
|
def get_raw_playlists(self):
|
||||||
|
try:
|
||||||
response = self.connection.getPlaylists()
|
response = self.connection.getPlaylists()
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning('Connecting to subsonic failed when loading list of playlists.')
|
||||||
|
return []
|
||||||
if response.get('status') != RESPONSE_OK:
|
if response.get('status') != RESPONSE_OK:
|
||||||
return None
|
logger.warning('Got non-okay status code from subsonic: %s' % response.get('status'))
|
||||||
return response.get('playlists').get('playlist')
|
return []
|
||||||
|
playlists = response.get('playlists').get('playlist')
|
||||||
|
if playlists is None:
|
||||||
|
logger.warning('Subsonic does not seem to have any playlists in it\'s library.')
|
||||||
|
return []
|
||||||
|
return playlists
|
||||||
|
|
||||||
def get_raw_playlist(self, playlist_id):
|
def get_raw_playlist(self, playlist_id):
|
||||||
|
try:
|
||||||
response = self.connection.getPlaylist(playlist_id)
|
response = self.connection.getPlaylist(playlist_id)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning('Connecting to subsonic failed when loading playlist.')
|
||||||
|
return None
|
||||||
if response.get('status') != RESPONSE_OK:
|
if response.get('status') != RESPONSE_OK:
|
||||||
|
logger.warning('Got non-okay status code from subsonic: %s' % response.get('status'))
|
||||||
return None
|
return None
|
||||||
return response.get('playlist')
|
return response.get('playlist')
|
||||||
|
|
||||||
def get_raw_dir(self, parent_id):
|
def get_raw_dir(self, parent_id):
|
||||||
|
try:
|
||||||
response = self.connection.getMusicDirectory(parent_id)
|
response = self.connection.getMusicDirectory(parent_id)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning('Connecting to subsonic failed when listing content of music directory.')
|
||||||
|
return None
|
||||||
if response.get('status') != RESPONSE_OK:
|
if response.get('status') != RESPONSE_OK:
|
||||||
|
logger.warning('Got non-okay status code from subsonic: %s' % response.get('status'))
|
||||||
return None
|
return None
|
||||||
directory = response.get('directory')
|
directory = response.get('directory')
|
||||||
if directory is not None:
|
if directory is not None:
|
||||||
|
|
Loading…
Add table
Reference in a new issue