#!/usr/bin/env ruby

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

require 'browshot'

browshot = Browshot.new('my_api_key')

crawl = browshot.crawl_create(65, "blitapp.com", "https://blitapp.com/", { 'screen_width' => 1600, 'screen_height' => 1200, 'size' => 'page' }) # Get full size thumbnails
if (crawl['status'] == 'error')
  puts "Crawl failed: #{crawl['error']}\n"
  exit(0)
end

print "Crawl ##{crawl['id']} in process\n"

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

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

# The crawl succeeded, download the thumbnails
if (crawl.key?('urls'))
	crawl['urls'].each { |screenshot|
		puts "Downloading #{screenshot['id']}...\n"
    browshot.screenshot_thumbnail_file(screenshot['id'], "file-#{screenshot['id']}.png", { 'right' => 768, 'bottom' => 768, 'height' => 300, 'width' => 300, 'ratio' => 'fit' })
	}
end