In [ ]:
from lib2to3.pgen2.pgen import DFAState
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import cred
import pandas as pd 
import time
import numpy as np
import matplotlib.pyplot as plt
import math
import mpld3
In [ ]:
scope = "user-read-recently-played"
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=cred.client_ID, client_secret= cred.client_SECRET, redirect_uri=cred.redirect_url, scope=scope))

ids = []
results = sp.current_user_recently_played()
for idx, item in enumerate(results['items']):
    track = item['track']
    print(idx, track['artists'][0]['name'], " – ", track['name'])
    ids.append(track['id'])
0 Aaron May  –  HIDDEN VOICES
1 Aaron May  –  HIDDEN VOICES
2 Sheff G  –  Moody
3 Sheff G  –  On Go (feat. Polo G)
4 Denzel Curry  –  Walkin - Key Glock remix
5 Denzel Curry  –  Walkin - Key Glock remix
6 Goldensuns  –  Easy Love
7 Teenage Priest  –  Anymore
8 Wallice  –  Dramamine
9 spill tab  –  Splinter
10 Bennett Coast  –  Seeds
11 Ritt Momney  –  Show Runner 99
12 Harry Styles  –  Little Freak
13 Tom The Mail Man  –  Bad Hoe
14 Future  –  WAIT FOR U (feat. Drake & Tems)
15 Vince Staples  –  AYE! (FREE THE HOMIES)
16 Steve Lacy  –  Static
17 Steve Lacy  –  Static
18 Public Library Commute  –  Across the Room
19 Public Library Commute  –  kauai
20 Omar Apollo  –  Want U Around (feat. Ruel)
21 Dreamer Boy  –  Orange Girl
22 Daniel Caesar  –  Please Do Not Lean (feat. BADBADNOTGOOD)
23 Del Water Gap  –  Perfume
24 Vansire  –  Vivienne
25 Skip Marley  –  Slow Down
26 Free Nationals  –  Shibuya (feat. Syd)
27 Free Nationals  –  Time
28 Rex Orange County  –  7AM
29 Public Library Commute  –  You Been on My Mind
30 Kid Bloom  –  I Fell in Love Again
31 Deb Never  –  Funky
32 Public Library Commute  –  Moonlight
33 Steve Lacy  –  Helmet
34 Vansire  –  Evening Light
35 The 1975  –  TOOTIMETOOTIMETOOTIME
36 Yellow House  –  Better Views
37 Far Caspian  –  Brother
38 Public Library Commute  –  Brand New
39 Vansire  –  Set Piece
40 Vampire Weekend  –  Sunflower (feat. Steve Lacy)
41 Steve Lacy  –  N Side
42 Deb Never  –  Funky
43 Public Library Commute  –  Moonlight
44 Rex Orange County  –  7AM
45 Pale Jay  –  Donny In Valdez - PJ's Flip
46 Isaiah Rashad  –  Headshots (4r Da Locals)
47 Baby Keem  –  family ties (with Kendrick Lamar)
48 Saba  –  Still (feat. 6LACK and Smino)
49 Megan Thee Stallion  –  Budget (feat. Latto)
In [ ]:
print(ids)
['1oMKYS43XRqNsY1SVuiCoJ', '4BwGU8l91SQWeVqnWqWClh', '1Vrw7LZl56M8akId20D455', '3i058E8uxTsYqJ5NWZzqSj', '6OGvHDjNLuEJoBc8BEQWBP', '59nOXPmaKlBfGMDeOVGrIK', '7CvtBcThQ4piVKkfUXieig', '4OmfWzukSVD140NiAIEjem', '4OmfWzukSVD140NiAIEjem', '7dCSqTnarqs3EF7iDRVRW9', '1yWbRXtwrkVO4yrjp7ohgG', '1Qu24vc8msUVLcMZWa4k1x', '6iha68MS4jddL4TY5pZGRi', '5uXO1sjZ7VbNKexKaFRna3', '3wJAk2rKEEoYlXSLpLVrkU', '7sPJFcAr6I8VPjXgNrXgh1', '0jPEWByX9DZAQb5zyQ6eng', '6WVSnyKQGzs1fosa2I3FMQ', '7klPHv3HnXdUY3dSfTccNc', '32B72vNdAd1987euX9Lkgs', '53Kum5jxy3QWism3RUUQzU', '2vAFFYY2Iwhry8o8ZQgW5D', '2KZVQQfBRgAYkFqlr3GRqv', '66EeuV09dYF6KxfqzZ6tWt', '0DmkBOGgEWMbUJlL6VwwF2', '4qde41KNfJv2TyrKC1kug7', '33DzKnwuBE6lfOiADwzd5E', '5PE2TkQKrgcLZupVGe5Pxu', '54tuwZt2mi4kn46IeAz1FJ', '5FcK65jGaIPzdIUB9asktp', '2z2w98hk0TWjLTCLtACnwJ', '2zqCw4g4w9ZLzwHtw1I7e9', '24G1PXBWoRgV0wDXZKwxzz', '2KZVQQfBRgAYkFqlr3GRqv', '66EeuV09dYF6KxfqzZ6tWt', '32B72vNdAd1987euX9Lkgs', '2GDzymJClRxEhhDYGwQZz6', '0xta6Gah3inmeHbj0e3F9a', '3QFInJAm9eyaho5vBzxInN', '0L2G8Khc96e6yLcLyLklzS', '3BFxkzhkESwALQxjxOVFgJ', '1DIFFX5zpjyxaJvA8YFEji', '28qA8y1sz0FTuSapsCxNOG', '5GJS3CY3aqbT9j2lv434aQ', '6BeEg9qR8jDuEpH3YtMJHd', '6vA7RdHmU23h3ygUPPFXz5', '68kc71kxyfuWFLTheYyZlO', '5XFoaqV1Z8XaLJidchVWhh', '3H5pElWVrTWGqLhqo7LxQM', '1XcQwxhNmgzGVW76JfBDPX']
In [ ]:
def getTrackFeatures(id):
  meta = sp.track(id)
  features = sp.audio_features(id)

  # meta
  name = meta['name']
  album = meta['album']['name']
  artist = meta['album']['artists'][0]['name']
  release_date = meta['album']['release_date']
  length = meta['duration_ms']
  popularity = meta['popularity']

  # features
  acousticness = features[0]['acousticness']
  danceability = features[0]['danceability']
  energy = features[0]['energy']
  instrumentalness = features[0]['instrumentalness']
  liveness = features[0]['liveness']
  loudness = features[0]['loudness']
  speechiness = features[0]['speechiness']
  tempo = features[0]['tempo']
  time_signature = features[0]['time_signature']

  track = [name, album, artist, release_date, length, popularity, danceability, acousticness, danceability, energy, instrumentalness, liveness, loudness, speechiness, tempo, time_signature]
  return track
