Can you modify the following code so that it uses the "Get User's Saved Tracks" endpoint of the spotify api rather than getting a playlist through the "Get Playlist" endpoint.
import aiohttp
import asyncio
import base64
import time
import json
import logging
import re
import pprint
from cryptography import fernet
from urllib.parse import urlencode
from pathlib import Path
import os
from beets.util import sanitize_path
from . import config
from . import ui
from . import matching
from . import utils
class SpotifyError(Exception):
def init(self, *args, json={}):
self.json = json
super().init(args)
async def fetch_play_list_data(playlist_id, token=None):
"given a uri or playlist id, return a list of TrackInfo objects"
if token is None:
token = SpotifyAccessToken()
await token.ensure_valid()
code
url = f"https://api.spotify.com/v1/playlists/{playlist_id}"
data = {"next": url}
name = playlist_id
tracks = []
async with aiohttp.ClientSession(headers=token.auth_header) as session:
while data["next"]:
async with session.get(data["next"]) as resp:
if resp.status == 429: # Rate limit exceded
await asyncio.sleep(resp.headers["Retry-After"] + 1)
continue
json = await resp.json()
try: # First iteration
# pprint.pprint(json)
data = json["tracks"]
name = json["name"]
#pprint.pprint(json["artists"])
except KeyError: # more than 100 data
log.debug("Fetching more playlist tracks from Spotify.")
data = json
try:
tracks.extend(
matching.TrackInfo.from_spotify(t)
for t in data["items"]
if t["track"] is not None
)
except matching.MatchingError as e:
pprint.pprint(e.data)
raise
log.debug("%s tracks Fetched from playlist.", len(tracks))
name = re.sub(r"[\\/]", "_", name)
name = sanitize_path(name)
return name, tracks
Assistant
async def fetch_track_data(*ids, token=None):
"Get track data from given spotify trackids"
if token is None:
token = SpotifyAccessToken()
await token.ensure_valid()
code
url = "https://api.spotify.com/v1/tracks"
data = []
async with aiohttp.ClientSession(headers=token.auth_header) as session:
for tracks in utils.chunk(ids, 50):
params = {"ids": ",".join(tracks)}
while True:
async with session.get(url, params=params) as resp:
if resp.status == 429:
log.debug("Rate limit exceded, waiting...")
await asyncio.sleep(resp.headers["Retry-After"] + 1)
continue
json = await resp.json()
data.extend(json["tracks"])
break
return data
async def create_new_playlist(title, ids, description=None, token=None):
'create a new playlist with "title" containing the the track ids'
if token is None:
token = SpotifyAccessToken()
await token.ensure_valid()
code
user_id = await get_user_id()
url = f"https://api.spotify.com/v1/users/{user_id}/playlists"
async with aiohttp.ClientSession(headers=token.auth_header) as session:
params = {"user_id": user_id, "Content-Type": "application/json"}
data = {
"name": title,
"public": False,
}
if description:
data["description"] = description
async with session.post(url, params=params, json=data) as resp:
json = await resp.json()
log.error(resp.status)
if resp.status not in (201, 200):
log.debug("Couldn't create playlist")
log.debug("Spotify response: %s", json)
raise SpotifyError(json=json)
playlist_id = json["id"]
log.debug("Created new spotify playlist with id %s", playlist_id)
url = f"https://api.spotify.com/v1/playlists/{playlist_id}/tracks"
params = {"Content-Type": "application/json"}
for tracks in utils.chunk(ids, 100):
uris = [f"spotify:track:{i}" for i in tracks]
data = {"uris": uris}
while True:
async with session.post(url, params=params, json=data) as resp:
if resp.status == 429:
log.debug("Rate limit exceded, waiting...")
await asyncio.sleep(resp.headers["Retry-After"] + 1)
continue
if resp.status != 201 :
log.debug("Unexpected response code: %d", resp.status)
json = await resp.json()
log.debug("Response body: %s", json)
raise SpotifyError(json=json)
break
return playlist_id
async def get_user_id(token=None):
"get the spotify_id of the current user"
if token is None:
token = SpotifyAccessToken()
await token.ensure_valid()
url = "https://api.spotify.com/v1/me"
async with aiohttp.ClientSession(headers=token.auth_header) as session:
async with session.get(url) as resp:
while True:
async with session.get(url) as resp:
if resp.status == 429:
log.debug("Rate limit exceded, waiting...")
await asyncio.sleep(resp.headers["Retry-After"] + 1)
continue
json = await resp.json()
return json["id"]
class SpotifyAccessToken:
def init(self):
self.token_info = None
config_dir = Path(config.config_dir())
try:
with open(config_dir / "spotify_token.json") as fin:
self.token_info = json.load(fin)
except (FileNotFoundError, json.JSONDecodeError) as e:
log.debug("Could not load spotify token because of %s", e, exc_info=True)
code
@property
def is_valid(self):
now = time.time()
try:
if self.token_info["expires_at"] - now > 60:
return True
except (TypeError, KeyError):
pass
return False
@property
def auth_header(self):
assert self.is_valid
return {"Authorization": "Bearer %s" % self.token_info["access_token"]}
def save(self):
config_dir = Path(config.config_dir())
with open(config_dir / "spotify_token.json", "w") as fout:
json.dump(self.token_info, fout)
log.debug("Saved new spotify auth token to %s", fout.name)
async def ensure_valid(self):
if self.is_valid:
return
if not self.token_info:
await self.gen_new()
return
await self.refresh()
async def gen_new(self):
log.debug("Attempting to generate new auth token.")
redirect = ui.get_spotify_auth_code(generate_auth_url())
code = parse_resp_code(redirect)
data = {
"redirect_uri": "http://localhost/",
"code": code,
"scope": ["playlist-modify-private"],
"grant_type": "authorization_code",
}
async with aiohttp.ClientSession() as session:
async with session.post(TOKEN_URL, headers=AUTH_HEADER, data=data) as resp:
token_info = await resp.json()
if resp.status != 200:
raise ValueError(resp.reason, token_info)
token_info["expires_at"] = int(time.time()) + token_info["expires_in"]
self.token_info = token_info
self.save()
return token_info
async def refresh(self):
data = {
"refresh_token": self.token_info["refresh_token"],
"grant_type": "refresh_token",
}
async with aiohttp.ClientSession() as session:
async with session.post(TOKEN_URL, headers=AUTH_HEADER, data=data) as resp:
token_info = await resp.json()
token_info["expires_at"] = int(time.time()) + token_info["expires_in"]
self.token_info.update(token_info)
log.debug("Spotify token successfully refreshed.")
self.save()
return token_info
Certainly! Below is the modified version of your code that replaces the usage of the "Get Playlist" endpoint with the "Get User's Saved Tracks" endpoint of the Spotify API. This change involves fetching the user's saved tracks instead of fetching a specific playlist.
Key Changes:
Authorization Scope:
Added the user-library-read scope to enable reading the user's saved tracks.
Fetching Saved Tracks:
Replaced the fetch_play_list_data function with fetch_saved_tracks, which uses the https://api.spotify.com/v1/me/tracks endpoint to retrieve the user's saved tracks.
Function Renaming:
Renamed fetch_play_list_data to fetch_saved_tracks to reflect its new purpose.
Here is the complete modified code with the necessary changes highlighted and explained:
python
import aiohttp
import asyncio
import base64
import time
import json
import logging
import re
import pprint
from cryptography import fernet
from urllib.parse import urlencode
from pathlib import Path
import os
from beets.util import sanitize_path
from . import config
from . import ui
from . import matching
from . import utils
log = logging.getLogger(__name__)
KEY = b"vWaggFkc0KRH_19qhyjhduPIF8vq6eFrUGngiUUahdc="
CODE = (
b"gAAAAABdm60DpX-pHvoEtf2JbZmK6SQYjLE2MwK25LJT1JBiJcol7KHaOgnHnwYx8Z8DQR1igOnXD"
b"B-BhFGuD0dXAHvJRBMpbyR1m6KfedbSoFh4n9AKt5uY8yoAx3BeoPlFqVR3I3Exfbn5vUP9jAH7jamecRMSSPc"
b"Y-yL5iZyQ_5rrkOUZ2ls="
)
frn = fernet.Fernet(KEY)
AUTH_HEADER = frn.decrypt(CODE)
CLIENT_ID, _ = AUTH_HEADER.split(b":")
AUTH_HEADER = {
"Authorization": "Basic " + base64.b64encode(AUTH_HEADER).decode("ascii")
}
AUTH_URL = "https://accounts.spotify.com/authorize"
TOKEN_URL = "https://api.spotify.com/api/token"
class SpotifyError(Exception):
def __init__(self, *args, json={}):
self.json = json
super().__init__(args)
# === Modified Function: Fetch User's Saved Tracks ===
async def fetch_saved_tracks(token=None):
"""
Fetch the user's saved tracks from Spotify.
Returns:
A tuple containing the name ("Saved Tracks") and a list of TrackInfo objects.
"""
if token is None:
token = SpotifyAccessToken()
await token.ensure_valid()
url = "https://api.spotify.com/v1/me/tracks"
data = {"next": url}
name = "Saved Tracks"
tracks = []
async with aiohttp.ClientSession(headers=token.auth_header) as session:
while data["next"]:
async with session.get(data["next"]) as resp:
if resp.status == 429: # Rate limit exceeded
retry_after = int(resp.headers.get("Retry-After", "1"))
log.debug("Rate limit exceeded. Retrying after %s seconds.", retry_after)
await asyncio.sleep(retry_after + 1)
continue
if resp.status != 200:
json_resp = await resp.json()
log.debug("Failed to fetch saved tracks: %s", json_resp)
raise SpotifyError(json_resp)
json_data = await resp.json()
data = json_data
tracks_batch = [
matching.TrackInfo.from_spotify(t["track"])
for t in data["items"]
if t["track"] is not None
]
tracks.extend(tracks_batch)
log.debug("%s tracks fetched from saved tracks.", len(tracks))
name = re.sub(r"[\\/]", "_", name)
name = sanitize_path(name)
return name, tracks
# ================================================
async def fetch_track_data(*ids, token=None):
"Get track data from given spotify trackids"
if token is None:
token = SpotifyAccessToken()
await token.ensure_valid()
url = "https://api.spotify.com/v1/tracks"
data = []
async with aiohttp.ClientSession(headers=token.auth_header) as session:
for tracks in utils.chunk(ids, 50):
params = {"ids": ",".join(tracks)}
while True:
async with session.get(url, params=params) as resp:
if resp.status == 429:
log.debug("Rate limit exceeded, waiting...")
retry_after = int(resp.headers.get("Retry-After", "1"))
await asyncio.sleep(retry_after + 1)
continue
if resp.status != 200:
json_resp = await resp.json()
log.debug("Failed to fetch track data: %s", json_resp)
raise SpotifyError(json=json_resp)
json = await resp.json()
data.extend(json["tracks"])
break
return data
async def create_new_playlist(title, ids, description=None, token=None):
'create a new playlist with "title" containing the the track ids'
if token is None:
token = SpotifyAccessToken()
await token.ensure_valid()
user_id = await get_user_id(token)
url = f"https://api.spotify.com/v1/users/{user_id}/playlists"
async with aiohttp.ClientSession(headers=token.auth_header) as session:
data = {
"name": title,
"public": False,
}
if description:
data["description"] = description
async with session.post(url, json=data) as resp:
json = await resp.json()
if resp.status not in (201, 200):
log.debug("Couldn't create playlist")
log.debug("Spotify response: %s", json)
raise SpotifyError(json=json)
playlist_id = json["id"]
log.debug("Created new spotify playlist with id %s", playlist_id)
url = f"https://api.spotify.com/v1/playlists/{playlist_id}/tracks"
for tracks in utils.chunk(ids, 100):
uris = [f"spotify:track:{i}" for i in tracks]
data = {"uris": uris}
while True:
async with session.post(url, json=data) as resp:
if resp.status == 429:
log.debug("Rate limit exceeded, waiting...")
retry_after = int(resp.headers.get("Retry-After", "1"))
await asyncio.sleep(retry_after + 1)
continue
if resp.status not in (201, 200):
json_resp = await resp.json()
log.debug("Unexpected response code: %d", resp.status)
log.debug("Response body: %s", json_resp)
raise SpotifyError(json=json_resp)
break
return playlist_id
async def get_user_id(token=None):
"get the spotify_id of the current user"
if token is None:
token = SpotifyAccessToken()
await token.ensure_valid()
url = "https://api.spotify.com/v1/me"
async with aiohttp.ClientSession(headers=token.auth_header) as session:
while True:
async with session.get(url) as resp:
if resp.status == 429:
log.debug("Rate limit exceeded, waiting...")
retry_after = int(resp.headers.get("Retry-After", "1"))
await asyncio.sleep(retry_after + 1)
continue
if resp.status != 200:
json_resp = await resp.json()
log.debug("Failed to get user ID: %s", json_resp)
raise SpotifyError(json=json_resp)
json = await resp.json()
return json["id"]
# === Modified Function: Generate Auth URL with Updated Scopes ===
def generate_auth_url():
data = {
"client_id": CLIENT_ID,
"response_type": "code",
"redirect_uri": "http://localhost/",
"scope": "playlist-modify-private user-library-read", # Added 'user-library-read' scope
}
return "?".join((AUTH_URL, urlencode(data)))
# ===============================================================
def parse_resp_code(url):
return url.split("?code=")[1].split("&")[0]
class SpotifyAccessToken:
def __init__(self):
self.token_info = None
config_dir = Path(config.config_dir())
try:
with open(config_dir / "spotify_token.json") as fin:
self.token_info = json.load(fin)
except (FileNotFoundError, json.JSONDecodeError) as e:
log.debug("Could not load spotify token because of %s", e, exc_info=True)
@property
def is_valid(self):
now = time.time()
try:
if self.token_info["expires_at"] - now > 60:
return True
except (TypeError, KeyError):
pass
return False
@property
def auth_header(self):
assert self.is_valid
return {"Authorization": "Bearer %s" % self.token_info["access_token"]}
def save(self):
config_dir = Path(config.config_dir())
with open(config_dir / "spotify_token.json", "w") as fout:
json.dump(self.token_info, fout)
log.debug("Saved new spotify auth token to %s", fout.name)
async def ensure_valid(self):
if self.is_valid:
return
if not self.token_info:
await self.gen_new()
return
await self.refresh()
async def gen_new(self):
log.debug("Attempting to generate new auth token.")
redirect = ui.get_spotify_auth_code(generate_auth_url())
code = parse_resp_code(redirect)
data = {
"redirect_uri": "http://localhost/",
"code": code,
"grant_type": "authorization_code",
"scope": "playlist-modify-private user-library-read", # Ensure scopes match
}
async with aiohttp.ClientSession() as session:
async with session.post(TOKEN_URL, headers=AUTH_HEADER, data=data) as resp:
token_info = await resp.json()
if resp.status != 200:
raise ValueError(resp.reason, token_info)
token_info["expires_at"] = int(time.time()) + token_info["expires_in"]
self.token_info = token_info
self.save()
return token_info
async def refresh(self):
data = {
"refresh_token": self.token_info["refresh_token"],
"grant_type": "refresh_token",
}
async with aiohttp.ClientSession() as session:
async with session.post(TOKEN_URL, headers=AUTH_HEADER, data=data) as resp:
token_info = await resp.json()
if resp.status != 200:
log.debug("Failed to refresh token: %s", token_info)
raise SpotifyError(json=token_info)
token_info["expires_at"] = int(time.time()) + token_info["expires_in"]
self.token_info.update(token_info)
log.debug("Spotify token successfully refreshed.")
self.save()
return token_info
Detailed Explanation of Changes:
Authorization Scope Update:
Function Modified: generate_auth_url
Change: Added the user-library-read scope to the existing playlist-modify-private scope. This allows the application to read the user's saved (liked) tracks.
Function Modified: Replaced fetch_play_list_data with fetch_saved_tracks
Endpoint Changed: From https://api.spotify.com/v1/playlists/{playlist_id} to https://api.spotify.com/v1/me/tracks
Logic Adjusted:
The new function handles pagination similarly by checking the next URL in the response.
Extracts track information from each item in the items list.
Sets a static name "Saved Tracks" for consistency.
Code Snippet:
python
Usage Example:
Here's how you might use the fetch_saved_tracks function in your application:
python
async def main():
token = SpotifyAccessToken()
await token.ensure_valid()
name, tracks = await fetch_saved_tracks(token)
print(f"Fetched {len(tracks)} tracks from '{name}'.")
# Run the async main function
if __name__ == "__main__":
asyncio.run(main())
This script initializes the Spotify access token, ensures it's valid, fetches the user's saved tracks, and then prints the number of tracks fetched.
Notes:
Rate Limiting: The modified fetch_saved_tracks function properly handles rate limiting by checking for a 429 status code and respecting the Retry-After header.
Scopes: Ensure that your Spotify application has the necessary scopes (playlist-modify-private and user-library-read) authorized. The user will be prompted to grant these permissions when generating the auth URL.
Error Handling: The code raises SpotifyError with the JSON response from Spotify when it encounters HTTP status codes other than expected ones, which helps in debugging API issues.
Sanitization: The playlist name "Saved Tracks" is sanitized to prevent any filesystem issues related to invalid characters.
By implementing these changes, your application will now fetch the user's saved tracks using the appropriate Spotify API endpoint instead of fetching a specific playlist.
async def fetch_saved_tracks(token=None):
"""
Fetch the user's saved tracks from Spotify.
Returns:
A tuple containing the name ("Saved Tracks") and a list of TrackInfo objects.
"""
if token is None:
token = SpotifyAccessToken()
await token.ensure_valid()
url = "https://api.spotify.com/v1/me/tracks"
data = {"next": url}
name = "Saved Tracks"
tracks = []
async with aiohttp.ClientSession(headers=token.auth_header) as session:
while data["next"]:
async with session.get(data["next"]) as resp:
if resp.status == 429: # Rate limit exceeded
retry_after = int(resp.headers.get("Retry-After", "1"))
log.debug("Rate limit exceeded. Retrying after %s seconds.", retry_after)
await asyncio.sleep(retry_after + 1)
continue
if resp.status != 200:
json_resp = await resp.json()
log.debug("Failed to fetch saved tracks: %s", json_resp)
raise SpotifyError(json_resp)
json_data = await resp.json()
data = json_data
tracks_batch = [
matching.TrackInfo.from_spotify(t["track"])
for t in data["items"]
if t["track"] is not None
]
tracks.extend(tracks_batch)
log.debug("%s tracks fetched from saved tracks.", len(tracks))
name = re.sub(r"[\\/]", "_", name)
name = sanitize_path(name)
return name, tracks
Token Generation and Refresh Adjustments:
Class Modified: SpotifyAccessToken
Changes:
Ensured that when generating a new token, the scopes include both playlist-modify-private and user-library-read.
Added error handling for token refresh failures.
Code Snippets:
python
# In generate_auth_url function
"scope": "playlist-modify-private user-library-read",
python
# In gen_new method
"scope": "playlist-modify-private user-library-read",
python
# In refresh method
if resp.status != 200:
log.debug("Failed to refresh token: %s", token_info)
raise SpotifyError(json=token_info)
Error Handling Enhancements:
Improved error logging for failed requests when fetching saved tracks and refreshing tokens.