svg_downloader.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. """Downloads SVGs into a specified directory."""
  6. import optparse
  7. import os
  8. import sys
  9. import urllib
  10. PARENT_DIR = os.path.dirname(os.path.realpath(__file__))
  11. def downloadSVGs(svgs_file, output_dir, prefix):
  12. with open(svgs_file, 'r') as f:
  13. for url in f.xreadlines():
  14. svg_url = url.strip()
  15. dest_file = os.path.join(output_dir, prefix + os.path.basename(svg_url))
  16. print 'Downloading %s' % svg_url
  17. urllib.urlretrieve(svg_url, dest_file)
  18. if '__main__' == __name__:
  19. option_parser = optparse.OptionParser()
  20. option_parser.add_option(
  21. '-s', '--svgs_file',
  22. help='Path to the text file containing SVGs. Each line should contain a '
  23. 'single URL.',
  24. default=os.path.join(PARENT_DIR, 'svgs.txt'))
  25. option_parser.add_option(
  26. '-o', '--output_dir',
  27. help='The output dir where downloaded SVGs will be stored in.')
  28. option_parser.add_option(
  29. '-p', '--prefix',
  30. help='The prefix which downloaded SVG file will begin with.',
  31. default='')
  32. options, unused_args = option_parser.parse_args()
  33. if not options.output_dir:
  34. raise Exception('Must specify --output_dir')
  35. sys.exit(downloadSVGs(options.svgs_file, options.output_dir, options.prefix))