brookemckim/hlspider

View on GitHub
bin/hlspider

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
begin
  require 'hlspider'
rescue LoadError
  require 'rubygems'
  require 'hlspider'
end

require 'optparse'

options = {}

opts_parser = OptionParser.new do |opts|
  opts.banner  = 'Downloads m3u8 playlists and confirms their segments are aligned.'
  opts.banner += ''

  opts.on('-p', '--playlists PLAYLISTS', Array, 'URL(s) to playlist(s)') do |playlists|
    options[:playlists] = playlists
  end
  
  options[:loop] = 1
  opts.on('-l', '--loop TIMES', Integer) do |l|
    options[:loop] = l || 5
  end 
  
  options[:sleep] = 3
  opts.on('-s', '--sleep SECONDS', Integer) do |s|
    options[:sleep] = s
  end   
  
  opts.on( '-h', '--help', 'Display this screen' ) do
    puts opts
    exit
  end
end
opts_parser.parse!

if options[:playlists]
  spider = HLSpider::Spider.new(options[:playlists])
else
  puts "No playlists were specified."
  exit(1)
end
  
if options[:loop] == 0
  x = -1
else
  x = 1
end
  
while x <= options[:loop] do
  spider.crawl!
  
  if spider.aligned?
    puts "--- Aligned at segment : #{spider.last_segments[0]} ---"
  else
    puts "--- Unaligned with segments : #{spider.last_segments.join(', ')} ---"
  end

  x += 1 unless options[:loop] == 0
  
  sleep(options[:sleep])
end