6"""Downloads SVGs into a specified directory."""
9from __future__
import print_function
15PARENT_DIR = os.path.dirname(os.path.realpath(__file__))
19 with open(input_file,
'r')
as f:
22 if keep_common_prefix:
23 common_prefix = os.path.commonprefix(lines)
26 file_url = url.strip()
28 if keep_common_prefix:
29 rel_file = file_url.replace(common_prefix,
'')
30 dest_dir = os.path.join(output_dir, os.path.dirname(rel_file))
34 dest_file = os.path.join(dest_dir, prefix + os.path.basename(file_url))
35 if not os.path.exists(dest_dir):
38 print(
'Downloading %s to %s' % (file_url, dest_file))
39 urllib.urlretrieve(file_url, dest_file)
42if '__main__' == __name__:
43 option_parser = optparse.OptionParser()
44 option_parser.add_option(
46 help=
'Path to the text file containing URLs. Each line should contain a '
48 default=os.path.join(PARENT_DIR,
'svgs.txt'))
49 option_parser.add_option(
51 help=
'The output dir where downloaded SVGs and images will be stored in.')
52 option_parser.add_option(
54 help=
'The prefix which downloaded files will begin with.',
56 option_parser.add_option(
57 '-k',
'--keep_common_prefix',
58 help=
'Preserve everything in the URL after the common prefix as directory '
60 action=
'store_true', default=
False)
61 options, unused_args = option_parser.parse_args()
63 if not options.output_dir:
64 raise Exception(
'Must specify --output_dir')
67 options.prefix, options.keep_common_prefix)
def print(*args, **kwargs)
def download_files(input_file, output_dir, prefix, keep_common_prefix)