add v1
This commit is contained in:
parent
0581c2a9b3
commit
abf3f05342
1 changed files with 48 additions and 0 deletions
48
main.py
Normal file
48
main.py
Normal file
|
@ -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)
|
Reference in a new issue