Class: Rypper::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/rypper/cli.rb

Constant Summary collapse

OPTS =
[
  ['--help', '-h', GetoptLong::NO_ARGUMENT],
  ['--output', '-o', GetoptLong::REQUIRED_ARGUMENT],
  ['--overwrite', '-w', GetoptLong::NO_ARGUMENT],
]

Class Method Summary collapse

Class Method Details

.getoptObject



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rypper/cli.rb', line 11

def self.getopt()
  opts = {}
  GetoptLong.new(*OPTS).each do |opt, arg|
    opt_sym = opt.sub('--', '').to_sym
    opt_type = OPTS.find {|e| e.first == opt}.last
    if opt_type == GetoptLong::NO_ARGUMENT
      opts[opt_sym] = true
    else
      opts[opt_sym] = arg
    end
  end
  opts
end

.mainObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
# File 'lib/rypper/cli.rb', line 25

def self.main()
  opts = self.getopt()
  argv = ARGV
  if argv.count != 2
    puts "USAGE: ruby #{$0} <uri> <selector>"
    exit 1
  end
  uri = Rypper::URI.new(argv[0]) # 'http://www.mangafox.com/manga/history_s_strongest_disciple_kenichi/v[01-45:vol]/c[001-459:chap]/[1-99:pic].html'
  uri.parse!
  uri.first!

  extractor = nil
  extractor = Rypper::Extractor.new(argv[1]) # '#image'
  counter = uri.counter[uri.order.last]

  puts "Processing #{uri.uri} ..."
  while true
    html_uri = uri.to_uri
    puts " * #{html_uri} ..."
    html = Rypper::Loader.get(html_uri)
    if html.is_a?(String)
      extractor.extract!(html_uri, html).each do |image_uri|
        if image_uri.is_a?(String)
          image_path = uri.to_path(File.extname(image_uri))
          if opts.has_key?(:output)
            image_path = File.join(opts[:output], image_path)
          end
          print "   * #{image_uri} --> #{image_path} ..."
          if !File.exists?(image_path) || opts.has_key?(:overwrite)
            Rypper::Loader.mkdir!(File.dirname(image_path))
            image_file = File.open(image_path, 'w')
            image_file.binmode
            image_file.write(Rypper::Loader.get(image_uri))
            image_file.close
            puts ' OK'
          else
            puts ' Exists: Skipping'
          end
        else
          puts ' * Imageless'
        end
      end
    else
      counter.last!
      puts ' * Last'
    end
    uri.next!
    break if uri.first?
  end
  
  puts 'OK'
  
  exit 0
end