In [ ]:
# loop over track ids 
tracks = []
for i in range(len(ids)):
  time.sleep(.5)
  track = getTrackFeatures(ids[i])
  tracks.append(track)

# create dataset
df = pd.DataFrame(tracks, columns = ['name', 'album', 'artist', 'release_date', 'length', 'popularity', 'danceability', 'acousticness', 'danceability', 'energy', 'instrumentalness', 'liveness', 'loudness', 'speechiness', 'tempo', 'time_signature'])
In [ ]:
df
Out[ ]:
name album artist release_date length popularity danceability acousticness danceability energy instrumentalness liveness loudness speechiness tempo time_signature
0 HIDDEN VOICES WOKEUPTHINKINBOUT / HIDDEN VOICES Aaron May 2022-01-07 150909 56 0.754 0.77400 0.754 0.530 0.017600 0.1160 -10.846 0.0742 140.014 4
1 HIDDEN VOICES WOKEUPTHINKINBOUT / HIDDEN VOICES Aaron May 2022-01-07 150909 56 0.754 0.77400 0.754 0.530 0.017600 0.1160 -10.846 0.0742 140.014 4
2 Moody One and Only Sheff G 2020-05-15 102800 53 0.820 0.82100 0.820 0.446 0.000000 0.1040 -7.797 0.1470 131.070 4
3 On Go (feat. Polo G) On Go (feat. Polo G) Sheff G 2021-07-23 180805 65 0.880 0.45900 0.880 0.427 0.000001 0.1190 -6.004 0.3230 103.542 3
4 Walkin - Key Glock remix Walkin (Key Glock remix) Denzel Curry 2022-06-01 226115 63 0.777 0.14000 0.777 0.703 0.000013 0.2350 -6.863 0.0485 135.985 4
5 Walkin - Key Glock remix Walkin (Key Glock remix) Denzel Curry 2022-06-01 226115 63 0.777 0.14000 0.777 0.703 0.000013 0.2350 -6.863 0.0485 135.985 4
6 Easy Love Easy Love Goldensuns 2021-02-24 243126 44 0.584 0.04240 0.584 0.481 0.002320 0.0794 -6.707 0.0257 139.969 4
7 Anymore Rhymes & Rhythms Teenage Priest 2021-08-27 247957 37 0.665 0.44900 0.665 0.578 0.287000 0.0978 -7.548 0.0279 75.003 4
8 Dramamine Off the Rails Wallice 2021-06-04 175123 35 0.607 0.01560 0.607 0.751 0.002040 0.2330 -5.577 0.0338 124.169 4
9 Splinter Splinter spill tab 2022-05-20 148173 52 0.593 0.02200 0.593 0.543 0.000000 0.4790 -7.330 0.0336 97.987 4
10 Seeds Seeds Bennett Coast 2022-02-11 219286 48 0.475 0.78600 0.475 0.436 0.000004 0.5890 -9.872 0.0490 113.273 5
11 Show Runner 99 Sunny Boy Ritt Momney 2021-10-22 206316 37 0.260 0.91600 0.260 0.465 0.003450 0.3300 -10.143 0.0332 127.465 5
12 Little Freak Harry's House Harry Styles 2022-05-20 202183 85 0.559 0.71900 0.559 0.535 0.000143 0.0954 -7.723 0.0390 141.960 4
13 Bad Hoe Sometimes Sorry Isn't Enough Tom The Mail Man 2021-04-16 207250 54 0.806 0.08700 0.806 0.735 0.000000 0.0885 -4.390 0.0495 128.019 4
14 WAIT FOR U (feat. Drake & Tems) I NEVER LIKED YOU Future 2022-04-29 189893 89 0.463 0.31400 0.463 0.642 0.000000 0.0686 -4.474 0.3400 83.389 4
15 AYE! (FREE THE HOMIES) RAMONA PARK BROKE MY HEART Vince Staples 2022-04-08 185879 58 0.745 0.21500 0.745 0.690 0.000000 0.0779 -7.063 0.0628 95.022 4
16 Static Gemini Rights Steve Lacy 2022-07-15 156506 77 0.335 0.43400 0.335 0.305 0.631000 0.1010 -12.661 0.0741 79.001 5
17 Static Gemini Rights Steve Lacy 2022-07-15 156506 77 0.335 0.43400 0.335 0.305 0.631000 0.1010 -12.661 0.0741 79.001 5
18 Across the Room Across the Room Public Library Commute 2021-11-05 180731 53 0.826 0.09620 0.826 0.709 0.000646 0.3970 -6.597 0.0369 117.028 4
19 kauai Over Grey Skies Public Library Commute 2019-06-17 152500 25 0.723 0.72800 0.723 0.313 0.371000 0.1050 -13.244 0.0620 144.031 4
20 Want U Around (feat. Ruel) Apolonio Omar Apollo 2020-10-16 247720 61 0.571 0.30000 0.571 0.443 0.000007 0.1010 -6.760 0.0451 143.923 3
21 Orange Girl Love, Nostalgia Dreamer Boy 2018-11-13 258939 41 0.231 0.57800 0.231 0.450 0.475000 0.1310 -10.162 0.0350 82.507 4
22 Please Do Not Lean (feat. BADBADNOTGOOD) Please Do Not Lean Daniel Caesar 2022-04-22 240937 71 0.670 0.24100 0.670 0.410 0.000009 0.1670 -13.444 0.0381 127.972 4
23 Perfume Perfume Del Water Gap 2021-08-18 232076 44 0.490 0.34800 0.490 0.821 0.000841 0.1980 -7.711 0.0848 195.128 3
24 Vivienne The Modern Western World Vansire 2022-05-12 177653 42 0.718 0.83800 0.718 0.334 0.002280 0.1030 -12.226 0.0328 112.899 4
25 Slow Down Higher Place Skip Marley 2020-08-28 194406 60 0.662 0.04600 0.662 0.764 0.000000 0.0947 -5.224 0.1110 99.971 4
26 Shibuya (feat. Syd) Free Nationals Free Nationals 2019-12-13 136831 61 0.760 0.10500 0.760 0.665 0.399000 0.0945 -7.021 0.0612 92.992 4
27 Time Free Nationals Free Nationals 2019-12-13 210385 65 0.469 0.51800 0.469 0.771 0.000197 0.0789 -7.071 0.3380 88.968 4
28 7AM WHO CARES? Rex Orange County 2022-03-11 199200 63 0.640 0.50000 0.640 0.393 0.000017 0.0978 -7.584 0.0411 148.166 4
29 You Been on My Mind 1000 Summers Public Library Commute 2021-03-25 160000 22 0.816 0.55300 0.816 0.540 0.000262 0.1210 -7.221 0.0390 105.033 4
30 I Fell in Love Again I Fell in Love Again Kid Bloom 2022-01-21 218013 39 0.587 0.01310 0.587 0.737 0.000672 0.3610 -5.071 0.0353 179.971 4
31 Funky Where Have All the Flowers Gone? Deb Never 2021-09-10 214760 44 0.557 0.07910 0.557 0.703 0.000000 0.2700 -7.024 0.0584 166.964 4
32 Moonlight 1000 Summers Public Library Commute 2021-03-25 191249 25 0.721 0.84800 0.721 0.698 0.001800 0.0969 -4.160 0.0302 96.011 3
33 Helmet Gemini Rights Steve Lacy 2022-07-15 201546 75 0.612 0.09940 0.612 0.675 0.000000 0.2170 -6.518 0.0231 97.040 4
34 Evening Light The Modern Western World Vansire 2022-05-12 130813 56 0.552 0.88800 0.552 0.416 0.000522 0.1090 -11.072 0.0317 86.089 4
35 TOOTIMETOOTIMETOOTIME A Brief Inquiry Into Online Relationships The 1975 2018-11-30 207851 61 0.667 0.17700 0.667 0.824 0.015200 0.0933 -5.867 0.0665 114.998 4
36 Better Views Better Views Yellow House 2016-10-04 239640 48 0.456 0.66100 0.456 0.611 0.000822 0.0996 -8.952 0.0382 88.881 4
37 Brother Ways To Get Out (Deluxe) Far Caspian 2022-02-25 173066 25 0.608 0.19900 0.608 0.662 0.898000 0.0916 -8.234 0.0257 134.408 4
38 Brand New Brand New Public Library Commute 2021-09-08 156381 47 0.814 0.38300 0.814 0.681 0.025600 0.2710 -8.002 0.0400 132.023 4
39 Set Piece Angel Youth Vansire 2018-04-26 199490 46 0.569 0.35100 0.569 0.483 0.418000 0.1110 -10.675 0.0519 80.736 4
40 Sunflower (feat. Steve Lacy) Father of the Bride Vampire Weekend 2019-05-03 137600 43 0.863 0.40600 0.863 0.576 0.008240 0.1390 -5.756 0.0565 100.034 4
41 N Side Apollo XXI Steve Lacy 2019-05-24 224444 78 0.838 0.07200 0.838 0.536 0.000259 0.2080 -6.288 0.0435 119.956 4
42 Funky Where Have All the Flowers Gone? Deb Never 2021-09-10 214760 44 0.557 0.07910 0.557 0.703 0.000000 0.2700 -7.024 0.0584 166.964 4
43 Moonlight 1000 Summers Public Library Commute 2021-03-25 191249 25 0.721 0.84800 0.721 0.698 0.001800 0.0969 -4.160 0.0302 96.011 3
44 7AM WHO CARES? Rex Orange County 2022-03-11 199200 63 0.640 0.50000 0.640 0.393 0.000017 0.0978 -7.584 0.0411 148.166 4
45 Donny In Valdez - PJ's Flip Donny In Valdez (PJ's Flip) Pale Jay 2022-05-20 113747 54 0.445 0.46300 0.445 0.637 0.000002 0.1850 -9.011 0.1240 81.566 4
46 Headshots (4r Da Locals) The House Is Burning Isaiah Rashad 2021-07-30 193208 66 0.894 0.03080 0.894 0.615 0.009210 0.3090 -6.269 0.0826 120.000 4
47 family ties (with Kendrick Lamar) The Melodic Blue Baby Keem 2021-09-10 252262 77 0.711 0.00588 0.711 0.611 0.000000 0.2300 -5.453 0.3300 134.093 4
48 Still (feat. 6LACK and Smino) Few Good Things Saba 2022-02-04 225546 55 0.487 0.38600 0.487 0.630 0.000133 0.4640 -9.942 0.5550 76.349 4
49 Budget (feat. Latto) Traumazine Megan Thee Stallion 2022-08-12 201979 67 0.922 0.00451 0.922 0.858 0.000000 0.1240 -4.185 0.2510 145.033 4
In [ ]:
df['popularity']= df['popularity'].astype('float64')
df['danceability']= df['danceability'].astype('float64')
df['acousticness']= df['acousticness'].astype('float64')
df['energy']= df['energy'].astype('float64')
df['instrumentalness']= df['instrumentalness'].astype('float64')
df['liveness']= df['liveness'].astype('float64')
df['time_signature'] = df['time_signature'].astype('float64')
df['length'] = df['length'].astype('float64')
In [ ]:
df['popularity']= df['popularity'].mean(axis=0)
df['danceability']= df['danceability'].mean(axis=0)
df['acousticness']= df['acousticness'].mean(axis=0)
df['energy']= df['energy'].mean(axis=0)
df['instrumentalness']= df['instrumentalness'].mean(axis=0)
df['liveness']= df['liveness'].mean(axis=0)
df['time_signature']= df['time_signature'].mean(axis=0)
df['length']= df['length'].mean(axis=0)
df['loudness']= df['loudness'].mean(axis=0)
df['speechiness']= df['speechiness'].mean(axis=0)
df['tempo']= df['tempo'].mean(axis=0)
df['time_signature']= df['time_signature'].mean(axis=0)
In [ ]:
df2= df.drop(['name','album','artist','release_date','length'],axis=1)
In [ ]:
df2.drop_duplicates(inplace=True)
In [ ]:
df2
Out[ ]:
popularity danceability acousticness danceability energy instrumentalness liveness loudness speechiness tempo time_signature
0 53.9 0.63972 0.377742 0.63972 0.5833 0.084434 0.175972 -7.8176 0.089128 117.89558 3.98
In [ ]:
pop= df2['popularity']
acoutstic= df2['acousticness']
energy= df2['energy']
instru= df2['instrumentalness']
live= df2['liveness']
loud= df2['loudness']
speech= df2['speechiness']
tempo= df2['tempo']
timSig= df2['time_signature']
In [ ]:
X= (pop/10)-(acoutstic)-(instru+energy)-(live)
X
Out[ ]:
0    4.168552
dtype: float64
In [ ]:
Y= (tempo/-20)-(loud)+(speech)-(timSig)
Y
Out[ ]:
0   -1.968051
dtype: float64
In [ ]:
from turtle import color
from numpy import size


