-
Notifications
You must be signed in to change notification settings - Fork 0
/
playlist.py
54 lines (46 loc) · 1.46 KB
/
playlist.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
42
43
44
45
46
47
48
49
50
51
52
53
54
import spotipy
import sys
import pprint
import numpy as np
import matplotlib.pyplot as plt
from spotipy.oauth2 import SpotifyClientCredentials
# Log in
client_credentials_manager = SpotifyClientCredentials(client_id='43a1cf4b58e24cc9b5fb5a90ad8c1bb7',
client_secret='4436a0ee76a3474092a0dfe60703c2dc')
spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
# Get playlist
user = "dadsh"
playlist = "spotify:user:dadsh:playlist:1zeoeTuPGQsAao5uBDhaa1"
results = spotify.user_playlist_tracks(user, playlist, fields="items(track(artists(name),name,popularity,uri))")
# pprint.pprint(results)
# Get URIs of tracks
uris = []
for i in range(len(results['items'])):
uri = results['items'][i]['track']['uri']
uris.append(uri)
# Get features of tracks
features = spotify.audio_features(uris)
# pprint.pprint(features)
# Rebuild playlist object
pl = results['items']
for i in range(len(pl)):
pl[i]['track']['features'] = features[i]
# Create x and y coordinates
tempo = []
energy = []
name = []
for i in range(len(pl)):
tempo.append(pl[i]['track']['features']['tempo'])
energy.append(pl[i]['track']['features']['energy'])
name.append(pl[i]['track']['name'])
print(tempo)
print(energy)
print(name)
plt.scatter(tempo, energy)
for i, txt in enumerate(name):
plt.annotate(txt, (tempo[i], energy[i]))
plt.xlabel("Tempo")
plt.ylabel("Energy")
plt.show()
# print(type(pl))
# pprint.pprint(pl)