#!/usr/bin/perl -w

use strict;
use warnings;
use Carp;

use WebService::Browshot 1.29.0; # requires 1.29.0 or above

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



my $browshot = WebService::Browshot->new(
	key	=> 'my_api_key',
	debug	=> 0, # no more debug information
);



my $crawl = $browshot->crawl_create(url => 'https://blitapp.com/', domain => 'blitapp.com', max => 50, instance_id => 65, screen_width => 1600, screen_height => 1200, size => 'page'); # Get full size thumbnails
if ($crawl->{status} eq 'error') {
	print "Crawl failed: ", $crawl->{error}, "\n";
	exit(0);
}

print "Crawl #", $crawl->{id}, " in process\n";

sleep 30;
while ($crawl->{status} ne 'finished' && $crawl->{status} ne 'error') {
	sleep 15;
	$crawl = $browshot->crawl_info(id => $crawl->{id});
}

if ($crawl->{status} eq 'error') {
	print "crawl failed: ", $crawl->{error}, "\n";
	exit(0);
}

# The crawl succeeded, download the thumbnails.
foreach my $url (@{ $crawl->{urls} }) {
	print "Downloading ", $url->screenshot_url, "...\n";
	 $browshot->screenshot_thumbnail_file(id => $url->id, file => '/tmp/' + $url->id +'.png')
}
