forked from giuband/msc-thesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_list.py
66 lines (57 loc) · 1.88 KB
/
print_list.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
import sys, os
import essentia
from operator import itemgetter
from ConfigParser import SafeConfigParser
from essentia.standard import MetadataReader
import pyechonest.config as echo_conf
import pyechonest.track as echo_track
import time
parser = SafeConfigParser()
parser.read('config.ini')
api_key = parser.get('echonest', 'apikey')
echo_conf.ECHO_NEST_API_KEY = api_key
path = sys.argv[1]
output_file = sys.argv[2]
files = []
# find files that need to be analyzed
for dirpath, dirnames, filenames in os.walk(path):
for filename in [f for f in filenames if f.endswith(".mp3")]:
files.append(os.path.join(dirpath, filename))
all_files = []
count = 0
for filename in files:
metadata_reader = MetadataReader(filename = filename)
metadata = metadata_reader()
track_title = metadata[0]
track_artist = metadata[1]
track_year = metadata[6]
while True:
try:
pytrack = echo_track.track_from_filename(filename)
pytrack.get_analysis()
break
except:
print "Error encountered"
time.sleep(60)
echonest_id = pytrack.id
count +=1
print "Files analyzed: ", count
echonest_id = echonest_id.decode('utf-8')
all_files.append({"title": track_title, "artist": track_artist, "year": track_year, "echonest_id": echonest_id})
all_files.sort(key=itemgetter('artist', 'year', 'title'))
# echonest_example = echo_track.track_from_filename(files[0])
# echonest_example.get_analysis()
# for key in echonest.keys():
# print key
f = open(output_file, 'w')
for file_ in all_files:
track_title = file_["title"]
track_artist = file_["artist"]
track_year = file_["year"]
echonest_id = file_["echonest_id"].encode('ascii','ignore')
track_title = track_title.replace("&", "\&")
track_artist = track_artist.replace("&", "\&")
track_year = track_year.replace("&", "\&")
output_str = track_title + " & " + track_artist + " & " + track_year + " & " + echonest_id + " \\\\ \hline \n"
f.write(output_str)
f.close()