forked from alastair/scrobbyl
-
Notifications
You must be signed in to change notification settings - Fork 2
/
echonest.py
55 lines (45 loc) · 1.28 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
#!/usr/bin/python
import os
import sys
import urllib2
import urlparse
import urllib
import json
API_KEY = "F4LP3UJVBPYSPVKRZ"
def _do_en_query(method, postdata=None, **kwargs):
args = {}
for k,v in kwargs.items():
args[k] = v.encode("utf8")
args["api_key"] = API_KEY
args["format"]="json"
url=urlparse.urlunparse(('http',
'developer.echonest.com',
'/api/v4/%s' % method,
'',
urllib.urlencode(args),
''))
#print >> sys.stderr, "opening url",url
f = urllib2.Request(url)
try:
f = urllib2.urlopen(f)
except Exception, e:
print >> sys.stderr, e.msg
print >> sys.stderr, e.fp.read()
raise
return json.loads(f.read())
def artist_profile(artistid):
return _do_en_query("artist/profile", bucket="id:musicbrainz", id=artistid)
def fp_lookup(code):
return _do_en_query("song/identify", code=code)
def track_profile(id):
return _do_en_query("track/profile", id=id)
def pp(data):
print json.dumps(data, indent=4)
def main():
pp(artist_profile("ARH6W4X1187B99274F"))
if __name__ =="__main__":
if len(sys.argv) < 2:
print "usage: %s [mbid|] <args>" % sys.argv[0]
sys.exit()
else:
main()