berkaygkv54's picture
latest
a20c02a
import spotipy
from spotipy.oauth2 import SpotifyOAuth
from ..config.configs import Credentials, ProjectPaths
import json
def list_personal_saved_tracks():
scope = "user-library-read"
auth = SpotifyOAuth(client_id=Credentials.SPOTIFY_CLIENT_ID, client_secret=Credentials.SPOTIFY_SECRET_ID, scope=scope, redirect_uri="https://localhost:5000")
sp = spotipy.Spotify(auth_manager=auth)
tracks = []
downloaded_tracks = []
offset_count = 0
for _ in range(50):
results = sp.current_user_saved_tracks(limit=50, offset=offset_count)
for idx, item in enumerate(results['items']):
track = item['track']
data = {"artist": track['artists'][0]['name'], "track": track['name']}
if track not in downloaded_tracks:
tracks.append(data)
downloaded_tracks.append(track)
print(idx, track['artists'][0]['name'], " - ", track['name'])
offset_count += 50
with open(ProjectPaths.DATA_DIR.joinpath("json", "saved_tracks.json"), "w", encoding="UTF-8") as wr:
json.dump(tracks, wr, indent=4)