Skip to content

Commit

Permalink
Fix continuations to always retrieve at least the specified limit, ev…
Browse files Browse the repository at this point in the history
…en if YTMusic returns less than per_page
  • Loading branch information
sigma67 committed Jul 15, 2020
1 parent 2e87279 commit 90bc753
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
6 changes: 3 additions & 3 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ def test_get_library_playlists(self):
self.assertGreater(len(playlists), 0)

def test_get_library_songs(self):
songs = youtube_brand.get_library_songs(200)
songs = youtube_brand.get_library_songs(100)
self.assertGreater(len(songs), 0)

def test_get_library_albums(self):
albums = youtube_brand.get_library_albums(50)
albums = youtube_brand.get_library_albums(100)
self.assertGreater(len(albums), 0)

def test_get_library_artists(self):
artists = youtube_brand.get_library_artists(50)
artists = youtube_brand.get_library_artists(100)
self.assertGreater(len(artists), 0)

def test_get_library_subscriptions(self):
Expand Down
4 changes: 1 addition & 3 deletions ytmusicapi/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,16 +310,14 @@ def get_browse_id(item, index):


def get_continuations(results, continuation_type, per_page, limit, request_func, parse_func):
request_count = 1
items = []
while 'continuations' in results and request_count * per_page < limit:
while 'continuations' in results and len(items) < limit - per_page:
ctoken = nav(results, CONTINUATION)
additionalParams = "&ctoken=" + ctoken + "&continuation=" + ctoken
response = request_func(additionalParams)
results = response['continuationContents'][continuation_type]
continuation_contents = 'contents' if 'Shelf' in continuation_type else 'items'
items.extend(parse_func(results[continuation_contents]))
request_count += 1

return items

Expand Down

0 comments on commit 90bc753

Please sign in to comment.