root/other-projects/mirex/grand-challenge/generate-jamendo-dataset/scripts-2015/get-audio-download-mp3.py @ 30008

Revision 30008, 1.3 KB (checked in by davidb, 5 years ago)

Generalization of command line args to accept 2 arguments

  • Property svn:executable set to *
Line 
1#!/usr/bin/python
2
3import os
4import os.path
5
6import json
7import sys
8
9argc = len(sys.argv)
10
11input_idir  = sys.argv[1] if (argc==2) or (argc==3)  else "download-json-all"
12output_dir = sys.argv[2] if argc==3 else "download-audio"
13
14if not os.path.isdir(output_dir):
15    print "Creating directory: " + output_dir
16    os.mkdir(output_dir)
17   
18
19json_files = [ jf for jf in os.listdir(input_dir) if os.path.isfile(os.path.join(input_dir,jf)) ]
20
21for jf in json_files:
22    json_filename = os.path.join(input_dir,jf)
23    json_data=open(json_filename).read()
24    data = json.loads(json_data)
25
26    for rec in data[u'results']:
27
28        id = int(rec[u'id'])
29        prefix = id//1000
30
31        output_prefix_dir = os.path.join(output_dir,str(prefix));
32        if not os.path.isdir(output_prefix_dir):
33            print "Creating " + output_prefix_dir
34            os.mkdir(output_prefix_dir)
35
36        output_filename = os.path.join(output_prefix_dir, str(id) + ".mp3")
37
38        if not os.path.isfile(output_filename):
39            audio_download_url = rec[u'audio']
40             
41            cmd = "wget -O " + output_filename +  " \"" + audio_download_url + "\""
42
43            print "Downloading:" +cmd
44            os.system(cmd)
45        else:
46            print "Already downloaded: " + output_filename + " => skipping"
47
Note: See TracBrowser for help on using the browser.