Last change
on this file since 30008 was 30008, checked in by davidb, 9 years ago |
Generalization of command line args to accept 2 arguments
|
-
Property svn:executable
set to
*
|
File size:
1.3 KB
|
Rev | Line | |
---|
[30005] | 1 | #!/usr/bin/python
|
---|
| 2 |
|
---|
| 3 | import os
|
---|
| 4 | import os.path
|
---|
| 5 |
|
---|
| 6 | import json
|
---|
| 7 | import sys
|
---|
| 8 |
|
---|
[30008] | 9 | argc = len(sys.argv)
|
---|
[30005] | 10 |
|
---|
[30008] | 11 | input_idir = sys.argv[1] if (argc==2) or (argc==3) else "download-json-all"
|
---|
| 12 | output_dir = sys.argv[2] if argc==3 else "download-audio"
|
---|
[30005] | 13 |
|
---|
[30008] | 14 | if not os.path.isdir(output_dir):
|
---|
| 15 | print "Creating directory: " + output_dir
|
---|
| 16 | os.mkdir(output_dir)
|
---|
| 17 |
|
---|
| 18 |
|
---|
[30005] | 19 | json_files = [ jf for jf in os.listdir(input_dir) if os.path.isfile(os.path.join(input_dir,jf)) ]
|
---|
| 20 |
|
---|
| 21 | for 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 repository browser.