#!/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	=> 0, # no more debug information
);


# Create a text file with a lsit of URLs
open SOURCE, "> batch.txt" or croak "Cannot create file: $!\n";
print SOURCE "google.com\n";
print SOURCE "mobilito.net\n";
close SOURCE;


my $batch = $browshot->batch_create(file => "batch.txt", instance_id => 65, screen_width => 1600, 
  screen_height => 1200, size => 'page', width => 600); # Get thumbnails
  
if ($batch->{status} eq 'error') {
	print "Batch failed: ", $batch->{error}, "\n";
	exit(0);
}

print "Batch #", $batch->{id}, " in process\n";

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

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

# Check how many screenshot failed
print $batch->{failed}, "/", $batch->{count}, " screenshots failed\n";

# The batch succeeded, download the archive. There may be fore than 1 URL
foreach my $url (@{ $batch->{urls} }) {
	print "Downloading $url...\n";
	# Use LWP::Simple or LWP::UserAgent to download the archive
}

if (scalar @{ $batch->{urls} } > 1) {
	# Run 7a if the archive was split into multiple files
}