x = [-6,6]


fig= plt.figure(figsize=(7,7)) #Size of Graph

plt.xlim(x) # 1 X Range [-6,6]
plt.ylim(x) # 2 Y Range [-6,6]
axis = plt.gca() #3 Get Current Axis
axis2 = axis.twinx()
axis.axes.xaxis.set_ticklabels([])
axis.axes.yaxis.set_ticklabels([])
axis2.axes.xaxis.set_ticklabels([])
axis2.axes.yaxis.set_ticklabels([])
axis2.spines['right'].set_color('black')
axis2.spines['right'].set_linewidth(2)
axis.spines['top'].set_linewidth(2)
axis.spines['bottom'].set_linewidth(2)
axis.spines['left'].set_linewidth(2)
axis.spines['left'].set_visible(True)
axis.spines['right'].set_visible(True)
axis.spines['top'].set_visible(True)
axis.spines['bottom'].set_visible(True)

axis2.spines['left'].set_visible(True)
axis2.spines['right'].set_visible(True)
axis2.spines['top'].set_visible(True)
axis2.spines['bottom'].set_visible(True)


axis2.set_ylabel('chaotic')
plt.grid(False)
axis.set_ylabel('lawful', color='black')
axis.set_xlabel('evil', color='black')
plt.plot(axis.get_xlim(),[0,0],'slateblue',linewidth=3) #4 X Axis Plots Line Across
plt.plot([0,0],axis.get_ylim(),'slateblue',linewidth=3) #5 Y Axis Plots Line Across
plt.plot(axis2.get_xlim(),[0,0],'slateblue',linewidth=3) #4 X Axis Plots Line Across
plt.plot([0,0],axis2.get_ylim(),'slateblue',linewidth=3) #5 Y Axis Plots Line Across


plt.title("good", size= 'medium')





plt.plot(X, Y,'*',markersize=15)


plt.show()
In [ ]:
html_str = mpld3.fig_to_html(fig)
Html_file= open("index2.html","w")
Html_file.write(html_str)
Html_file.close()
In [ ]:
import mpld3