Skip to content

Commit

Permalink
get_library_songs, get_library_upload_songs: fix empty result for lib…
Browse files Browse the repository at this point in the history
…raries containing exactly 1 song
  • Loading branch information
sigma67 committed Dec 18, 2023
1 parent 43d3c3b commit 8f7970b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
7 changes: 5 additions & 2 deletions ytmusicapi/mixins/uploads.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
9 changes: 8 additions & 1 deletion ytmusicapi/parsers/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


Expand Down

0 comments on commit 8f7970b

Please sign in to comment.