-
Notifications
You must be signed in to change notification settings - Fork 0
/
obter_dados.py
49 lines (43 loc) · 1.61 KB
/
obter_dados.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
#!/usr/bin/python3
import sys
import json
import pandas as pd
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
def autentica(client_id, client_secret):
client_credentials_manager = SpotifyClientCredentials(client_id, client_secret)
return spotipy.Spotify(client_credentials_manager = client_credentials_manager)
def nome_artista(sp, uri_artista):
artist = sp.artist(uri)
return artist['name'].replace(' ','_')
def features_top_tracks(sp, uri_artista):
top_tracks = sp.artist_top_tracks(uri)
uri_tracks = []
for track in top_tracks['tracks']:
uri_tracks.append(track['uri'])
return sp.audio_features(uri_tracks)
def converte_json(features, artist):
with open('features_' + artist.lower() + '.json','w') as fp:
json.dump(features, fp)
print('Features convertido para json.')
def converte_csv(features, artist):
df = pd.DataFrame(features)
df.to_csv('features_' + artist.lower() + '.csv', index=False)
print('Features convertido para csv.')
if __name__ == "__main__":
while True:
try:
uri = sys.argv[1]
spotify = autentica('CLIENT_ID',
'CLIENT_SECRET')
artista = nome_artista(spotify, uri)
features = features_top_tracks(spotify, uri)
converte_csv(features, artista)
converte_json(features, artista)
break
except spotipy.oauth2.SpotifyOauthError:
print('Client ID e Secret inválido.')
sys.exit()
except IndexError:
print('Informe a URI do Artista.')
sys.exit()