-
Notifications
You must be signed in to change notification settings - Fork 0
/
savePlaylist.py
65 lines (52 loc) · 2.04 KB
/
savePlaylist.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
import sys
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import math
import csv
import setVars
setVars.setVars()
playlist_id = "5S8SJdl1BDc0ugpkEvFsIL"
playlist_name = "longplaylist"
scope = "user-library-read"
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope))
length = 10000
data = [["Artist", "Name", "Date","ID", "danceability", "energy", "key", "loudness", "speechiness", "acousticness", "instrumentalness", "liveness", "valence", "tempo", "duration"]]
for i in range(math.ceil(length/100)):
results = sp.playlist_items(playlist_id, offset=i*100)
for idx, item in enumerate(results['items']):
try:
idx = i*100 + idx
track = item['track']
trackID = track['id']
artist = track['artists'][0]['name']
try:
features = sp.audio_features(trackID)[0]
except:
print(sys.exc_info()[0])
continue
#Janky add-in to trim disliked songs data
if artist in ["Taylor Swift", "Bruno Mars"]:
continue
data.append([artist,
track['name'],
item['added_at'].split("T")[0],
trackID,
features['danceability'],
features['energy'],
features['key'],
features['loudness'],
features['speechiness'],
features['acousticness'],
features['instrumentalness'],
features['liveness'],
features['valence'],
features['tempo'],
features['duration_ms']])
except Exception as e:
print(e)
print("Pass " + str(i))
print("Writing to CSV")
print(len(data))
with open("localdata/" + playlist_name + ".csv", "w",encoding="utf-8") as csvfile:
csvwriter = csv.writer(csvfile, delimiter=",")
csvwriter.writerows(data)