From 2aca3a8d3aedd00861f9f57a3e0e2cc954eaba00 Mon Sep 17 00:00:00 2001 From: cloudwithax Date: Wed, 20 Oct 2021 19:54:39 -0400 Subject: [PATCH] Improved Spotify playlist querying to be a little less janky --- pomice/__init__.py | 2 +- pomice/spotify/client.py | 32 ++++++++------------------------ setup.py | 2 +- 3 files changed, 10 insertions(+), 26 deletions(-) diff --git a/pomice/__init__.py b/pomice/__init__.py index 9eeae14..258ce0f 100644 --- a/pomice/__init__.py +++ b/pomice/__init__.py @@ -11,7 +11,7 @@ if discord.__version__ != '2.0.0a': "using 'pip install git+https://github.com/Rapptz/discord.py@master'" ) -__version__ = "1.0.6.2" +__version__ = "1.0.7.1" __title__ = "pomice" __author__ = "cloudwithax" diff --git a/pomice/spotify/client.py b/pomice/spotify/client.py index 438769b..4861f57 100644 --- a/pomice/spotify/client.py +++ b/pomice/spotify/client.py @@ -77,44 +77,28 @@ class Client: return Album(album_data) elif spotify_type == "playlist": - # Okay i know this looks like a mess, but hear me out, this works - # The Spotify Web API limits how many tracks can be seen in a single request to 100 - # So we have to do some clever techniques to get all the tracks in any playlist larger than 100 songs - # This method doesn't need to be applied to albums due to the fact that 99% of albums - # are never more than 100 tracks (I'm looking at you, Deep Zone Project...) - request_url = f"https://api.spotify.com/v1/playlists/{spotify_id}" - # Set the offset now so we can change it when we get all the tracks - offset = 0 tracks = [] - - # First, get the playlist data so we can get the total amount of tracks for later async with self.session.get(request_url, headers=self._bearer_headers) as resp: if resp.status != 200: raise SpotifyRequestException(resp.status, resp.reason) playlist_data: dict = await resp.json() - # Second, get the total amount of tracks in said playlist so we can use this to get all the tracks - total_tracks: int = playlist_data["tracks"]["total"] + tracks += [Track(track["track"]) for track in playlist_data["tracks"]["items"]] - # This section of code may look spammy, but trust me, it's not - while len(tracks) < total_tracks: - tracks_request_url = f"https://api.spotify.com/v1/playlists/{spotify_id}/tracks?offset={offset}&limit=100" - async with self.session.get(tracks_request_url, headers=self._bearer_headers) as resp: + next_page_url = playlist_data["tracks"]["next"] + + while next_page_url != None: + async with self.session.get(next_page_url, headers=self._bearer_headers) as resp: if resp.status != 200: raise SpotifyRequestException(resp.status, resp.reason) - playlist_track_data: dict = await resp.json() + next_page_data: dict = await resp.json() - # This is the juicy part.. - # Add the tracks we got from the current page of results - tracks += [Track(track["track"]) for track in playlist_track_data["items"]] - # Set the offset to go to the next page - offset += 100 - # Repeat until we have all the tracks + tracks += [Track(track["track"]) for track in next_page_data["items"]] + next_page_url = next_page_data["next"] - # We have all the tracks, cast to the class for easier reading return Playlist(playlist_data, tracks) diff --git a/setup.py b/setup.py index 8429816..94c6f2d 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ with open("README.md") as f: setuptools.setup( name="pomice", author="cloudwithax", - version="1.0.7", + version="1.0.7.1", url="https://github.com/cloudwithax/pomice", packages=setuptools.find_packages(), license="GPL",