-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathechonest.py
91 lines (75 loc) · 2.78 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
#-*- coding: utf-8 -*-
import urllib, json, csv, time, io, sys
import pandas as pd
data = pd.read_csv("/home/drew/data.csv")
library = data[['song_id']].values
songs = {}
size = len(library)
sys.stderr = open("log.txt", "w")
for key, value in enumerate(library[0]):
if songs.get('id') is None:
songs['id'] = []
url = "http://developer.echonest.com/api/v4/song/search?api_key=NHSTSBEENVYUFRLY5&title=thunderstruck&artist=ac/dc&bucket=audio_summary&bucket=artist_discovery&bucket=artist_familiarity&bucket=artist_hotttnesss&bucket=artist_location&bucket=song_currency&bucket=song_discovery&bucket=song_hotttnesss&bucket=song_type"
response = urllib.urlopen(url);
data = json.loads(response.read())['response']['songs'][0]
for key, value in data.iteritems():
if type(value) is dict:
for key2, value2 in data[key].iteritems():
if songs.get(key2) is None:
songs[key2] = []
else:
if songs.get(key) is None:
songs[key] = []
#library = library[1585:]
for i in xrange(len(library)):
libList = {}
enid = library[i][0]
print str(i) + ": " + enid + " <br>"
url = "http://developer.echonest.com/api/v4/song/profile?api_key=NHSTSBEENVYUFRLY5&id="+enid+"&bucket=audio_summary&bucket=artist_discovery&bucket=artist_familiarity&bucket=artist_hotttnesss&bucket=artist_location&bucket=song_currency&bucket=song_discovery&bucket=song_hotttnesss&bucket=song_type"
response = urllib.urlopen(url)
data = json.loads(response.read())['response']['songs']
if len(data) > 0:
data = data[0]
else:
print "--skipped <br>"
time.sleep(5)
size -= 1
continue
#print json.dumps(data, indent=4, separators=(',', ': '))
for key, value in enumerate(library[i]):
libList['id'] = value
for key, value in data.iteritems():
if type(value) is dict:
for key2, value2 in data[key].iteritems():
libList[key2] = value2
elif type(value) is list:
temp = []
sep = ','
for key2, value2 in enumerate(value):
temp.append(str(value2))
libList[key] = sep.join(temp)
else:
libList[key] = value
for key, value in songs.iteritems():
if libList.get(key):
songs[key].append(libList[key])
else:
songs[key].append("")
time.sleep(3.2)
size = len(songs['id'])
libKeys = []
libVals = []
libSongs = []
temp = []
for key, value in songs.iteritems():
if key not in libKeys:
libKeys.append(key)
libSongs.append(libKeys)
for i in xrange(size):
temp = []
for key, value in songs.iteritems():
temp.append(unicode(songs[key][i]).replace(',', ' '))
libSongs.append([unicode(s).encode("utf-8") for s in temp])
with open('enmusic.csv', 'w') as fp:
a = csv.writer(fp, delimiter=',')
a.writerows(libSongs)