-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify_api.py
63 lines (52 loc) · 2.35 KB
/
spotify_api.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
from __future__ import print_function # (at top of module)
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy
CID = '44fe800219464e71a583778604f1a2df'
SECRET = '2d56de73e69941958a549f69f838c8dd'
class Spotify:
""""Spotify web API data scraping"""
def __init__(self, artist, song, song_id):
""""initiating class using search string"""
self.artist = artist
self.song = song
self.id = song_id
client_credentials_manager = SpotifyClientCredentials(CID, SECRET)
self.sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
def get_value_dict(self):
""""get data from web API
:param self: getting sp object and search str
:return: 2 dictionaries of features for track """
sp = self.sp
search_str = self.artist + ' ' + self.song
result = sp.search(search_str, type='track')
try:
value_dict = result['tracks']['items'][0] # assuming the 1st one is the right one
except IndexError: # no results
return None, None
album_keys = ['release_date', 'name']
track_keys = ['available_markets', 'duration_ms', 'explicit', 'popularity']
excluded_audio_feat = ['type', 'id', 'uri', 'track_href', 'analysis_url', 'mode', 'time_signature',
'duration_ms', 'key']
audio_analysis_keys = ['bars', 'beats', 'tatums', 'sections', 'segments']
track_dict = {key: value_dict[key] for key in track_keys}
track_dict['song_name'] = self.song
track_dict['artist_name'] = self.artist
track_dict['id'] = self.id
for key in album_keys:
track_dict['album_' + key] = value_dict['album'][key]
audio_feature = sp.audio_features(value_dict['id'])[0]
for key in audio_feature.keys():
if key not in excluded_audio_feat:
track_dict['audio_' + key] = audio_feature[key]
audio_analysis = sp.audio_analysis(value_dict['id'])
audio_analysis_dict = dict()
for key in audio_analysis_keys:
audio_analysis_dict[key] = audio_analysis[key]
audio_analysis['feature'] = key
return track_dict, audio_analysis_dict
x = Spotify('the weekend', 'blinding lights', 'whatever')
a, b = x.get_value_dict()
print(a)
print(a.keys())
print(b)
print(b.keys())