-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path021-echonest.py
More file actions
executable file
·66 lines (51 loc) · 2.13 KB
/
021-echonest.py
File metadata and controls
executable file
·66 lines (51 loc) · 2.13 KB
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
#!/usr/bin/env python
"""
Demonstrates fetching Echonest data for a song
Get a key! https://developer.echonest.com/account/register
eg: ./021-echonest.py "data/06 Cliantro Vision.mp3"
"""
import argparse
import json
import requests
import hashlib
import time
api_key = "OHIGAA8TFWUEWMX6K"
def get_analysis( song ):
"""Returns a json object that contains the echonest analysis for the supplied song"""
md5 = hashlib.md5(open(song, 'rb').read()).hexdigest()
url = "http://developer.echonest.com/api/v4/track/profile?api_key={0}&format=json&md5={1}&bucket=audio_summary".format(api_key, md5)
r = requests.get(url)
profile = json.loads( r.text )
if profile["response"]["status"]["code"] == 5: # The Identifier specified does not exist
url = 'http://developer.echonest.com/api/v4/track/upload'
files = {'track': open(song, 'rb')}
data = {'filetype': 'mp3', 'api_key': api_key}
print "Uploading {0}...".format(song)
r = requests.post(url, files=files, data=data)
response = json.loads( r.text )
if response["response"]["status"]["code"]==0: # Upload succeeded
print "Waiting 2 seconds for analysis..."
time.sleep(2)
return get_analysis( song )
elif profile["response"]["track"]["status"]=="pending":
print "Analysis pending. Waiting 2 more seconds..."
time.sleep(2)
return get_analysis( song )
elif profile["response"]["track"]["status"]=="complete":
url = profile["response"]["track"]["audio_summary"]["analysis_url"]
r = requests.get(url)
analysis = json.loads( r.text )
return analysis
def main():
parser = argparse.ArgumentParser(description='Download some videos')
parser.add_argument('--key', type=str, help='Echonest developers key', default=api_key)
parser.add_argument('song', type=str, nargs=1, help='The song to read id3 tags from')
args = parser.parse_args()
analysis = get_analysis(args.song[0])
print "Sections: {0}".format(len(analysis["sections"]))
print "Bars: {0}".format(len(analysis["bars"]))
print "Beats: {0}".format(len(analysis["beats"]))
print "Tatums: {0}".format(len(analysis["tatums"]))
print "Segments: {0}".format(len(analysis["segments"]))
if __name__ == '__main__':
main()