-
Notifications
You must be signed in to change notification settings - Fork 25
/
Spotipy.py
112 lines (95 loc) · 4.32 KB
/
Spotipy.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import spotipy
import spotipy.oauth2 as oauth2
from spotipy.oauth2 import SpotifyOAuth
from spotipy.oauth2 import SpotifyClientCredentials
import pandas as pd
import time
auth_manager = SpotifyClientCredentials('','')
sp = spotipy.Spotify(auth_manager=auth_manager)
def getTrackIDs(user, playlist_id):
track_ids = []
playlist = sp.user_playlist(user, playlist_id)
for item in playlist['tracks']['items']:
track = item['track']
track_ids.append(track['id'])
return track_ids
def getTrackFeatures(id):
track_info = sp.track(id)
name = track_info['name']
album = track_info['album']['name']
artist = track_info['album']['artists'][0]['name']
# release_date = track_info['album']['release_date']
# length = track_info['duration_ms']
# popularity = track_info['popularity']
track_data = [name, album, artist] #, release_date, length, popularity
return track_data
# Code for creating dataframe of feteched playlist
emotion_dict = {0:"Angry",1:"Disgusted",2:"Fearful",3:"Happy",4:"Neutral",5:"Sad",6:"Surprised"}
music_dist={0:"0l9dAmBrUJLylii66JOsHB?si=e1d97b8404e34343",1:"1n6cpWo9ant4WguEo91KZh?si=617ea1c66ab6446b ",2:"4cllEPvFdoX6NIVWPKai9I?si=dfa422af2e8448ef",3:"0deORnapZgrxFY4nsKr9JA?si=7a5aba992ea14c93",4:"4kvSlabrnfRCQWfN0MgtgA?si=b36add73b4a74b3a",5:"1n6cpWo9ant4WguEo91KZh?si=617ea1c66ab6446b",6:"37i9dQZEVXbMDoHDwVN2tF?si=c09391805b6c4651"}
'''
Code can def be modularised into a function but i tried to write it when i was extremely sleepy so thought screw it and repeated code block
Uncomment for fetching updated playlists
'''
# track_ids = getTrackIDs('spotify',music_dist[0])
# track_list = []
# for i in range(len(track_ids)):
# time.sleep(.3)
# track_data = getTrackFeatures(track_ids[i])
# track_list.append(track_data)
# df = pd.DataFrame(track_list, columns = ['Name','Album','Artist']) # ,'Release_date','Length','Popularity'
# df.to_csv('songs/angry.csv')
# print("CSV Generated")
# track_ids = getTrackIDs('spotify',music_dist[1])
# track_list = []
# for i in range(len(track_ids)):
# time.sleep(.3)
# track_data = getTrackFeatures(track_ids[i])
# track_list.append(track_data)
# df = pd.DataFrame(track_list, columns = ['Name','Album','Artist']) # ,'Release_date','Length','Popularity'
# df.to_csv('songs/disgusted.csv')
# print("CSV Generated")
# track_ids = getTrackIDs('spotify',music_dist[2])
# track_list = []
# for i in range(len(track_ids)):
# time.sleep(.3)
# track_data = getTrackFeatures(track_ids[i])
# track_list.append(track_data)
# df = pd.DataFrame(track_list, columns = ['Name','Album','Artist']) # ,'Release_date','Length','Popularity'
# df.to_csv('songs/fearful.csv')
# print("CSV Generated")
# track_ids = getTrackIDs('spotify',music_dist[3])
# track_list = []
# for i in range(len(track_ids)):
# time.sleep(.3)
# track_data = getTrackFeatures(track_ids[i])
# track_list.append(track_data)
# df = pd.DataFrame(track_list, columns = ['Name','Album','Artist']) # ,'Release_date','Length','Popularity'
# df.to_csv('songs/happy.csv')
# print("CSV Generated")
# track_ids = getTrackIDs('spotify',music_dist[4])
# track_list = []
# for i in range(len(track_ids)):
# time.sleep(.3)
# track_data = getTrackFeatures(track_ids[i])
# track_list.append(track_data)
# df = pd.DataFrame(track_list, columns = ['Name','Album','Artist']) # ,'Release_date','Length','Popularity'
# df.to_csv('songs/neutral.csv')
# print("CSV Generated")
# track_ids = getTrackIDs('spotify',music_dist[5])
# track_list = []
# for i in range(len(track_ids)):
# time.sleep(.3)
# track_data = getTrackFeatures(track_ids[i])
# track_list.append(track_data)
# df = pd.DataFrame(track_list, columns = ['Name','Album','Artist']) # ,'Release_date','Length','Popularity'
# df.to_csv('songs/sad.csv')
# print("CSV Generated")
# track_ids = getTrackIDs('spotify',music_dist[6])
# track_list = []
# for i in range(len(track_ids)):
# time.sleep(.3)
# track_data = getTrackFeatures(track_ids[i])
# track_list.append(track_data)
# df = pd.DataFrame(track_list, columns = ['Name','Album','Artist']) # ,'Release_date','Length','Popularity'
# df.to_csv('songs/surprised.csv')
# print("CSV Generated")