-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfinal_script.py
More file actions
150 lines (86 loc) · 3.88 KB
/
final_script.py
File metadata and controls
150 lines (86 loc) · 3.88 KB
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
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import pandas as pd
import numpy as np
<<<<<<< HEAD
client_id = '' #getpass.getpass()
client_secret = "" # getpass.getpass()
=======
from keys import *
>>>>>>> 9744ec4f51e5d6713149b56d4bc00ff3c8ef92e9
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id,
client_secret,
redirect_uri="https://localhost:8089/callback",
scope="user-library-read"))
conf_intervals = pd.read_csv('confidence_intervals.csv', index_col='trait')
## Functions
def frame_maker(link):
df = pd.DataFrame(sp.audio_features(link))
df[['Hit', 'Dance_miss', 'Energy_miss', 'Valence_miss']] = df.apply(hit_detector,axis = 1, result_type='expand')
return df
def hit_detector(row):
hit_score = 0
needs = np.array([0, 0, 0, 0])
traits = ['danceability', 'energy', 'valence']
for trait in traits:
if row[trait] >= conf_intervals.loc[trait, 'bottom']: # (abs(row[trait]) < abs(conf_intervals['top'][traits.index(trait)])):
hit_score += 1
#print(trait, row[trait])
else:
hit_score += 0
if trait == 'danceability':
needs += np.array([0,1,0,0])
elif trait == 'energy':
needs += np.array([0,0,1,0])
elif trait == 'valence':
needs += np.array([0,0,0,1])
#needs.append(trait)
#print(trait,conf_intervals['bottom'][traits.index(trait)],conf_intervals['top'][traits.index(trait)])
#print(trait, row[trait])
if row['mode'] == 1:
hit_score += 0.5
else:
hit_score += 0
if hit_score >= 2.5:
#print(needs)
return (1, 0,0,0)
else:
return needs
def playlist_dataframe(playlist_link):
playlist_URI = playlist_link.split("/")[-1].split("?")[0]
track_uris = [x["track"]["uri"] for x in sp.playlist_tracks(playlist_URI)["items"]]
df = pd.DataFrame(columns=['artist_name','track_name','album_name','release_date','artist_popularity','track_popularity', 'artist_genres','danceability',
'energy','key','loudness','mode','speechiness','acousticness','instrumentalness','liveness','valence','tempo','type','id','uri','track_href','analysis_url',
'duration_ms','time_signature'])
for track in sp.playlist_tracks(playlist_URI)["items"]:
info_vector = []
#URI
track_uri = track["track"]["uri"]
#Track name
track_name = track["track"]["name"]
#Main Artist
artist_uri = track["track"]["artists"][0]["uri"]
artist_info = sp.artist(artist_uri)
#Name, popularity, genre
artist_name = track["track"]["artists"][0]["name"]
artist_pop = artist_info["popularity"]
artist_genres = artist_info["genres"]
#Album
album = track["track"]["album"]["name"]
#Release Date
release_date = track['track']['album']['release_date'] ## keep full date
#Popularity of the track
track_pop = track["track"]["popularity"]
info_vector = [artist_name,track_name,album,release_date,artist_pop,track_pop,artist_genres]
info_vector.extend(list((sp.audio_features(track_uri)[0].values())))
df.loc[len(df)] = info_vector
df[['Hit', 'Dance_miss', 'Energy_miss', 'Valence_miss']] = df.apply(hit_detector,axis = 1, result_type='expand')
return df
track =frame_maker('https://open.spotify.com/track/2MZSXhq4XDJWu6coGoXX1V?si=6b38baf17f9f4964')
#print(track)
playlist = playlist_dataframe('https://open.spotify.com/playlist/08FwuC2mWOk78HgL30lvk8?si=330dafcc50e24282')
<<<<<<< HEAD
print(playlist)
=======
#print(playlist)
>>>>>>> 9744ec4f51e5d6713149b56d4bc00ff3c8ef92e9