-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspotify.py
68 lines (46 loc) · 1.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
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import pandas as pd
# Get cid and secret by logging in here https://developer.spotify.com/dashboard/
cid = ''
secret=''
client_credentials_manager = SpotifyClientCredentials(client_id=cid, client_secret=secret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
def basic_info(title, artist):
query = "artist:"+artist + " track:" + title
track_results = sp.search(q=query, type='track')
if not track_results:
return ()
t = track_results['tracks']['items'][0]
artist_name = t['artists'][0]['name']
track_name = t['name']
return( track_name, artist_name)
def find_song(title, artist):
query = "artist:"+artist + " track:" + title
track_results = sp.search(q=query, type='track')
if not track_results['tracks']['items']:
return []
t = track_results['tracks']['items'][0]
artist_name = t['artists'][0]['name']
track_name = t['name']
popularity = t['popularity']
track_id = t['id']
print(artist_name)
print(track_name)
print(popularity)
print(track_id)
info = (artist_name, track_name)
#track_dataframe = pd.DataFrame({'artist_name' : artist_name, 'track_name' : track_name, 'popularity' : popularity})
#print(track_dataframe.shape)
#track_dataframe.head()
#print(track_dataframe)
song_data = sp.audio_features(tracks=[track_id])
print('------------------------------------------------')
print(song_data)
song_extract = []
for item in song_data:
song_extract = [item['danceability'], item['tempo'], item['energy'], item['valence'], item['mode'], item['loudness'], item['instrumentalness']]
print(song_extract)
return song_extract
if __name__ == "__main__":
find_song("Baby", "Justin Bieber")