#!/usr/bin/perl -w

use strict;
use warnings;
use Carp;

use WebService::Browshot;



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

my $screenshot = $browshot->screenshot_create(url => 'http://www.google.com/', instance_id => 12, size => 'page'); # all default parameters, instance_id = 12 (free)
# If the screenshot is already in cache, it could be finished already. Otherwise, wait longer
while ($screenshot->{status} ne 'finished' &&  $screenshot->{status} ne 'error') {
	print "Wait...\n";
	sleep 10;
	$screenshot = $browshot->screenshot_info(id => $screenshot->{id});
}

# screenshot is done: finished (sucessful) or error (failed)
if ($screenshot->{status} eq 'error') {
	print "Screenshot failed: ", $screenshot->{error}, "\n"; # display the reason for the error
}
else { # request the thumbnail
	my $image = $browshot->screenshot_thumbnail(id => $screenshot->{id});
	
	# save the screenshot
	open PNG, "> browshot.png" or croak "Cannot open browshot.png: $!\n";
	binmode PNG;
	print PNG $image;
	close PNG;
}