-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathspotify.py
126 lines (107 loc) · 3.81 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
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
import joblib
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
from dotenv import load_dotenv
import pandas as pd
import os
def hit_flop(track, artist):
# Load keys
load_dotenv()
client_id = os.getenv("SPOTIPY_CLIENT_ID")
client_secret = os.getenv("SPOTIPY_CLIENT_SECRET")
# Transform strings to lower case
track = track.lower()
artist = artist.lower()
# Call song
spotify = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials(client_id=client_id, client_secret=client_secret))
try:
results = spotify.search(q='track:' + track, type='track')
# Find song uri by matching artist
items = results['tracks']['items']
for i in items:
item_artist = i['artists'][0]['name']
if item_artist.lower() == artist:
uri = i['uri']
# Setting information from audio analysis
audio_analysis = spotify.audio_analysis(track_id=uri)
count_sections = len(audio_analysis['sections'])
chorus_hit = audio_analysis['sections'][2]['start']
# Setting information from audio features
audio_features = spotify.audio_features(tracks=[uri])
f = audio_features[0]
# Arrange data to load to model
x = [[f['danceability'],
f['energy'],
f['key'],
f['loudness'],
f['mode'],
f['speechiness'],
f['acousticness'],
f['instrumentalness'],
f['liveness'],
f['valence'],
f['tempo'],
f['duration_ms'],
f['time_signature'],
chorus_hit,
count_sections]]
# Arrange data to fill dictionary for DataFrame
key = ''
if f['key'] == 1:
key = 'Major'
else:
key = 'Minor'
mode = ''
pitch_class = {-1:'No key was detected',
0:'C',
1:'C♯ or D♭',
2:'D',
3:'D♯ or E♭',
4:'E',
5:'F',
6:'F♯ or G♭',
7:'G',
8:'G♯ or A♭',
9:'A',
10:'A♯ or B♭',
11:'B'}
if f['mode'] in pitch_class:
mode = pitch_class[f['mode']]
# Create DataFrame of data
table = pd.DataFrame({
'Danceability': [f['danceability']],
'Energy': [f['energy']],
'Key': [key],
'Loudness (db)': [f['loudness']],
'Mode': [mode],
'Speechiness': [f['speechiness']],
'Acousticness': [f['acousticness']],
'Instrumentalness': [f['instrumentalness']],
'Liveness': [f['liveness']],
'Valence': [f['valence']],
'Tempo (beats per minute (BPM))': [f['tempo']],
'Duration (seconds)': [f['duration_ms']/1000],
'Time Signature': [f['time_signature']],
'Aprox timestamp the chorus "hit" (seconds)': [chorus_hit],
'Sections count': [count_sections]
})
ttable = table.T
feature_table = ttable.to_html(classes="table table-hover table-success table-striped",
justify='center',
header=False)
except:
error = "We can't find your entry in the spotify database, please check your spelling and/or try again"
return [error]
try:
# Load model
model = joblib.load('model.sav')
scaler = joblib.load('scaler.sav')
# Scaling data
x_scaled = scaler.transform(x)
# Running model
hit_predict = model.predict(x_scaled)
hit_score = model.predict_proba(x_scaled)
return [hit_predict, hit_score, feature_table]
except:
error_model = "Model error"
return [error_model]