| 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 |
from __future__ import print_function |
| 10 |
import optparse |
| 11 |
import os |
| 12 |
import urllib |
| 13 |
|
| 14 |
|
| 15 |
PARENT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 16 |
|
| 17 |
|
| 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 |
|
| 38 |
print('Downloading %s to %s' % (file_url, dest_file)) |
| 39 |
urllib.urlretrieve(file_url, dest_file) |
| 40 |
|
| 41 |
|
| 42 |
if '__main__' == __name__: |
| 43 |
option_parser = optparse.OptionParser() |
| 44 |
option_parser.add_option( |
| 45 |
'-i', '--input_file', |
| 46 |
help='Path to the text file containing URLs. Each line should contain a ' |
| 47 |
'single URL.', |
| 48 |
default=os.path.join(PARENT_DIR, 'svgs.txt')) |
| 49 |
option_parser.add_option( |
| 50 |
'-o', '--output_dir', |
| 51 |
help='The output dir where downloaded SVGs and images will be stored in.') |
| 52 |
option_parser.add_option( |
| 53 |
'-p', '--prefix', |
| 54 |
help='The prefix which downloaded files will begin with.', |
| 55 |
default='') |
| 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) |
| 61 |
options, unused_args = option_parser.parse_args() |
| 62 |
|
| 63 |
if not options.output_dir: |
| 64 |
raise Exception('Must specify --output_dir') |
| 65 |
|
| 66 |
download_files(options.input_file, options.output_dir, |
| 67 |
options.prefix, options.keep_common_prefix) |