rmistry | d8a620b | 2016-08-10 07:00:43 -0700 | [diff] [blame] | 1 | #!/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 | |
| 9 | import optparse |
| 10 | import os |
rmistry | d8a620b | 2016-08-10 07:00:43 -0700 | [diff] [blame] | 11 | import urllib |
| 12 | |
| 13 | |
| 14 | PARENT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 15 | |
| 16 | |
Tyler Denniston | 96a9749 | 2021-01-28 13:56:49 -0500 | [diff] [blame] | 17 | def download_files(input_file, output_dir, prefix, keep_common_prefix): |
| 18 | with open(input_file, 'r') as f: |
| 19 | lines = f.readlines() |
| 20 | |
| 21 | if keep_common_prefix: |
| 22 | common_prefix = os.path.commonprefix(lines) |
| 23 | |
| 24 | for url in lines: |
| 25 | file_url = url.strip() |
| 26 | |
| 27 | if keep_common_prefix: |
| 28 | rel_file = file_url.replace(common_prefix, '') |
| 29 | dest_dir = os.path.join(output_dir, os.path.dirname(rel_file)) |
| 30 | else: |
| 31 | dest_dir = output_dir |
| 32 | |
| 33 | dest_file = os.path.join(dest_dir, prefix + os.path.basename(file_url)) |
| 34 | if not os.path.exists(dest_dir): |
| 35 | os.makedirs(dest_dir) |
| 36 | |
| 37 | print 'Downloading %s to %s' % (file_url, dest_file) |
| 38 | urllib.urlretrieve(file_url, dest_file) |
rmistry | d8a620b | 2016-08-10 07:00:43 -0700 | [diff] [blame] | 39 | |
| 40 | |
| 41 | if '__main__' == __name__: |
| 42 | option_parser = optparse.OptionParser() |
| 43 | option_parser.add_option( |
Tyler Denniston | 96a9749 | 2021-01-28 13:56:49 -0500 | [diff] [blame] | 44 | '-i', '--input_file', |
| 45 | help='Path to the text file containing URLs. Each line should contain a ' |
rmistry | d8a620b | 2016-08-10 07:00:43 -0700 | [diff] [blame] | 46 | 'single URL.', |
| 47 | default=os.path.join(PARENT_DIR, 'svgs.txt')) |
| 48 | option_parser.add_option( |
| 49 | '-o', '--output_dir', |
Tyler Denniston | 96a9749 | 2021-01-28 13:56:49 -0500 | [diff] [blame] | 50 | help='The output dir where downloaded SVGs and images will be stored in.') |
Ravi Mistry | af10923 | 2017-08-16 10:30:40 -0400 | [diff] [blame] | 51 | option_parser.add_option( |
| 52 | '-p', '--prefix', |
Tyler Denniston | 96a9749 | 2021-01-28 13:56:49 -0500 | [diff] [blame] | 53 | help='The prefix which downloaded files will begin with.', |
Ravi Mistry | af10923 | 2017-08-16 10:30:40 -0400 | [diff] [blame] | 54 | default='') |
Tyler Denniston | 96a9749 | 2021-01-28 13:56:49 -0500 | [diff] [blame] | 55 | option_parser.add_option( |
| 56 | '-k', '--keep_common_prefix', |
| 57 | help='Preserve everything in the URL after the common prefix as directory ' |
| 58 | 'hierarchy.', |
| 59 | action='store_true', default=False) |
rmistry | d8a620b | 2016-08-10 07:00:43 -0700 | [diff] [blame] | 60 | options, unused_args = option_parser.parse_args() |
| 61 | |
| 62 | if not options.output_dir: |
| 63 | raise Exception('Must specify --output_dir') |
Tyler Denniston | 96a9749 | 2021-01-28 13:56:49 -0500 | [diff] [blame] | 64 | |
| 65 | download_files(options.input_file, options.output_dir, |
| 66 | options.prefix, options.keep_common_prefix) |