Fix critical bug in iter_playlist_tracks wave iteration logic

- Fixed TypeError where next() was incorrectly called on a range object
- Replaced flawed iterator manipulation with clean list-based slicing
- Maintains same wave-based pagination functionality for efficiency
- Ensures proper handling of large Spotify playlists with multiple pages
This commit is contained in:
wizardoesmagic 2025-12-28 07:48:19 +00:00
parent 9bffdebe25
commit c48ac5716b
1 changed files with 10 additions and 19 deletions

View File

@ -286,25 +286,16 @@ class Client:
# Fetch pages in rolling waves; yield promptly as soon as a wave completes. # Fetch pages in rolling waves; yield promptly as soon as a wave completes.
wave_size = self._playlist_concurrency * 2 wave_size = self._playlist_concurrency * 2
for i, offset in enumerate(remaining_offsets): remaining_offsets_list = list(remaining_offsets)
# Build wave
if i % wave_size == 0: for i in range(0, len(remaining_offsets_list), wave_size):
wave_offsets = list( wave_offsets = remaining_offsets_list[i : i + wave_size]
o for o in remaining_offsets if o >= offset and o < offset + wave_size results = await asyncio.gather(*[fetch(o) for o in wave_offsets])
) for page_tracks in results:
results = await asyncio.gather(*[fetch(o) for o in wave_offsets]) if not page_tracks:
for page_tracks in results: continue
if not page_tracks: for j in range(0, len(page_tracks), batch_size):
continue yield page_tracks[j : j + batch_size]
for j in range(0, len(page_tracks), batch_size):
yield page_tracks[j : j + batch_size]
# Skip ahead in iterator by adjusting enumerate drive (consume extras)
# Fast-forward the generator manually
for _ in range(len(wave_offsets) - 1):
try:
next(remaining_offsets) # type: ignore
except StopIteration:
break
async def get_recommendations(self, *, query: str) -> List[Track]: async def get_recommendations(self, *, query: str) -> List[Track]:
if not self._bearer_token or time.time() >= self._expiry: if not self._bearer_token or time.time() >= self._expiry: