2 # Copyright (c) 2016 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Downloads SVGs into a specified directory."""
9 from __future__ import print_function
15 PARENT_DIR = os.path.dirname(os.path.realpath(__file__))
18 def download_files(input_file, output_dir, prefix, keep_common_prefix):
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)
42 if '__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')
66 download_files(options.input_file, options.output_dir,
67 options.prefix, options.keep_common_prefix)