Improved Spotify playlist querying to be a little less janky
This commit is contained in:
parent
38b40b676b
commit
2aca3a8d3a
|
|
@ -11,7 +11,7 @@ if discord.__version__ != '2.0.0a':
|
||||||
"using 'pip install git+https://github.com/Rapptz/discord.py@master'"
|
"using 'pip install git+https://github.com/Rapptz/discord.py@master'"
|
||||||
)
|
)
|
||||||
|
|
||||||
__version__ = "1.0.6.2"
|
__version__ = "1.0.7.1"
|
||||||
__title__ = "pomice"
|
__title__ = "pomice"
|
||||||
__author__ = "cloudwithax"
|
__author__ = "cloudwithax"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -77,44 +77,28 @@ class Client:
|
||||||
return Album(album_data)
|
return Album(album_data)
|
||||||
|
|
||||||
elif spotify_type == "playlist":
|
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}"
|
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 = []
|
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:
|
async with self.session.get(request_url, headers=self._bearer_headers) as resp:
|
||||||
if resp.status != 200:
|
if resp.status != 200:
|
||||||
raise SpotifyRequestException(resp.status, resp.reason)
|
raise SpotifyRequestException(resp.status, resp.reason)
|
||||||
|
|
||||||
playlist_data: dict = await resp.json()
|
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
|
tracks += [Track(track["track"]) for track in playlist_data["tracks"]["items"]]
|
||||||
total_tracks: int = playlist_data["tracks"]["total"]
|
|
||||||
|
|
||||||
# This section of code may look spammy, but trust me, it's not
|
next_page_url = playlist_data["tracks"]["next"]
|
||||||
while len(tracks) < total_tracks:
|
|
||||||
tracks_request_url = f"https://api.spotify.com/v1/playlists/{spotify_id}/tracks?offset={offset}&limit=100"
|
while next_page_url != None:
|
||||||
async with self.session.get(tracks_request_url, headers=self._bearer_headers) as resp:
|
async with self.session.get(next_page_url, headers=self._bearer_headers) as resp:
|
||||||
if resp.status != 200:
|
if resp.status != 200:
|
||||||
raise SpotifyRequestException(resp.status, resp.reason)
|
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..
|
tracks += [Track(track["track"]) for track in next_page_data["items"]]
|
||||||
# Add the tracks we got from the current page of results
|
next_page_url = next_page_data["next"]
|
||||||
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
|
|
||||||
|
|
||||||
# We have all the tracks, cast to the class for easier reading
|
|
||||||
return Playlist(playlist_data, tracks)
|
return Playlist(playlist_data, tracks)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
2
setup.py
2
setup.py
|
|
@ -6,7 +6,7 @@ with open("README.md") as f:
|
||||||
setuptools.setup(
|
setuptools.setup(
|
||||||
name="pomice",
|
name="pomice",
|
||||||
author="cloudwithax",
|
author="cloudwithax",
|
||||||
version="1.0.7",
|
version="1.0.7.1",
|
||||||
url="https://github.com/cloudwithax/pomice",
|
url="https://github.com/cloudwithax/pomice",
|
||||||
packages=setuptools.find_packages(),
|
packages=setuptools.find_packages(),
|
||||||
license="GPL",
|
license="GPL",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue