From abf3f053421a02a7c02b3f4b0cc48675c8e5bcd7 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 6 Sep 2023 02:30:28 +0200 Subject: [PATCH] add v1 --- main.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..5aface7 --- /dev/null +++ b/main.py @@ -0,0 +1,48 @@ +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)