-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathhelpers.py
135 lines (106 loc) · 4.25 KB
/
helpers.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
127
128
129
130
131
132
133
134
135
import spotipy
import time
from IPython.core.display import clear_output
from spotipy import SpotifyClientCredentials, util
client_id='your_spotify_client_id'
client_secret='your_spotify_client_secret'
redirect_uri='your_url_to_redirect'
username = 'your_username_spotify_code'
scope = 'playlist-modify-public'
#Credentials to access the Spotify Music Data
manager = SpotifyClientCredentials(client_id,client_secret)
sp = spotipy.Spotify(client_credentials_manager=manager)
#Credentials to access to the Spotify User's Playlist, Favorite Songs, etc.
token = util.prompt_for_user_token(username,scope,client_id,client_secret,redirect_uri)
spt = spotipy.Spotify(auth=token)
def get_albums_id(ids):
album_ids = []
results = sp.artist_albums(ids)
for album in results['items']:
album_ids.append(album['id'])
return album_ids
def get_album_songs_id(ids):
song_ids = []
results = sp.album_tracks(ids,offset=0)
for songs in results['items']:
song_ids.append(songs['id'])
return song_ids
def get_songs_features(ids):
meta = sp.track(ids)
features = sp.audio_features(ids)
# 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']
ids = meta['id']
# features
acousticness = features[0]['acousticness']
danceability = features[0]['danceability']
energy = features[0]['energy']
instrumentalness = features[0]['instrumentalness']
liveness = features[0]['liveness']
valence = features[0]['valence']
loudness = features[0]['loudness']
speechiness = features[0]['speechiness']
tempo = features[0]['tempo']
key = features[0]['key']
time_signature = features[0]['time_signature']
track = [name, album, artist, ids, release_date, popularity, length, danceability, acousticness,
energy, instrumentalness, liveness, valence, loudness, speechiness, tempo, key, time_signature]
columns = ['name','album','artist','id','release_date','popularity','length','danceability','acousticness','energy','instrumentalness',
'liveness','valence','loudness','speechiness','tempo','key','time_signature']
return track,columns
def get_songs_artist_ids_playlist(ids):
playlist = sp.playlist_tracks(ids)
songs_id = []
artists_id = []
for result in playlist['items']:
songs_id.append(result['track']['id'])
for artist in result['track']['artists']:
artists_id.append(artist['id'])
return songs_id,artists_id
def download_albums(music_id,artist=False):
if artist == True:
ids_album = get_albums_id(music_id)
else:
if type(music_id) == list:
ids_album = music_id
elif type(music_id) == str:
ids_album = list([music_id])
tracks = []
for ids in ids_album:
#Obtener Ids de canciones en album
song_ids = get_album_songs_id(ids=ids)
#Obtener feautres de canciones en album
ids2 = song_ids
print(f"Album Length: {len(song_ids)}")
time.sleep(.6)
track, columns = get_songs_features(ids2)
tracks.append(track)
print(f"Song Added: {track[0]} By {track[2]} from the album {track[1]}")
clear_output(wait = True)
clear_output(wait = True)
print("Music Downloaded!")
return tracks,columns
def download_playlist(id_playlist,n_songs):
songs_id = []
tracks = []
for i in range(0,n_songs,100):
playlist = spt.playlist_tracks(id_playlist,limit=100,offset=i)
for songs in playlist['items']:
songs_id.append(songs['track']['id'])
counter = 1
for ids in songs_id:
time.sleep(.6)
track,columns = get_songs_features(ids)
tracks.append(track)
print(f"Song {counter} Added:")
print(f"{track[0]} By {track[2]} from the album {track[1]}")
clear_output(wait = True)
counter+=1
clear_output(wait = True)
print("Music Downloaded!")
return tracks,columns