-
Notifications
You must be signed in to change notification settings - Fork 1
/
mp3_downloader.py
executable file
·50 lines (41 loc) · 1.5 KB
/
mp3_downloader.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
import os
import argparse
import urllib2
import csv
__author__ = "Oriol Nieto"
__copyright__ = "Copyright 2012, SALAMI Data Set Project"
__license__ = "GPL"
__version__ = "1.0"
__email__ = "[email protected]"
def download(url, localName):
"""Downloads the file from the url and saves it as localName."""
req = urllib2.Request(url)
r = urllib2.urlopen(req)
f = open(localName, 'wb')
f.write(r.read())
f.close()
def process(csv_file, output_dir):
"""Main process function to download all mp3s from the csv_file
and put thm in the ouput_dir."""
f = open(csv_file, "r")
file_reader = csv.reader(f)
for fields in file_reader:
id = fields[0]
url = fields[4]
print("Downloading: ", id, url)
try:
download(url, os.path.join(output_dir, id + ".mp3"))
except Exception as e:
print ("Could not retrieve:", id, url)
print("Exception:", e)
f.close()
def main():
"""Main function to parse the arguments and call the main process."""
parser = argparse.ArgumentParser(description='Evaluates the results of the analyzer with the ' \
'ground truth data.')
parser.add_argument('csv_file', action='store', help='The path to id_index_internetarchive.csv')
parser.add_argument('output_dir', action='store', help='Directory where to save all the mp3s')
args = parser.parse_args()
process(args.csv_file, args.output_dir)
if __name__ == "__main__":
main()