Add error handling for empty libraries (closes #43)
This commit is contained in:
parent
562b9fed9c
commit
035a122422
2 changed files with 31 additions and 33 deletions
|
|
@ -54,7 +54,10 @@ class LibraryMixin:
|
||||||
response = self._send_request(endpoint, body)
|
response = self._send_request(endpoint, body)
|
||||||
results = find_object_by_key(nav(response, SINGLE_COLUMN_TAB + SECTION_LIST),
|
results = find_object_by_key(nav(response, SINGLE_COLUMN_TAB + SECTION_LIST),
|
||||||
'itemSectionRenderer')
|
'itemSectionRenderer')
|
||||||
results = nav(results, ITEM_SECTION)['musicShelfRenderer']
|
results = nav(results, ITEM_SECTION)
|
||||||
|
if 'musicShelfRenderer' not in results:
|
||||||
|
return []
|
||||||
|
results = results['musicShelfRenderer']
|
||||||
songs = parse_playlist_items(results['contents'][1:])
|
songs = parse_playlist_items(results['contents'][1:])
|
||||||
|
|
||||||
if 'continuations' in results:
|
if 'continuations' in results:
|
||||||
|
|
@ -84,8 +87,7 @@ class LibraryMixin:
|
||||||
results = nav(results, ITEM_SECTION)
|
results = nav(results, ITEM_SECTION)
|
||||||
if 'gridRenderer' not in results:
|
if 'gridRenderer' not in results:
|
||||||
return []
|
return []
|
||||||
else:
|
results = results['gridRenderer']
|
||||||
results = results['gridRenderer']
|
|
||||||
albums = parse_albums(results['items'], False)
|
albums = parse_albums(results['items'], False)
|
||||||
|
|
||||||
if 'continuations' in results:
|
if 'continuations' in results:
|
||||||
|
|
@ -118,20 +120,9 @@ class LibraryMixin:
|
||||||
body = {'browseId': 'FEmusic_library_corpus_track_artists'}
|
body = {'browseId': 'FEmusic_library_corpus_track_artists'}
|
||||||
endpoint = 'browse'
|
endpoint = 'browse'
|
||||||
response = self._send_request(endpoint, body)
|
response = self._send_request(endpoint, body)
|
||||||
results = find_object_by_key(nav(response, SINGLE_COLUMN_TAB + SECTION_LIST),
|
return parse_library_artists(
|
||||||
'itemSectionRenderer')
|
response,
|
||||||
results = nav(results, ITEM_SECTION)['musicShelfRenderer']
|
lambda additionalParams: self._send_request(endpoint, body, additionalParams), limit)
|
||||||
artists = parse_artists(results['contents'])
|
|
||||||
|
|
||||||
if 'continuations' in results:
|
|
||||||
request_func = lambda additionalParams: self._send_request(
|
|
||||||
endpoint, body, additionalParams)
|
|
||||||
parse_func = lambda contents: parse_artists(contents)
|
|
||||||
artists.extend(
|
|
||||||
get_continuations(results, 'musicShelfContinuation', 25, limit, request_func,
|
|
||||||
parse_func))
|
|
||||||
|
|
||||||
return artists
|
|
||||||
|
|
||||||
def get_library_subscriptions(self, limit: int = 25) -> List[Dict]:
|
def get_library_subscriptions(self, limit: int = 25) -> List[Dict]:
|
||||||
"""
|
"""
|
||||||
|
|
@ -144,20 +135,9 @@ class LibraryMixin:
|
||||||
body = {'browseId': 'FEmusic_library_corpus_artists'}
|
body = {'browseId': 'FEmusic_library_corpus_artists'}
|
||||||
endpoint = 'browse'
|
endpoint = 'browse'
|
||||||
response = self._send_request(endpoint, body)
|
response = self._send_request(endpoint, body)
|
||||||
results = find_object_by_key(nav(response, SINGLE_COLUMN_TAB + SECTION_LIST),
|
return parse_library_artists(
|
||||||
'itemSectionRenderer')
|
response,
|
||||||
results = nav(results, ITEM_SECTION)['musicShelfRenderer']
|
lambda additionalParams: self._send_request(endpoint, body, additionalParams), limit)
|
||||||
artists = parse_artists(results['contents'])
|
|
||||||
|
|
||||||
if 'continuations' in results:
|
|
||||||
request_func = lambda additionalParams: self._send_request(
|
|
||||||
endpoint, body, additionalParams)
|
|
||||||
parse_func = lambda contents: parse_artists(contents)
|
|
||||||
artists.extend(
|
|
||||||
get_continuations(results, 'musicShelfContinuation', 25, limit, request_func,
|
|
||||||
parse_func))
|
|
||||||
|
|
||||||
return artists
|
|
||||||
|
|
||||||
def get_liked_songs(self, limit: int = 100) -> Dict:
|
def get_liked_songs(self, limit: int = 100) -> Dict:
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -267,6 +267,24 @@ def parse_uploaded_items(results):
|
||||||
return songs
|
return songs
|
||||||
|
|
||||||
|
|
||||||
|
def parse_library_artists(response, request_func, limit):
|
||||||
|
results = find_object_by_key(nav(response, SINGLE_COLUMN_TAB + SECTION_LIST),
|
||||||
|
'itemSectionRenderer')
|
||||||
|
results = nav(results, ITEM_SECTION)
|
||||||
|
if 'musicShelfRenderer' not in results:
|
||||||
|
return []
|
||||||
|
results = results['musicShelfRenderer']
|
||||||
|
artists = parse_artists(results['contents'])
|
||||||
|
|
||||||
|
if 'continuations' in results:
|
||||||
|
parse_func = lambda contents: parse_artists(contents)
|
||||||
|
artists.extend(
|
||||||
|
get_continuations(results, 'musicShelfContinuation', 25, limit, request_func,
|
||||||
|
parse_func))
|
||||||
|
|
||||||
|
return artists
|
||||||
|
|
||||||
|
|
||||||
def parse_song_artists(data, index):
|
def parse_song_artists(data, index):
|
||||||
flex_item = get_flex_column_item(data, index)
|
flex_item = get_flex_column_item(data, index)
|
||||||
if not flex_item:
|
if not flex_item:
|
||||||
|
|
@ -332,7 +350,7 @@ def nav(root, items, none_if_absent=False):
|
||||||
if none_if_absent:
|
if none_if_absent:
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
raise err;
|
raise err
|
||||||
|
|
||||||
|
|
||||||
def find_object_by_key(object_list, key, nested=None, is_key=False):
|
def find_object_by_key(object_list, key, nested=None, is_key=False):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue