First functions
This commit is contained in:
85
spotify.py
Normal file
85
spotify.py
Normal file
@@ -0,0 +1,85 @@
|
||||
import spotipy
|
||||
from spotipy.oauth2 import SpotifyOAuth
|
||||
import json
|
||||
import requests
|
||||
|
||||
"""
|
||||
User functions
|
||||
"""
|
||||
def authentify(client_id,client_secret,scope):
|
||||
api = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id="1ad3f973e5924940bec0bcbf1adf7475",client_secret="e0601efe11ee4e31b27b255c06d70a38",redirect_uri="http://127.0.0.1:8888",scope=scope))
|
||||
return api
|
||||
|
||||
def get_user_id(api):
|
||||
return api.current_user()['id']
|
||||
|
||||
"""
|
||||
Tracks functions
|
||||
"""
|
||||
def get_original_release_year(api, track_id):
|
||||
response = api.track(track_id)
|
||||
title = response['artists'][0]['name']
|
||||
artist = ['name']
|
||||
url = "https://musicbrainz.org/ws/2/recording/"
|
||||
params = {
|
||||
"query": f"{title} AND artist:{artist}",
|
||||
"fmt": "json"
|
||||
}
|
||||
response = requests.get(url, params=params)
|
||||
data = response.json()
|
||||
|
||||
# Traitement de la première date de sortie si disponible
|
||||
if data['recordings']:
|
||||
release_date = data['recordings'][0]['first-release-date']
|
||||
return release_date[:4] # Renvoie uniquement l'année
|
||||
return None
|
||||
|
||||
# Max 100 tracks
|
||||
def get_tracks_bpm(api,tracks):
|
||||
response = api.audio_features(tracks)
|
||||
tempos = ['{:.1f}'.format(track["tempo"]) for track in response]
|
||||
return tempos
|
||||
|
||||
"""
|
||||
Playlists functions
|
||||
"""
|
||||
def get_playlist_data(api, playlist_id):
|
||||
response = api.playlist_tracks(playlist_id)
|
||||
|
||||
def get_playlist_tracks_with_metadata(api, playlist_id):
|
||||
results = api.playlist_tracks(playlist_id)
|
||||
tracks = results['items']
|
||||
|
||||
for idx, item in enumerate(tracks):
|
||||
track = item['track']
|
||||
track_name = track['name']
|
||||
artist_name = track['artists'][0]['name']
|
||||
album_id = track['album']['id']
|
||||
|
||||
# Récupération de l'année de sortie via l'album
|
||||
album_info = api.album(album_id)
|
||||
release_year = album_info['release_date'][:4]
|
||||
|
||||
# Récupération du BPM via les caractéristiques audio
|
||||
track_features = api.audio_analysis(track['id'])[0]
|
||||
bpm = track_features['tempo']
|
||||
|
||||
# Affichage des informations
|
||||
print(f"{idx + 1}: {track_name} by {artist_name} | Year: {release_year} | BPM: {bpm}")
|
||||
|
||||
def get_playlist_id(api, nom):
|
||||
playlists = []
|
||||
results = api.current_user_playlists()
|
||||
playlists.extend(results['items'])
|
||||
|
||||
# Pagination pour récupérer toutes les playlists si l'utilisateur en a plus de 50
|
||||
while results['next']:
|
||||
results = api.next(results)
|
||||
playlists.extend(results['items'])
|
||||
|
||||
# Recherche de la playlist par nom
|
||||
for playlist in playlists:
|
||||
if playlist['name'] == nom:
|
||||
return playlist['id']
|
||||
print(f"Playlist '{nom}' non trouvée.")
|
||||
return None
|
||||
Reference in New Issue
Block a user