48 lines
1.2 KiB
Python
48 lines
1.2 KiB
Python
import spotipy
|
|
from spotipy.oauth2 import SpotifyOAuth
|
|
from dotenv import load_dotenv
|
|
|
|
# Load .env
|
|
load_dotenv()
|
|
|
|
# Connect to spotify
|
|
spotify = spotipy.Spotify(auth_manager=SpotifyOAuth(
|
|
scope="user-library-read",
|
|
open_browser=False))
|
|
|
|
# Fetch the saved tracks of a specified user
|
|
offset = 0
|
|
saved_tracks = []
|
|
limit = 50
|
|
while True:
|
|
try:
|
|
print(f"Récupération {offset}-{offset + limit}")
|
|
results = spotify.current_user_saved_tracks(limit=limit, offset=offset)
|
|
except spotipy.exceptions.SpotifyException as e:
|
|
if e.http_status == 429:
|
|
print("Récolte terminé, l'API a bien mangé.")
|
|
break
|
|
else:
|
|
print(f"Ca a buggé: {e}")
|
|
exit(1)
|
|
else:
|
|
offset += limit
|
|
|
|
saved_tracks.extend(results['items'])
|
|
|
|
|
|
with open("result.csv", "w") as file:
|
|
file.write("titre;artiste;date\n")
|
|
|
|
# Extract data
|
|
buffer = ""
|
|
for item in saved_tracks:
|
|
track = item['track']
|
|
|
|
buffer += f"{track['name'].replace(';', ',')};"
|
|
buffer += f"{track['artists'][0]['name'].replace(';', ',')};"
|
|
buffer += f"{track['album']['release_date']}\n"
|
|
|
|
# Save to a file
|
|
with open("result.csv", "a") as file:
|
|
file.write(buffer)
|