Class: Videoinfo::CLI

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

Constant Summary collapse

ENV_IMAGE_HOST =
'VIDEOINFO_IMAGE_HOST'

Class Method Summary collapse

Class Method Details

.run(args) ⇒ Object

Analyze a video from command line arguments and print the result to STDOUT.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
# File 'lib/videoinfo/cli.rb', line 9

def self.run(args)
  Videoinfo.interactive = true
  image_host            = ENV[ENV_IMAGE_HOST] || Videoinfo.image_host.class.to_s.split('::').last
  screenshots           = 2
  option_parser         = OptionParser.new do |opts|
    opts.banner = 'Usage: videoinfo [options] "MOVIENAME" file'
    opts.on('-i', '--image-host=IMAGEHOST', "The ImageHost to use for uploading screenshots. Default: #{image_host}") do |host|
      image_host = host
    end
    opts.on('-s', '--screenshots=SCREENSHOTS', "The number of screenshots to create, max 7. Default: #{screenshots}") do |ss|
      screenshots = ss.to_i
      if screenshots > 7
        STDERR.puts opts
        exit 1
      end
    end
    opts.on('-n', '--no-prompt', 'Disable interactive mode') do
      Videoinfo.interactive = false
    end
    opts.on('-h', '--help', 'Show this message') do
      puts opts
      exit
    end
  end

  name, file = option_parser.parse!(args)

  begin
    Videoinfo.image_host = Videoinfo::ImageHosts.const_get(image_host).new
  rescue
    hosts = Videoinfo::ImageHosts.constants.map(&:to_s).join(', ')
    STDERR.puts "ERROR: '#{image_host}' is an unknown image host. Available hosts: #{hosts}"
    exit 1
  end

  if name.to_s == '' || file.to_s == ''
    STDERR.puts option_parser
    exit 1
  end

  begin
    result = Videoinfo.analyze_movie(name, file, screenshots)
    puts result.to_s
  rescue Error => e
    STDERR.puts "ERROR: #{e.message}"
    exit 1
  end
end