-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspotify.py
More file actions
54 lines (45 loc) · 1.57 KB
/
spotify.py
File metadata and controls
54 lines (45 loc) · 1.57 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy
import pandas as pd
import matplotlib.pyplot as plt
import re
# Set up Client Credentials
sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(
client_id='e6d5b2932c6246ecaef8e8cf6f207a66',
client_secret='78de5eebf1024a83bc7bc6524bd814cc'
))
# Full track URL
track_url = "https://open.spotify.com/track/3n3Ppam7vgaVa1iaRUc9Lp"
# Extract track ID directly from URL using regex
track_id = re.search(r'track/([a-zA-Z0-9]+)', track_url).group(1)
# Fetch track details
track = sp.track(track_id)
print(track)
# Extract metadata
track_data = {
'Track Name': track['name'],
'Artist': track['artists'][0]['name'],
'Album': track['album']['name'],
'Popularity': track['popularity'],
'Duration (minutes)': track['duration_ms'] / 60000
}
# Display metadata
print(f"\nTrack Name: {track_data['Track Name']}")
print(f"Artist: {track_data['Artist']}")
print(f"Album: {track_data['Album']}")
print(f"Popularity: {track_data['Popularity']}")
print(f"Duration: {track_data['Duration (minutes)']:.2f} minutes")
# Convert metadata to DataFrame
df = pd.DataFrame([track_data])
print("\nTrack Data as DataFrame:")
print(df)
# Save metadata to CSV
df.to_csv('spotify_track_data.csv', index=False)
# Visualize track data
features = ['Popularity', 'Duration (minutes)']
values = [track_data['Popularity'], track_data['Duration (minutes)']]
plt.figure(figsize=(8, 5))
plt.bar(features, values, color='skyblue', edgecolor='black')
plt.title(f"Track Metadata for '{track_data['Track Name']}'")
plt.ylabel('Value')
plt.show()