From 8f7970b2643379c26f2cfcc739df69b31baabd7b Mon Sep 17 00:00:00 2001 From: sigma67 Date: Mon, 18 Dec 2023 20:34:36 +0100 Subject: [PATCH] get_library_songs, get_library_upload_songs: fix empty result for libraries containing exactly 1 song --- ytmusicapi/mixins/uploads.py | 7 +++++-- ytmusicapi/parsers/library.py | 9 ++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/ytmusicapi/mixins/uploads.py b/ytmusicapi/mixins/uploads.py index e63bb19..924dc9d 100644 --- a/ytmusicapi/mixins/uploads.py +++ b/ytmusicapi/mixins/uploads.py @@ -7,12 +7,14 @@ from ytmusicapi.helpers import * from ytmusicapi.navigation import * from ytmusicapi.continuations import get_continuations -from ytmusicapi.parsers.library import parse_library_albums, parse_library_artists, get_library_contents +from ytmusicapi.parsers.library import parse_library_albums, parse_library_artists, get_library_contents, \ + pop_songs_random_mix from ytmusicapi.parsers.albums import parse_album_header from ytmusicapi.parsers.uploads import parse_uploaded_items class UploadsMixin: + def get_library_upload_songs(self, limit: int = 25, order: str = None) -> List[Dict]: """ Returns a list of uploaded songs @@ -44,9 +46,10 @@ def get_library_upload_songs(self, limit: int = 25, order: str = None) -> List[D body["params"] = prepare_order_params(order) response = self._send_request(endpoint, body) results = get_library_contents(response, MUSIC_SHELF) + pop_songs_random_mix(results['contents']) if results is None: return [] - songs = parse_uploaded_items(results['contents'][1:]) + songs = parse_uploaded_items(results['contents']) if 'continuations' in results: request_func = lambda additionalParams: self._send_request( diff --git a/ytmusicapi/parsers/library.py b/ytmusicapi/parsers/library.py index 1fa35c1..48249f0 100644 --- a/ytmusicapi/parsers/library.py +++ b/ytmusicapi/parsers/library.py @@ -75,11 +75,18 @@ def parse_library_artists(response, request_func, limit): return artists +def pop_songs_random_mix(results) -> None: + """remove the random mix that conditionally appears at the start of library songs""" + if len(results) >= 2: + results.pop(0) + + def parse_library_songs(response): results = get_library_contents(response, MUSIC_SHELF) + pop_songs_random_mix(results['contents']) return { 'results': results, - 'parsed': parse_playlist_items(results['contents'][1:]) if results else results + 'parsed': parse_playlist_items(results['contents']) if results else results }