#!/usr/bin/perl -w

use strict;
use warnings;
use Carp;

use WebService::Browshot;

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

my $screenshot = $browshot->screenshot_create(url => 'http://www.google.com/', instance_id => 12, size => 'screen'); # all default paramters, 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
	$browshot->screenshot_thumbnail_file(id => $screenshot->{id}, file => '/tmp/google-640.png', width => 640, height => 480);
	print "Screenshot was saved to /tmp/google-640.png\n";
}
