-
Notifications
You must be signed in to change notification settings - Fork 1
/
echonest.py
executable file
·92 lines (82 loc) · 3.32 KB
/
echonest.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
#!ENV/bin/python
'''
Quick Script Which Retrieves Data From Echonest for Use in the Machine Learning Algorithm
'''
from pyechonest import config, playlist
from CONFIG import echonest, algorithm
from pydub import AudioSegment
import urllib
import logging
import os
import time
config.ECHO_NEST_API_KEY = echonest['apiKey']
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
api_calls = 0
songs_added = 0
# Retrieves song preview, stores it in directory structure
def download_song(genre, song, training=True):
logger.debug('Downloading Song: %s' % song)
global api_calls
global songs_added
try:
api_calls += 1
if api_calls >= 20:
time.sleep(20) # Sleep for 20 seconds to prevent going over API limit
api_calls = 0
tracks = song.get_tracks('7digital-US')
preview_url = tracks[0].get('preview_url')
logger.debug('Preview URL for Song %s: %s' % (song, preview_url))
# Save under .wav since mp3 will be converted later
filename = song.artist_name + '-' + song.title + '.wav'
filename = filename.replace(' ', '_')
# Genreate folder structure if does not exist
cur_dir = os.getcwd()
if not os.path.exists(cur_dir):
os.makedirs(cur_dir)
if training:
cur_dir += '/training'
else:
cur_dir += '/testing'
if not os.path.exists(cur_dir):
os.makedirs(cur_dir)
cur_dir += '/' + genre
if not os.path.exists(cur_dir):
os.makedirs(cur_dir)
# Download file
file_path = cur_dir + '/' + filename
urllib.urlretrieve(preview_url, file_path)
# Convert from mp3 to wav file
song = AudioSegment.from_mp3(file_path)
song.export(file_path, format="wav")
songs_added += 1
return file_path
except Exception, e:
logging.exception("Could not download file")
def main():
global songs_added
for genre in algorithm['genres']:
# Generate Playlist For Genre for training size as well as testing
logger.debug('Getting Playlist for Genre: %s' % genre)
songs_added = 0
genre_playlist = playlist.basic(type='genre-radio', genres=[genre], \
results=int((algorithm['training_size']+algorithm['testing_size'])*2), buckets=['id:7digital-US', 'tracks'])
for song in genre_playlist:
if songs_added < algorithm['training_size']:
download_song(genre, song, True)
elif songs_added - algorithm['training_size'] < algorithm['testing_size']:
download_song(genre, song, False)
else:
break
if songs_added < algorithm['training_size'] + algorithm['testing_size']:
genre_playlist = playlist.basic(type='genre-radio', genres=[genre], \
results=int((algorithm['training_size']+algorithm['testing_size'])*2), buckets=['id:7digital-US', 'tracks'])
for song in genre_playlist:
if songs_added < algorithm['training_size']:
download_song(genre, song, True)
elif songs_added - algorithm['training_size'] < algorithm['testing_size']:
download_song(genre, song, False)
else:
break
if __name__ == '__main__':
main()