-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
41 lines (32 loc) · 1.44 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
def get_spotify_recommendations(input_song_uri):
# Replace 'YOUR_CLIENT_ID' and 'YOUR_CLIENT_SECRET' with your Spotify API credentials
client_id = 'YOUR_CLIENT_ID'
client_secret = 'YOUR_CLIENT_SECRET'
# Initialize Spotipy with client credentials
client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
try:
# Get track information for the input song
input_track_info = sp.track(input_song_uri)
# Get recommendations based on the input song
recommendations = sp.recommendations(seed_tracks=[input_track_info['id']])
# Extract and return recommended tracks
recommended_tracks = [track['name'] for track in recommendations['tracks']]
return recommended_tracks
except Exception as e:
print(f"Error: {e}")
return None
# Example usage
if __name__ == "__main__":
input_song_uri = input("Enter the Spotify URI for a song: ")
# Get similar song recommendations
recommendations = get_spotify_recommendations(input_song_uri)
# Display recommendations
if recommendations:
print(f"Recommended songs similar to the input song:")
for song in recommendations:
print(song)
else:
print("Unable to retrieve recommendations.")