#!/usr/bin/perl -w

use strict;
use warnings;
use Carp;

use WebService::Browshot;

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


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

my ($code, $png) = $browshot->simple(url => 'http://mobilito.net/', cache => 60 * 60 * 24 * 365, instance_id => 12); # 1 year cache, free screenshot
# code above is blocking, it will return when the screenshot finished or failed

if ($code == 200) { # success
	open PNG, "> screenshot.png" or croak "Could not open screenshot.png: $!\n";
	binmode PNG;
	print PNG $png;
	close PNG;
	
	print "Screenshot saved to screenshot.png\n";
}
else {
	print "Screenshot failed!\n";
	# the reason for the error is sent as part of the HTTP response in the X-Error header but it is not exposed by this library
}

# quicker way to save a screenshot
my ($new_code, $file) = $browshot->simple_file(url => 'http://mobilito.net/', file => "mobilito.png", cache => 0, instance_id => 65, screen_width => 1280, screen_height => 1024); # no cache, premium browser
if ($file ne '') {
	print "Screenshot saved to $file\n";
}
else {
	print "The screenshot failed\n";
}