blob: 88c6a7fcbf53a59f0514ab9c0a11c8d57ddb92ac [file] [log] [blame]
rmistryd8a620b2016-08-10 07:00:43 -07001#!/usr/bin/env python
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.
5
6"""Downloads SVGs into a specified directory."""
7
8
Eric Borena1db7992021-03-25 09:04:43 -04009from __future__ import print_function
rmistryd8a620b2016-08-10 07:00:43 -070010import optparse
11import os
rmistryd8a620b2016-08-10 07:00:43 -070012import urllib
13
14
15PARENT_DIR = os.path.dirname(os.path.realpath(__file__))
16
17
Tyler Denniston96a97492021-01-28 13:56:49 -050018def download_files(input_file, output_dir, prefix, keep_common_prefix):
19 with open(input_file, 'r') as f:
20 lines = f.readlines()
21
22 if keep_common_prefix:
23 common_prefix = os.path.commonprefix(lines)
24
25 for url in lines:
26 file_url = url.strip()
27
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))
31 else:
32 dest_dir = output_dir
33
34 dest_file = os.path.join(dest_dir, prefix + os.path.basename(file_url))
35 if not os.path.exists(dest_dir):
36 os.makedirs(dest_dir)
37
Eric Borena1db7992021-03-25 09:04:43 -040038 print('Downloading %s to %s' % (file_url, dest_file))
Tyler Denniston96a97492021-01-28 13:56:49 -050039 urllib.urlretrieve(file_url, dest_file)
rmistryd8a620b2016-08-10 07:00:43 -070040
41
42if '__main__' == __name__:
43 option_parser = optparse.OptionParser()
44 option_parser.add_option(
Tyler Denniston96a97492021-01-28 13:56:49 -050045 '-i', '--input_file',
46 help='Path to the text file containing URLs. Each line should contain a '
rmistryd8a620b2016-08-10 07:00:43 -070047 'single URL.',
48 default=os.path.join(PARENT_DIR, 'svgs.txt'))
49 option_parser.add_option(
50 '-o', '--output_dir',
Tyler Denniston96a97492021-01-28 13:56:49 -050051 help='The output dir where downloaded SVGs and images will be stored in.')
Ravi Mistryaf109232017-08-16 10:30:40 -040052 option_parser.add_option(
53 '-p', '--prefix',
Tyler Denniston96a97492021-01-28 13:56:49 -050054 help='The prefix which downloaded files will begin with.',
Ravi Mistryaf109232017-08-16 10:30:40 -040055 default='')
Tyler Denniston96a97492021-01-28 13:56:49 -050056 option_parser.add_option(
57 '-k', '--keep_common_prefix',
58 help='Preserve everything in the URL after the common prefix as directory '
59 'hierarchy.',
60 action='store_true', default=False)
rmistryd8a620b2016-08-10 07:00:43 -070061 options, unused_args = option_parser.parse_args()
62
63 if not options.output_dir:
64 raise Exception('Must specify --output_dir')
Tyler Denniston96a97492021-01-28 13:56:49 -050065
66 download_files(options.input_file, options.output_dir,
67 options.prefix, options.keep_common_prefix)