-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
194 lines (152 loc) · 5.32 KB
/
app.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
from spotipy.oauth2 import SpotifyOAuth
from spotipy import oauth2
import spotipy
import sys
import os
from analysis import *
def song_information(spotify, playlistname, playlistsongids, playlistuser): #evaluate songs from a list of songs
#complete for now
username = playlistuser['display_name'].replace(' ', '')
headers = []
songdicts = []
for x,y in enumerate(playlistsongids):
#song id
songid = y
songdata = get_song_data(spotify, songid)
songfeatures = get_song_features(spotify, songid)
songdict = {**songdata, **songfeatures}
songdicts.append(songdict)
print(f'Finished {songdata["song_name"]}')
for key in songdicts[0]:
headers.append(key)
print(f'Got all info for all songs')
return songdicts, headers, [playlistname,username]
def get_song_genre(spotify, artist_id): #getting the genre of a song based on artist id
#complete for right now
try:
artist = spotify.artist(artist_id)#search for artist based on id to get exact result
genres = artist['genres']
genrestr = '('
for a in genres:
genrestr += (a + ';')
genrestr = genrestr[:-1]
genrestr += ')'
return genrestr
except(AttributeError):
pass
def get_song_features(spotify, songid): #getting the data of a song (dancability, loudness, etc)
features = spotify.audio_features(songid)
features = features[0]
song_features = {
'danceability' : features['danceability'],
'energy' : features['energy'],
'key' : features['key'],
'loudness' : features['loudness'],
'mode' : features['mode'],
'speechiness' : features['speechiness'],
'acousticness' : features['acousticness'],
'instrumentalness' : features['instrumentalness'],
'liveness' : features['liveness'],
'valence' : features['valence'],
'tempo' : features['tempo'],
'time_signature' : features['time_signature']
}
return song_features
def get_song_data(spotify, songid):
data = spotify.track(songid)
song_data = {
'song_name' : data['name'],
'album_name': data['album']['name'],
'artist_name' : data['artists'][0]['name'],
'artist_id' : data['artists'][0]['id'],
'song_release_date' : data['album']['release_date'],
'song_length' : data['duration_ms'],
'song_popularity' : data['popularity'],
'song_id' : songid,
}
print('Recieved Song Data for {}'.format(songid))
return song_data
def get_playlist(spotify, id): #extract and distribute info from a playlist to helper functions
playlist = spotify.playlist(id) #returns a dict
playlist_name = playlist['name']#playlist name
user = playlist['owner']#user who owns the playlist
tracks = playlist['tracks'] #only the first 100 tracks -- is a dict
all_tracks = tracks['items']
while tracks['next']: # getting the rest of the tracks from the playlist
tracks = spotify.next(tracks)
all_tracks.extend(tracks['items'])
#creating a list of all the songs in the playlist
local_songs = []
songs = [x['track'] for x in all_tracks]
songids = []
for x,y in enumerate(songs):
if not y['is_local']:
songids.append(y['id'])
else:
local_songs.append((y['name'],y['id']))
print(f'Revieved Playlist Info for playlist {id}')
return playlist_name, songids, user , local_songs
def to_csv(dictionaries, headers,names):
#∆ - option+J - special delimiter
playlist_name = names[0]
username = names[1]
try:
os.mkdir(username)
except(FileExistsError):
pass
filename = username+'/'+playlist_name+'.csv'
print(f'Writing file {filename}')
with open(filename, 'w') as data:
for x in headers:
data.write(x+'∆')
data.write('\n')
for dict in dictionaries:
for key in dict:
data.write(f'{dict[key]} ∆')
data.write('\n')
print(f'{filename} has been written')
def make_playlist(spotify, username, df, playlist_name):
hi_name = 'High Energy: ' + playlist_name
lo_name = 'Low Energy: ' + playlist_name
username = username.replace('spotify:user:','')
high_energy = spotify.user_playlist_create(user=username,
name=hi_name,
description= 'https://github.com/sidharthsrinath/spotipy-working')
print('Created High Energy Playlist')
low_energy = spotify.user_playlist_create(user=username,
name=lo_name,
description = 'https://github.com/sidharthsrinath/spotipy-working')
print('Created Low Energy Playlist')
playlist_1 = df[df['KMeans']==1] #low energy
playlist_2 = df[df['KMeans']==0] #high energy
id0 = list(playlist_1['song_id']) #low energy
id1 = list(playlist_2['song_id']) #high energy
for x in id0:
thisid = x
thisidN = thisid.strip()
spotify.user_playlist_add_tracks(user = username,playlist_id= low_energy['id'],tracks = [thisidN])
print('Added Songs to Low Energy Playlist')
for x in id1:
thisid = x
thisidN = thisid.strip()
spotify.user_playlist_add_tracks(user = username,playlist_id= high_energy['id'],tracks = [thisidN])
print('Added Songs to High Energy Playlist')
def change_scope(filename):
str = ''
with open(filename,'r') as file:
str = file.readlines()
print(str)
def authorize():
SCOPE = 'user-library-read playlist-modify-public'
CACHE_PATH=".cache"
sp = SpotifyOAuth(scope=SCOPE, cache_path=None)
spotify = spotipy.Spotify(auth_manager=sp)
return spotify
def delete_cache():
if os.path.isfile(".cache"):
os.remove(".cache")
def get_songs(spotify):
results = spotify.current_user_saved_tracks()
for idx, item in enumerate(results['items']):
track = item['track']
print(idx, track['artists'][0]['name'], " – ", track['name'])