#!/usr/bin/env ruby

#######################
# WARNING
# Running this code sample will cost you Browshot credits
#######################

require 'browshot'

browshot = Browshot.new('my_api_key')

# Create a text file with a list of URLs
File.open("batch.txt", 'w') {|f| 
	f.write("google.com\n")
	f.write("mobilito.net\n")
}


batch = browshot.batch_create(65, "batch.txt", { 'screen_width' => 1600, 
  'screen_height' => 1200, 'size' => 'page', 'width' => 600 }); # Get thumbnails
  
if (batch['status'] == 'error')
  puts "Batch failed: #{ batch['error']}\n"
  exit(0)
end

puts "Batch ##{batch['id']} in process\n"

sleep(30)
while (batch['status'] != 'finished' && batch['status'] != 'error')
  sleep(15)
  batch = browshot.batch_info(batch['id'])
end

if (batch['status'] == 'error')
  puts "Batch failed: #{batch['error']}\n"
  exit(0)
end

# Check how many screenshot failed
puts "#{batch['failed']}/#{batch['count']} screenshots failed\n"

if (batch.key?('urls'))
  # The batch succeeded, download the archive. There may be more than 1 URL.
  batch['urls'].each { |url|
    puts "Downloading #{url}...\n"
    # Download the archive
  }

  if (batch['urls'].count > 1)
    # Run 7a if the archive was split into multiple files
    puts "7a ...\n"
  end
end