#!/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' }) # Get full size thumbnails
if (batch['status'] == 'error')
  puts "Batch failed: #{batch['error']}\n"
  exit(0)
end

print "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

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