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 |
| 11 | import sys |
| 12 | import urllib |
| 13 | |
| 14 | |
| 15 | PARENT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 16 | |
| 17 | |
Ravi Mistry | af10923 | 2017-08-16 10:30:40 -0400 | [diff] [blame] | 18 | def downloadSVGs(svgs_file, output_dir, prefix): |
rmistry | d8a620b | 2016-08-10 07:00:43 -0700 | [diff] [blame] | 19 | with open(svgs_file, 'r') as f: |
| 20 | for url in f.xreadlines(): |
| 21 | svg_url = url.strip() |
Ravi Mistry | af10923 | 2017-08-16 10:30:40 -0400 | [diff] [blame] | 22 | dest_file = os.path.join(output_dir, prefix + os.path.basename(svg_url)) |
rmistry | d8a620b | 2016-08-10 07:00:43 -0700 | [diff] [blame] | 23 | print 'Downloading %s' % svg_url |
| 24 | urllib.urlretrieve(svg_url, dest_file) |
| 25 | |
| 26 | |
| 27 | if '__main__' == __name__: |
| 28 | option_parser = optparse.OptionParser() |
| 29 | option_parser.add_option( |
| 30 | '-s', '--svgs_file', |
| 31 | help='Path to the text file containing SVGs. Each line should contain a ' |
| 32 | 'single URL.', |
| 33 | default=os.path.join(PARENT_DIR, 'svgs.txt')) |
| 34 | option_parser.add_option( |
| 35 | '-o', '--output_dir', |
| 36 | help='The output dir where downloaded SVGs will be stored in.') |
Ravi Mistry | af10923 | 2017-08-16 10:30:40 -0400 | [diff] [blame] | 37 | option_parser.add_option( |
| 38 | '-p', '--prefix', |
| 39 | help='The prefix which downloaded SVG file will begin with.', |
| 40 | default='') |
rmistry | d8a620b | 2016-08-10 07:00:43 -0700 | [diff] [blame] | 41 | options, unused_args = option_parser.parse_args() |
| 42 | |
| 43 | if not options.output_dir: |
| 44 | raise Exception('Must specify --output_dir') |
Ravi Mistry | af10923 | 2017-08-16 10:30:40 -0400 | [diff] [blame] | 45 | sys.exit(downloadSVGs(options.svgs_file, options.output_dir, options.prefix)) |