require 'img_dl' require 'fileutils' require 'uri' module ImgDl module Cli CLEAR = case RUBY_PLATFORM when /win/i, /ming/i "cls" else "clear" end HELP = <<-HELP Usage: img_dl [OPTION]... URL SAVEPATH Search Images from URL and save to SAVEPATH.You can specified regular expression as a filter or set a limit num,etc. Example: img_dl http://google.com /home/me/download/icons OPTION: -r Enable recursive search, when this option parsed you should option -ul or -il to limit search,otherwise the program maybe can't stop -ul Limit recursive urls count, Only if you specified -r -il Limit download images count -ur Regex filter for recursive url, Example -ur www.foo.bai\/\?page=\d+ -ir Regex filter for images,Example -ir .gif$ -pf Save the file prefix -in Interval, default value is 0 -h print this help -version Print version HELP NOT_MATCH = "Arguments not match!" class << self def not_match puts NOT_MATCH puts HELP exit end def valid_save_path path FileUtils.mkdir_p path end def valid_url url URI url end def parse_to_options args case args.size when 0,1 case args.first when '-h' puts HELP when '-version' puts VERSION else not_match end else save_path = args.pop url = args.pop options = {} options[:recursive] = args.delete '-r' not_match if args.size.odd? args.each_slice(2) do |opt,arg| case opt when '-ul' options[:url_limit_count] = arg.to_i when '-il' options[:image_limit_count] = arg.to_i when '-ur' options[:url_reg] = Regexp.new arg when '-ir' options[:image_reg] = Regexp.new arg when '-pf' options[:prefix] = arg when '-in' options[:interval] = arg.to_i else puts "option '#{opt}' not support! please check out img_dl -h" exit end end parser = ImgDl::Parser.new(url,save_path,options) Thread.start{parser.start;puts 'All done.';exit} parser end end def prompt parser system CLEAR puts "url parser status: #{parser.status}" puts "downloader status: #{parser.dl_status}" puts "recursive urls: #{parser.url_count}" puts "images download queue: #{parser.image_count}" puts "downloaded images: #{parser.downloaded_image_count}" puts "successes: #{parser.success_download}" puts "errors: #{parser.error_urls.size}" end def run parse_to_options ARGV end end end end parser = ImgDl::Cli.run if parser at_exit do until parser.error_urls.empty? puts parser.error_urls.shift puts end ImgDl::Cli.prompt parser end $stdout.sync = true loop do ImgDl::Cli.prompt parser sleep 1 end end