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 | |
Eric Boren | a1db799 | 2021-03-25 09:04:43 -0400 | [diff] [blame] | 9 | from __future__ import print_function |
rmistry | d8a620b | 2016-08-10 07:00:43 -0700 | [diff] [blame] | 10 | import optparse |
| 11 | import os |
rmistry | d8a620b | 2016-08-10 07:00:43 -0700 | [diff] [blame] | 12 | import urllib |
| 13 | |
| 14 | |
| 15 | PARENT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 16 | |
| 17 | |
Tyler Denniston | 96a9749 | 2021-01-28 13:56:49 -0500 | [diff] [blame] | 18 | def 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 Boren | a1db799 | 2021-03-25 09:04:43 -0400 | [diff] [blame] | 38 | print('Downloading %s to %s' % (file_url, dest_file)) |
Tyler Denniston | 96a9749 | 2021-01-28 13:56:49 -0500 | [diff] [blame] | 39 | urllib.urlretrieve(file_url, dest_file) |
rmistry | d8a620b | 2016-08-10 07:00:43 -0700 | [diff] [blame] | 40 | |
| 41 | |
| 42 | if '__main__' == __name__: |
| 43 | option_parser = optparse.OptionParser() |
| 44 | option_parser.add_option( |
Tyler Denniston | 96a9749 | 2021-01-28 13:56:49 -0500 | [diff] [blame] | 45 | '-i', '--input_file', |
| 46 | help='Path to the text file containing URLs. Each line should contain a ' |
rmistry | d8a620b | 2016-08-10 07:00:43 -0700 | [diff] [blame] | 47 | 'single URL.', |
| 48 | default=os.path.join(PARENT_DIR, 'svgs.txt')) |
| 49 | option_parser.add_option( |
| 50 | '-o', '--output_dir', |
Tyler Denniston | 96a9749 | 2021-01-28 13:56:49 -0500 | [diff] [blame] | 51 | 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] | 52 | option_parser.add_option( |
| 53 | '-p', '--prefix', |
Tyler Denniston | 96a9749 | 2021-01-28 13:56:49 -0500 | [diff] [blame] | 54 | help='The prefix which downloaded files will begin with.', |
Ravi Mistry | af10923 | 2017-08-16 10:30:40 -0400 | [diff] [blame] | 55 | default='') |
Tyler Denniston | 96a9749 | 2021-01-28 13:56:49 -0500 | [diff] [blame] | 56 | 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) |
rmistry | d8a620b | 2016-08-10 07:00:43 -0700 | [diff] [blame] | 61 | options, unused_args = option_parser.parse_args() |
| 62 | |
| 63 | if not options.output_dir: |
| 64 | raise Exception('Must specify --output_dir') |
Tyler Denniston | 96a9749 | 2021-01-28 13:56:49 -0500 | [diff] [blame] | 65 | |
| 66 | download_files(options.input_file, options.output_dir, |
| 67 | options.prefix, options.keep_common_prefix) |