-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify.py
82 lines (69 loc) · 2.92 KB
/
spotify.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
76
77
78
79
80
81
82
import requests
import csv
AUTH_TOKEN = "BQBJYh1JuJ5N9uHi-BWhTdiottv_ZD213iC-PLAJzchYA36MY4qBWS0k4dTsR7KBxnXFCwrQ6jrvq_mPMpRWiypcb4lJmM0BtMDUCWPxsZxjteOzs3EoV30oHMNRpxJroQNFaG8FxLPXg7anjr-RN23WmsthjdfHyeMT4yxSLsDN54cSKxez1C3vlwOPMqAhKQ3zP__4RJVf"
def get_tracks_from_playlist(user, playlist, limit=100, offset=0):
if limit <= 100:
endpoint = "https://api.spotify.com/v1/users/%s/playlists/%s/tracks" % (user, playlist)
params = {
"fields": "items(track(id))",
"offset": offset,
"limit": 100
}
headers = {
"Accept": "application/json",
"Authorization": "Bearer " + AUTH_TOKEN
}
r = requests.get(endpoint, params, headers=headers)
data = r.json()
return [t["track"]["id"] for t in data["items"]]
else:
head = get_tracks_from_playlist(user,playlist,offset=offset)
tail = get_tracks_from_playlist(user,playlist,limit=limit-100, offset=offset+100)
return head + tail
def get_attributes_from_tracks(tracks):
if len(tracks) <= 100:
endpoint = "https://api.spotify.com/v1/audio-features/"
params = {
"ids": ",".join(tracks)
}
headers = {
"Accept": "application/json",
"Authorization": "Bearer " + AUTH_TOKEN
}
r = requests.get(endpoint, params, headers=headers)
data = r.json()
return [f for f in data["audio_features"]]
else:
head = get_attributes_from_tracks(tracks[:100])
tail = get_attributes_from_tracks(tracks[100:])
return head + tail
def get_names_from_tracks(tracks):
if len(tracks) <= 50:
endpoint = "https://api.spotify.com/v1/tracks/"
params = {
"ids": ",".join(tracks)
}
headers = {
"Accept": "application/json",
"Authorization": "Bearer " + AUTH_TOKEN
}
r = requests.get(endpoint, params, headers=headers)
data = r.json()
return [t["name"] for t in data["tracks"]]
else:
head = get_names_from_tracks(tracks[:50])
tail = get_names_from_tracks(tracks[50:])
return head + tail
if __name__ == "__main__":
tracks = get_tracks_from_playlist("worldwaffle", "7I0DQkqDHD8bOuUMj6QQej", limit=500)
track_attributes = get_attributes_from_tracks(tracks)
track_names = get_names_from_tracks(tracks)
with open("data/spotify.dat", "wb") as f:
writer = csv.writer(f)
header = ["id","name","energy","liveness","tempo","speechiness","acousticness","instrumentalness","time_signature","danceability","key","loudness","valence"]
writer.writerow(header)
for i,attr in enumerate(track_attributes):
computed = [attr[k] for k in header[2:]]
t_id = unicode(tracks[i]).encode("utf-8")
t_name = unicode(track_names[i]).encode("utf-8")
writer.writerow([t_id] + [t_name] + computed)