-
Notifications
You must be signed in to change notification settings - Fork 0
/
save_playlist_features.py
75 lines (66 loc) · 2.22 KB
/
save_playlist_features.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import csv
from dotenv import load_dotenv
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import os
load_dotenv()
scope = "playlist-modify-public playlist-modify-private user-library-read"
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope))
playlist = os.getenv("PLAYLIST")
# get length of playlist
playlist_len = sp.playlist(playlist_id=playlist)["tracks"]["total"]
# read result in chunks of 100
results = {
"items": [],
}
for i in range(0, playlist_len, 100):
new_results = sp.playlist_items(fields=["items"], playlist_id=playlist, limit=100, offset=i)
results["items"].extend(new_results["items"])
for idx, item in enumerate(results["items"], 1):
track = item["track"]
print(idx, track["artists"][0]["name"], " – ", track["name"])
# get all track uri's from results
track_uris = list(map(lambda item: item["track"]["uri"], results["items"]))
features = []
# get features for each track in chuncks of 100
for i in range(0, len(track_uris), 100):
features.extend(sp.audio_features(track_uris[i:i+100]))
# save track features to csv
with open("spotify_features.csv", "w") as csvfile:
fieldnames = [
"idx",
"name",
"uri",
"key",
"mode",
"danceability",
"energy",
"loudness",
"acousticness",
"instrumentalness",
"liveness",
"valence",
"tempo",
]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for idx, item in enumerate(results["items"], 1):
track = item["track"]
feature = features[idx - 1]
writer.writerow(
{
"idx": idx,
"name": track["name"],
"uri": track["uri"],
"key": feature["key"],
"mode": feature["mode"],
"danceability": feature["danceability"],
"energy": feature["energy"],
"loudness": feature["loudness"],
"acousticness": feature["acousticness"],
"instrumentalness": feature["instrumentalness"],
"liveness": feature["liveness"],
"valence": feature["valence"],
"tempo": feature["tempo"],
}
)