Added optional validate_reponses param to get_library_songs

This commit is contained in:
czifumasa 2020-08-30 17:09:19 +02:00
commit 6d8bbc006d

View file

@ -41,12 +41,14 @@ class LibraryMixin:
return playlists return playlists
def get_library_songs(self, limit: int = 25) -> List[Dict]: def get_library_songs(self, limit: int = 25, validate_responses: bool = False) -> List[Dict]:
""" """
Gets the songs in the user's library (liked videos are not included). Gets the songs in the user's library (liked videos are not included).
To get liked songs and videos, use :py:func:`get_liked_songs` To get liked songs and videos, use :py:func:`get_liked_songs`
:param limit: Number of songs to retrieve :param limit: Number of songs to retrieve
:param validate_responses: Flag indicating if responses from YTM should be validated and retried in case
when some songs are missing. Default: False
:return: List of songs. Same format as :py:func:`get_playlist` :return: List of songs. Same format as :py:func:`get_playlist`
""" """
self._check_auth() self._check_auth()
@ -56,9 +58,13 @@ class LibraryMixin:
request_func = lambda additionalParams: self._send_request(endpoint, body) request_func = lambda additionalParams: self._send_request(endpoint, body)
parse_func = lambda raw_response: parse_library_songs(raw_response) parse_func = lambda raw_response: parse_library_songs(raw_response)
validate_func = lambda parsed: validate_response(parsed, per_page, limit, 0)
response = resend_request_until_parsed_response_is_valid(request_func, None, parse_func, if validate_responses:
validate_func, 3) validate_func = lambda parsed: validate_response(parsed, per_page, limit, 0)
response = resend_request_until_parsed_response_is_valid(request_func, None,
parse_func, validate_func, 3)
else:
response = parse_func(request_func(None))
results = response['results'] results = response['results']
songs = response['parsed'] songs = response['parsed']
@ -67,10 +73,17 @@ class LibraryMixin:
request_continuations_func = lambda additionalParams: self._send_request( request_continuations_func = lambda additionalParams: self._send_request(
endpoint, body, additionalParams) endpoint, body, additionalParams)
parse_continuations_func = lambda contents: parse_playlist_items(contents) parse_continuations_func = lambda contents: parse_playlist_items(contents)
songs.extend(
get_validated_continuations(results, 'musicShelfContinuation', limit - len(songs), if validate_responses:
per_page, request_continuations_func, songs.extend(
parse_continuations_func)) get_validated_continuations(results, 'musicShelfContinuation',
limit - len(songs), per_page,
request_continuations_func,
parse_continuations_func))
else:
songs.extend(
get_continuations(results, 'musicShelfContinuation', limit - len(songs),
request_continuations_func, parse_continuations_func))
return songs return songs