Module: ImgDl::Cli

Defined in:
lib/img_dl/cli.rb

Constant Summary collapse

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 Method Summary collapse

Class Method Details

.not_matchObject



33
34
35
36
# File 'lib/img_dl/cli.rb', line 33

def not_match
  puts NOT_MATCH
  puts HELP
end

.parse_to_options(args) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/img_dl/cli.rb', line 46

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
      exit 1
    end
  else
    save_path = args.pop
    url = args.pop
    options = {}
    options[:recursive] = args.delete '-r'
    if args.size.odd?
      not_match 
      exit 1
    end
    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 1
      end
    end
    parser = ImgDl::Parser.new(url,save_path,options)
    Thread.start{parser.start;puts 'All done.';exit}
    parser
  end
end

.prompt(parser) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/img_dl/cli.rb', line 92

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

.runObject



103
104
105
# File 'lib/img_dl/cli.rb', line 103

def run
  parse_to_options ARGV
end

.valid_save_path(path) ⇒ Object



38
39
40
# File 'lib/img_dl/cli.rb', line 38

def valid_save_path path
  FileUtils.mkdir_p path
end

.valid_url(url) ⇒ Object



42
43
44
# File 'lib/img_dl/cli.rb', line 42

def valid_url url
  URI url
end