blib/lib/WebService/Browshot.pm

filter: Statementstmt 62.1% 166 / 267 Branchbran 51.2% 41 / 80 Conditioncond 26.4% 28 / 106 MC/DCmcdc 9.0% 6 / 66 Subroutinesub 59.5% 28 / 47 Podpod 71.0% 27 / 38 totaltotal 49.0% 296 / 604 CC 82 SCAR 27.5
CC
82
Cov
52%
screenshot_thumbnail
110.0
simple_file
30.0
screenshot_thumbnail_file
30.0
CRAP 15.6
↑ Summary
1 package WebService::Browshot;
2  
3 3 ▶use 5.006006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use URI::Encode qw(uri_encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;
14 3 ▶use IO::Socket::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 IO::Socket::SSL::set_ctx_defaults(
16 SSL_verifycn_scheme => 'www',
17 SSL_verify_mode => 0,
18 verify_mode => 0,
19 );
20  
21 3 our $VERSION = '1.32.0';
22  
23 =head1 NAME
24  
25 WebService::Browshot - Perl extension for Browshot (L<https://browshot.com/>), a web service to create screenshots of web pages.
26  
27 =head1 SYNOPSIS
28  
29 use WebService::Browshot;
30  
31 my $browshot = WebService::Browshot->new(key => 'my_key');
32 my $screenshot = $browshot->screenshot_create(url => 'http://www.google.com/');
33 [...]
34 $browshot->screenshot_thumbnail_file(id => $screenshot->{id}, file => 'google.png');
35  
36 =head1 DESCRIPTION
37  
38 Browshot (L<http://www.browshot.com/>) is a web service to easily make screenshots of web pages in any screen size, as any device: iPhone, iPad, Android, Nook, PC, etc. Browshot has full Flash, JavaScript, CSS, & HTML5 support.
39  
40 The latest API version is detailed at L<http://browshot.com/api/documentation>. WebService::Browshot follows the API documentation very closely: the function names are similar to the URLs used (screenshot/create becomes C<screenshot_create()>, instance/list becomes C<instance_list()>, etc.), the request arguments are exactly the same, etc.
41  
42 The library version matches closely the API version it handles: WebService::Browshot 1.0.0 is the first release for the API 1.0, WebService::Browshot 1.1.1 is the second release for the API 1.1, etc.
43  
44 WebService::Browshot can handle most the API updates within the same major version, e.g. WebService::Browshot 1.0.0 should be compatible with the API 1.1 or 1.2.
45  
46 The source code is available on github at L<https://github.com/juliensobrier/browshot-perl>.
47  
48  
49 =head1 METHODS
50  
51 =head2 new()
52  
53 my $browshot = WebService::Browshot->new(key => 'my_key', base => 'http://api.browshot.com/api/v1/', debug => 1]);
54  
55 Create a new WebService::Browshot object. You must pass your API key (go to you Dashboard to find your API key).
56  
57 Arguments:
58  
59 =over 4
60  
61 =item key
62  
63 Required. API key.
64  
65 =item base
66  
67 Optional. Base URL for all API requests. You should use the default base provided by the library. Be careful if you decide to use HTTP instead of HTTPS as your API key could be sniffed and your account could be used without your consent.
68  
69 =item debug
70  
71 Optional. Set to 1 to print debug output to the standard output. 0 (disabled) by default.
72  
73 =item timeout
74  
75 Optional. Set the request timeout - in seconds - against the API. Defaults to 90s.
76  
77 =back
78  
79 C<last_error> contains the last error message, it is NEVER reset, i.e last_error may not be empty after a successful API call if an earlier call failed.
80  
81 =cut
82  
83 2 ▶sub new {
Subroutine new: called CC=5 SCAR=16.4
Pod documented
84 2 my ($self, %args) = @_;
85  
86 2 my $ua = LWP::UserAgent->new();
87 2 ▶ $ua->timeout($args{'timeout'} || 90);
Condition50%1/2
$args{'timeout'} || 90
l!l
02
MC/DC0%0/1
$args{'timeout'} || 90
$args{'timeout'}
Truth table50%1/2
A
A= $args{'timeout'}
Aresult
11
00
88 2 $ua->env_proxy;
89 2 $ua->max_redirect(32); # for the simple API only
90 2 $ua->agent("WebService::Browshot $VERSION");
91 2 $ua->ssl_opts( verify_hostnames => 0 );
92  
93 my $browshot = {
94 _key => $args{key} || '',
95 _base => $args{base} || 'https://api.browshot.com/api/v1/',
96 2 ▶ _debug => $args{debug} || 0,
Condition66%4/6
$args{'key'} || ''
l!l
11
$args{'base'} || 'https://api.browshot.com/api/v1/'
l!l
02
$args{'debug'} || 0
l!l
02
MC/DC100%1/1
$args{'key'} || ''
$args{'key'}
MC/DC0%0/1
$args{'base'} || 'https://api.browshot.com/api/v1/'
$args{'base'}
MC/DC0%0/1
$args{'debug'} || 0
$args{'debug'}
Truth table100%2/2
A
A= $args{'key'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'base'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'debug'}
Aresult
11
00
97  
98 _retry => 2,
99 last_error => '',
100  
101 _ua => $ua,
102 };
103  
104 2 return bless($browshot, $self);
105 }
106  
107  
108 =head2 api_version()
109  
110 Return the API version handled by the library. Note that this library can usually handle new arguments in requests without requiring an update.
111  
112 =cut
113  
114 2 ▶sub api_version {
Subroutine api_version: called CC=2 SCAR=7.6
Pod documented
115 2 my ($self, %args) = @_;
116  
117 2 ▶ if ($VERSION =~ /^(\d+\.\d+)\.\d/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WebService::Browshot::VERSION =~ /^(\d+\.\d+)\.\d/) 2 0 2
118 2 return $1;
119 }
120  
121 0 return $VERSION;
122 }
123  
124  
125  
126 =head2 simple()
127  
128 $browshot->simple(url => 'http://mobilito.net')
129  
130 Retrieve a screenshot in one function. Note: by default, screenshots are cached for 24 hours. You can tune this value with the cache=X parameter.
131  
132 Return an array (status code, PNG). See L<http://browshot.com/api/documentation#simple> for the list of possible status codes.
133  
134 Arguments:
135  
136 See L<http://browshot.com/api/documentation#simple> for the full list of possible arguments.
137  
138 =over 4
139  
140 =item url
141  
142 Required. URL of the website to create a screenshot of.
143  
144 =back
145  
146 =cut
147  
148 0 ▶sub simple {
Subroutine simple: not called CC=1 SCAR=6.9
Pod documented
149 0 my ($self, %args) = @_;
150  
151 0 my $url = $self->make_url(action => 'simple', parameters => { %args });
152 0 my $res = $self->{_ua}->get($url);
153  
154 # $self->info($res->message);
155 # $self->info($res->request->as_string);
156 # $self->info($res->as_string);
157
158 0 return ($res->code, $res->decoded_content);
159 }
160  
161 =head2 simple_file()
162  
163 $browshot->simple_file(url => 'http://mobilito.net', file => '/tmp/mobilito.png')
164  
165 Retrieve a screenshot and save it locally in one function. Note: by default, screenshots are cached for 24 hours. You can tune this value with the cache=X parameter.
166  
167 Return an array (status code, file name). The file name is empty if the screenshot was not retrieved. See L<http://browshot.com/api/documentation#simple> for the list of possible status codes.
168  
169 Arguments:
170  
171 See L<http://browshot.com/api/documentation#simple> for the full list of possible arguments.
172  
173 =over 4
174  
175 =item url
176  
177 Required. URL of the website to create a screenshot of.
178  
179 =item file
180  
181 Required. Local file name to write to.
182  
183 =back
184  
185 =cut
186  
187 0 ▶sub simple_file {
Subroutine simple_file: not called CC=5 SCAR=34.0
Pod documented
188 0 my ($self, %args) = @_;
189 0 ▶ my $file = $args{file} || $self->error("Missing file in simple_file");
Condition0%0/3
$args{'file'} || $self->error('Missing file in simple_file')
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'file'} || $self->error('Missing file in simple_file')
$args{'file'} $self->error('Missing file in simple_file')
Truth table0%0/3
A || B
A= $args{'file'}B= $self->error('Missing file in simple_file')
ABresult
1X1
011
000
190
191 0 ▶ if (-d $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $file) 0 0 0
192 0 $self->error("You must specify a file path, not a folder, to save the screenshot");
193 0 return (400, '');
194 }
195  
196 0 my $url = $self->make_url(action => 'simple', parameters => { %args });
197 0 my $res = $self->{_ua}->get($url);
198  
199 0 my $content = $res->decoded_content;
200  
201 0 ▶ if ($content ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($content ne '') { } 0 0 0
202 0 ▶ open TARGET, "> $file" or $self->error("Cannot open $file for writing: $!");
Branch: 1 branch
BranchTrueFalseTotal
open TARGET, "> $file" or $self->error("Cannot open $file for writing: $!") 0 0 0
203 0 binmode TARGET;
204 0 print TARGET $content;
205 0 close TARGET;
206  
207 0 return ($res->code, $file);
208 }
209 else {
210 0 $self->error("No thumbnail retrieved");
211 0 return ($res->code, '');
212 }
213 }
214  
215 =head2 instance_list()
216  
217 Return the list of instances as a hash reference. See L<http://browshot.com/api/documentation#instance_list> for the response format.
218  
219 =cut
220  
221 0 ▶sub instance_list {
Subroutine instance_list: not called CC=1 SCAR=6.9
Pod documented
222 0 my ($self, %args) = @_;
223
224 0 return $self->return_reply(action => 'instance/list');
225 }
226  
227 =head2 instance_info()
228  
229 $browshot->instance_info(id => 2)
230  
231 Return the details of an instance. See L<http://browshot.com/api/documentation#instance_info> for the response format.
232  
233 Arguments:
234  
235 =over 4
236  
237 =item id
238  
239 Required. Instance ID
240  
241 =back
242  
243 =cut
244  
245 0 ▶sub instance_info {
Subroutine instance_info: not called CC=2 SCAR=17.9
Pod documented
246 0 my ($self, %args) = @_;
247 0 ▶ my $id = $args{id} || $self->error("Missing id in instance_info");
Condition0%0/3
$args{'id'} || $self->error('Missing id in instance_info')
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'id'} || $self->error('Missing id in instance_info')
$args{'id'} $self->error('Missing id in instance_info')
Truth table0%0/3
A || B
A= $args{'id'}B= $self->error('Missing id in instance_info')
ABresult
1X1
011
000
248  
249 0 return $self->return_reply(action => 'instance/info', parameters => { id => $id });
250 }
251  
252 =head2 browser_list()
253  
254 Return the list of browsers as a hash reference. See L<http://browshot.com/api/documentation#browser_list> for the response format.
255  
256 =cut
257  
258 0 ▶sub browser_list {
Subroutine browser_list: not called CC=1 SCAR=6.9
Pod documented
259 0 my ($self, %args) = @_;
260
261 0 return $self->return_reply(action => 'browser/list');
262 }
263  
264 =head2 browser_info()
265  
266 $browshot->browser_info(id => 2)
267  
268 Return the details of a browser. See L<http://browshot.com/api/documentation#browser_info> for the response format.
269  
270 Arguments:
271  
272 =over 4
273  
274 =item id
275  
276 Required. Browser ID
277  
278 =back
279  
280 =cut
281  
282 0 ▶sub browser_info {
Subroutine browser_info: not called CC=2 SCAR=17.9
Pod documented
283 0 my ($self, %args) = @_;
284 0 ▶ my $id = $args{id} || $self->error("Missing id in browser_info");
Condition0%0/3
$args{'id'} || $self->error('Missing id in browser_info')
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'id'} || $self->error('Missing id in browser_info')
$args{'id'} $self->error('Missing id in browser_info')
Truth table0%0/3
A || B
A= $args{'id'}B= $self->error('Missing id in browser_info')
ABresult
1X1
011
000
285  
286 0 return $self->return_reply(action => 'browser/info', parameters => { id => $id });
287 }
288  
289  
290 =head2 screenshot_create()
291  
292 $browshot->screenshot_create(url => 'http://wwww.google.com/', instance_id => 3, size => 'page')
293  
294 Request a screenshot. See L<http://browshot.com/api/documentation#screenshot_create> for the response format.
295 Note: by default, screenshots are cached for 24 hours. You can tune this value with the cache=X parameter.
296  
297 API 1.32 capture options are accepted as named arguments without conversion:
298 C<pdf>, C<pdf_format>, C<pdf_background>, C<pdf_landscape>, C<pdf_one_page>,
299 C<pdf_margin>, C<pdf_margin_top>, C<pdf_margin_bottom>, C<pdf_margin_left>,
300 C<pdf_margin_right>, C<video>, C<video_format>, C<video_duration>,
301 C<video_quality>, C<video_fps>, C<video_scale>, C<device_scale>, and C<hide_ads>.
302 These options also work with C<screenshot_multiple()>, C<batch_create()>, and
303 C<crawl_create()>.
304  
305 Use C<pdf =E<gt> 1> to generate a PDF (1 additional credit), or
306 C<video =E<gt> 1> to record WebM, MP4, or animated GIF with
307 C<video_format =E<gt> 'webm'>, C<'mp4'>, or C<'gif'>. WebM is the default.
308 C<video_duration> is 1-30 seconds (default 15); video adds 1 credit per started
309 5 seconds of requested duration, for every format. PDF and video require a paid
310 Chrome-based instance. Wait for C<status> to be C<finished> before downloading.
311  
312 C<device_scale =E<gt> 2> enables retina screenshots and C<hide_ads =E<gt> 1>
313 enables ad blocking, without an additional charge. Retina cannot be combined
314 with video. For additional countries, select the appropriate C<instance_id>
315 from C<instance_list()>; do not relocate an instance with a country override.
316 For example, instance 373 is Chrome in France and 377 is Chrome in India.
317  
318 my $screenshot = $browshot->screenshot_create(
319 url => 'https://example.org/', instance_id => 373,
320 pdf => 1, pdf_format => 'a4', device_scale => 2, hide_ads => 1,
321 );
322  
323 See L<https://browshot.com/api/documentation> for option defaults and limits.
324  
325 Arguments:
326  
327 See L<http://browshot.com/api/documentation#screenshot_create> for the full list of possible arguments.
328  
329 =over 4
330  
331 =item url
332  
333 Required. URL of the website to create a screenshot of.
334  
335 =item instance_id
336  
337 Optional. Instance ID to use for the screenshot.
338  
339 =item size
340  
341 Optional. Screenshot size.
342  
343 =back
344  
345 =cut
346  
347 4 ▶sub screenshot_create {
Subroutine screenshot_create: called CC=2 SCAR=7.1
Pod documented
348 4 my ($self, %args) = @_;
349 # my $url = $args{url} || $self->error("Missing url in screenshot_create");
350 # my $instance_id = $args{instance_id};
351 # my $screen = $args{screen};
352 # my $size = $args{size} || "screen";
353 # my $cache = $args{cache};
354 # my $priority = $args{priority};
355  
356 4 ▶ $self->error("Missing url in screenshot_create") if (! defined($args{url}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"url"} 0 4 4
357 # $args{size} = "screen" if (! defined($args{size}));
358  
359 4 return $self->return_reply(action => 'screenshot/create', parameters => { %args });
360 }
361  
362 =head2 screenshot_info()
363  
364 $browshot->screenshot_info(id => 568978)
365  
366 Get information about a screenshot requested previously. See L<http://browshot.com/api/documentation#screenshot_info> for the response format.
367  
368 Arguments:
369  
370 =over 4
371  
372 =item id
373  
374 Required. Screenshot ID.
375  
376 =back
377  
378 =cut
379  
380 0 ▶sub screenshot_info {
Subroutine screenshot_info: not called CC=2 SCAR=17.9
Pod documented
381 0 my ($self, %args) = @_;
382 0 ▶ my $id = $args{id} || $self->error("Missing id in screenshot_info");
Condition0%0/3
$args{'id'} || $self->error('Missing id in screenshot_info')
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'id'} || $self->error('Missing id in screenshot_info')
$args{'id'} $self->error('Missing id in screenshot_info')
Truth table0%0/3
A || B
A= $args{'id'}B= $self->error('Missing id in screenshot_info')
ABresult
1X1
011
000
383  
384  
385 0 return $self->return_reply(action => 'screenshot/info', parameters => { %args });
386 }
387  
388 =head2 screenshot_list()
389  
390 $browshot->screenshot_list(limit => 50)
391  
392 Get details about screenshots requested. See L<http://browshot.com/api/documentation#screenshot_list> for the response format.
393  
394 Arguments:
395  
396 =over 4
397  
398 =item limit
399  
400 Optional. Maximum number of screenshots to retrieve.
401  
402 =back
403  
404 =cut
405  
406 0 ▶sub screenshot_list {
Subroutine screenshot_list: not called CC=1 SCAR=6.9
Pod documented
407 0 my ($self, %args) = @_;
408  
409 0 return $self->return_reply(action => 'screenshot/list', parameters => { %args });
410 }
411  
412 =head2 screenshot_search()
413  
414 $browshot->screenshot_search(url => 'google.com')
415  
416 Get details about screenshots requested. See L<http://browshot.com/api/documentation#screenshot_search> for the response format.
417  
418 Arguments:
419  
420 =over 4
421  
422 =item url
423  
424 Required. URL string to look for.
425  
426 =back
427  
428 =cut
429  
430 0 ▶sub screenshot_search {
Subroutine screenshot_search: not called CC=2 SCAR=17.9
Pod documented
431 0 my ($self, %args) = @_;
432 0 ▶ my $url = $args{url} || $self->error("Missing url in screenshot_search");
Condition0%0/3
$args{'url'} || $self->error('Missing url in screenshot_search')
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'url'} || $self->error('Missing url in screenshot_search')
$args{'url'} $self->error('Missing url in screenshot_search')
Truth table0%0/3
A || B
A= $args{'url'}B= $self->error('Missing url in screenshot_search')
ABresult
1X1
011
000
433  
434 0 return $self->return_reply(action => 'screenshot/search', parameters => { %args });
435 }
436  
437 =head2 screenshot_host()
438  
439 $browshot->screenshot_host(id => 12345, hosting => 'browshot')
440  
441 Host a screenshot or thumbnail. See L<http://browshot.com/api/documentation#screenshot_host> for the response format.
442  
443 Arguments:
444  
445 =over 4
446  
447 =item id
448  
449 Required. Screenshot ID.
450  
451 =back
452  
453 =cut
454  
455 0 ▶sub screenshot_host {
Subroutine screenshot_host: not called CC=2 SCAR=17.9
Pod documented
456 0 my ($self, %args) = @_;
457 0 ▶ my $id = $args{id} || $self->error("Missing id in screenshot_host");
Condition0%0/3
$args{'id'} || $self->error('Missing id in screenshot_host')
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'id'} || $self->error('Missing id in screenshot_host')
$args{'id'} $self->error('Missing id in screenshot_host')
Truth table0%0/3
A || B
A= $args{'id'}B= $self->error('Missing id in screenshot_host')
ABresult
1X1
011
000
458  
459 0 return $self->return_reply(action => 'screenshot/host', parameters => { %args });
460 }
461  
462  
463 =head2 screenshot_thumbnail()
464  
465 $browshot->screenshot_thumbnail(id => 52942, width => 500)
466  
467 Retrieve the screenshot, or a thumbnail. See L<http://browshot.com/api/documentation#screenshot_thumbnail> for the response format.
468  
469 Return an empty string if the image could not be retrieved.
470  
471 Arguments:
472  
473 See L<http://browshot.com/api/documentation#screenshot_thumbnail> for the full list of possible arguments.
474  
475 =over 4
476  
477 =item id
478  
479 Required. Screenshot ID.
480  
481 =item width
482  
483 Optional. Maximum width of the thumbnail.
484  
485 =item height
486  
487 Optional. Maximum height of the thumbnail.
488  
489 =back
490  
491 =cut
492 0 ▶sub screenshot_thumbnail {
Subroutine screenshot_thumbnail: not called CC=10 SCAR=47.0
Pod documented
493 0 my ($self, %args) = @_;
494  
495 0 ▶ if (exists($args{url}) && $args{url} =~ /image\/(\d+)\?/i && ! exists($args{id})) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args{'url'} and $args{'url'} =~ m[image/(\d+)\?]i and not exists $args{'id'}) { } 0 0 0
elsif (not exists $args{'id'}) { } 0 0 0
Condition0%0/6
exists $args{"url"} and $args{"url"} =~ m[image/(\d+)\?]i and not exists $args{"id"}
!ll&&!rl&&r
000
exists $args{"url"} and $args{"url"} =~ m[image/(\d+)\?]i
!ll&&!rl&&r
000
MC/DC0%0/3
exists $args{"url"} and $args{"url"} =~ m[image/(\d+)\?]i and not exists $args{"id"}
exists $args{"url"} $args{"url"} =~ m[image/(\d+)\?]i not exists $args{"id"}
Truth table0%0/4
A and B and not(C)
A= exists $args{"url"}B= $args{"url"} =~ m[image/(\d+)\?]iC= not exists $args{"id"}
ABCresult
0XX0
10X0
1100
1111
496 # get ID from url
497 0 $args{id} = $1;
498  
499 0 ▶ if ($args{url} =~ /&width=(\d+)\?/i && ! exists($args{width})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"url"} =~ /&width=(\d+)\?/i and not exists $args{"width"}) 0 0 0
Condition0%0/3
$args{"url"} =~ /&width=(\d+)\?/i and not exists $args{"width"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"url"} =~ /&width=(\d+)\?/i and not exists $args{"width"}
$args{"url"} =~ /&width=(\d+)\?/i not exists $args{"width"}
Truth table0%0/3
A and not(B)
A= $args{"url"} =~ /&width=(\d+)\?/iB= not exists $args{"width"}
ABresult
0X0
100
111
500 0 $args{width} = $1;
501 }
502 0 ▶ if ($args{url} =~ /&height=(\d+)\?/i && ! exists($args{height})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"url"} =~ /&height=(\d+)\?/i and not exists $args{"height"}) 0 0 0
Condition0%0/3
$args{"url"} =~ /&height=(\d+)\?/i and not exists $args{"height"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"url"} =~ /&height=(\d+)\?/i and not exists $args{"height"}
$args{"url"} =~ /&height=(\d+)\?/i not exists $args{"height"}
Truth table0%0/3
A and not(B)
A= $args{"url"} =~ /&height=(\d+)\?/iB= not exists $args{"height"}
ABresult
0X0
100
111
503 0 $args{height} = $1;
504 }
505
506 }
507 elsif(! exists($args{id}) ) {
508 0 $self->error("Missing id and url in screenshot_thumbnail");
509 0 return '';
510 }
511  
512  
513 0 my $url = $self->make_url(action => 'screenshot/thumbnail', parameters => { %args });
514 0 my $res = $self->{_ua}->get($url);
515  
516 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
517 0 return $res->decoded_content; # raw image file content
518 }
519 else {
520 0 $self->error("Error in thumbnail request: " . $res->as_string);
521 0 return '';
522 }
523 }
524  
525  
526 =head2 screenshot_thumbnail_file()
527  
528 $browshot->screenshot_thumbnail_file(id => 123456, height => 500, file => '/tmp/google.png')
529  
530 Retrieve the screenshot, or a thumbnail, and save it to a file. See L<http://browshot.com/api/documentation#thumbnails> for the response format.
531  
532 Return an empty string if the image could not be retrieved or not saved. Returns the file name if successful.
533  
534 Arguments:
535  
536 See L<http://browshot.com/api/documentation#thumbnails> for the full list of possible arguments.
537  
538 =over 4
539  
540 =item url
541  
542 Required. URL of the screenshot (screenshot_url value retrieved from C<screenshot_create()> or C<screenshot_info()>). You will get the full image if no other argument is specified.
543  
544 =item file
545  
546 Required. Local file name to write to.
547  
548 =item width
549  
550 Optional. Maximum width of the thumbnail.
551  
552 =item height
553  
554 Optional. Maximum height of the thumbnail.
555  
556 =back
557  
558 =cut
559 0 ▶sub screenshot_thumbnail_file {
Subroutine screenshot_thumbnail_file: not called CC=5 SCAR=34.0
Pod documented
560 0 my ($self, %args) = @_;
561 0 ▶ my $file = $args{file} || $self->error("Missing file in screenshot_thumbnail_file");
Condition0%0/3
$args{'file'} || $self->error('Missing file in screenshot_thumbnail_file')
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'file'} || $self->error('Missing file in screenshot_thumbnail_file')
$args{'file'} $self->error('Missing file in screenshot_thumbnail_file')
Truth table0%0/3
A || B
A= $args{'file'}B= $self->error('Missing file in screenshot_thumbnail_file')
ABresult
1X1
011
000
562  
563 0 delete($args{file});
564 0 my $content = $self->screenshot_thumbnail(%args);
565  
566 0 my $dir = dirname($file);
567 0 ▶ if (! -d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 0 0
568 0 make_path($dir)
569 }
570  
571 0 ▶ if ($content ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($content ne '') { } 0 0 0
572 0 ▶ open TARGET, "> $file" or $self->error("Cannot open $file for writing: $!");
Branch: 1 branch
BranchTrueFalseTotal
open TARGET, "> $file" or $self->error("Cannot open $file for writing: $!") 0 0 0
573 0 binmode TARGET;
574 0 print TARGET $content;
575 0 close TARGET;
576  
577 0 return $file;
578 }
579 else {
580 0 $self->error("No thumbnail retrieved");
581 0 return '';
582 }
583 }
584  
585 =head2 screenshot_share()
586  
587 $browshot->screenshot_share(id => 12345, note => 'This is my screenshot')
588  
589 Share a screenshot. See L<http://browshot.com/api/documentation#screenshot_share> for the response format.
590  
591 Arguments:
592  
593 =over 4
594  
595 =item id
596  
597 Required. Screenshot ID.
598  
599 =item note
600  
601 Optional. Public note to add to the screenshot.
602  
603 =back
604  
605 =cut
606  
607 0 ▶sub screenshot_share {
Subroutine screenshot_share: not called CC=2 SCAR=17.9
Pod documented
608 0 my ($self, %args) = @_;
609 0 ▶ my $id = $args{id} || $self->error("Missing id in screenshot_share");
Condition0%0/3
$args{'id'} || $self->error('Missing id in screenshot_share')
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'id'} || $self->error('Missing id in screenshot_share')
$args{'id'} $self->error('Missing id in screenshot_share')
Truth table0%0/3
A || B
A= $args{'id'}B= $self->error('Missing id in screenshot_share')
ABresult
1X1
011
000
610  
611 0 return $self->return_reply(action => 'screenshot/share', parameters => { %args });
612 }
613  
614  
615 =head2 screenshot_delete()
616  
617 $browshot->screenshot_delete(id => 12345, data => 'url,metadata')
618  
619 Delete details of a screenshot. See L<http://browshot.com/api/documentation#screenshot_delete> for the response format.
620  
621 Arguments:
622  
623 =over 4
624  
625 =item id
626  
627 Required. Screenshot ID.
628  
629 =item data
630  
631 Optional. Information to delete.
632  
633 =back
634  
635 =cut
636  
637 0 ▶sub screenshot_delete {
Subroutine screenshot_delete: not called CC=2 SCAR=17.9
Pod documented
638 0 my ($self, %args) = @_;
639 0 ▶ my $id = $args{id} || $self->error("Missing id in screenshot_delete");
Condition0%0/3
$args{'id'} || $self->error('Missing id in screenshot_delete')
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'id'} || $self->error('Missing id in screenshot_delete')
$args{'id'} $self->error('Missing id in screenshot_delete')
Truth table0%0/3
A || B
A= $args{'id'}B= $self->error('Missing id in screenshot_delete')
ABresult
1X1
011
000
640  
641 0 return $self->return_reply(action => 'screenshot/delete', parameters => { %args });
642 }
643  
644 =head2 screenshot_html()
645  
646 $browshot->screenshot_html(id => 12345)
647  
648 Get the HTML code of the rendered page. See L<http://browshot.com/api/documentation#screenshot_html> for the response format.
649  
650 Arguments:
651  
652 =over 4
653  
654 =item id
655  
656 Required. Screenshot ID.
657  
658 =back
659  
660 =cut
661  
662 0 ▶sub screenshot_html {
Subroutine screenshot_html: not called CC=2 SCAR=17.9
Pod documented
663 0 my ($self, %args) = @_;
664 0 ▶ my $id = $args{id} || $self->error("Missing id in screenshot_html");
Condition0%0/3
$args{'id'} || $self->error('Missing id in screenshot_html')
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'id'} || $self->error('Missing id in screenshot_html')
$args{'id'} $self->error('Missing id in screenshot_html')
Truth table0%0/3
A || B
A= $args{'id'}B= $self->error('Missing id in screenshot_html')
ABresult
1X1
011
000
665  
666 0 return $self->return_string(action => 'screenshot/html', parameters => { %args });
667 }
668  
669 =head2 screenshot_pdf()
670  
671 my $pdf = $browshot->screenshot_pdf(id => 12345);
672  
673 Retrieve the PDF generated with C<pdf =E<gt> 1>.
674 Wait until C<screenshot_info()> reports C<status =E<gt> 'finished'>.
675 Returns the raw file bytes, or an empty string on failure with C<last_error>
676 set. Downloading does not cost additional credits.
677 See L<https://browshot.com/api/documentation#screenshot_pdf>.
678  
679 Arguments:
680  
681 =over 4
682  
683 =item id
684  
685 Required. Screenshot ID belonging to your account.
686  
687 =back
688  
689 =cut
690  
691 15 ▶sub screenshot_pdf {
Subroutine screenshot_pdf: called CC=3 SCAR=11.0
Pod documented
692 15 my ($self, %args) = @_;
693 15 ▶ return $self->error("Missing id in screenshot_pdf") if (! defined($args{id}) || $args{id} eq '');
Branch: 1 branch
BranchTrueFalseTotal
if not defined $args{"id"} or $args{"id"} eq "" 1 14 15
Condition66%2/3
not defined $args{"id"} or $args{"id"} eq ""
l!l&&r!l&&!r
1014
MC/DC50%1/2
not defined $args{"id"} or $args{"id"} eq ""
not defined $args{"id"} $args{"id"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $args{"id"}B= $args{"id"} eq ""
ABresult
1X1
011
000
694  
695 14 return $self->return_binary(action => 'screenshot/pdf', parameters => { %args }, content_type => qr{^application/pdf(?:\s*;|\s*$)}i);
696 }
697  
698 =head2 screenshot_pdf_file()
699  
700 $browshot->screenshot_pdf_file(id => 12345, file => '/tmp/capture.pdf');
701  
702 Retrieve the PDF and save it in binary mode. Missing parent
703 directories are created as with C<screenshot_thumbnail_file()>.
704 Returns the file name on success, or an empty string on failure with
705 C<last_error> set. Failed API responses are not saved as files.
706  
707 Arguments:
708  
709 =over 4
710  
711 =item id
712  
713 Required. Screenshot ID belonging to your account.
714  
715 =item file
716  
717 Required. Local file name to write to. Use the extension for the recorded format.
718  
719 =back
720  
721 =cut
722  
723 8 ▶sub screenshot_pdf_file {
Subroutine screenshot_pdf_file: called CC=1 SCAR=0.0
Pod documented
724 8 my ($self, %args) = @_;
725  
726 8 return $self->save_binary(method => 'screenshot_pdf', parameters => { %args });
727 }
728  
729 =head2 screenshot_video()
730  
731 my $video = $browshot->screenshot_video(id => 12345);
732  
733 Retrieve the WebM or MP4 video, or animated GIF generated with C<video =E<gt> 1>.
734 Wait until C<screenshot_info()> reports C<status =E<gt> 'finished'>.
735 Returns the raw file bytes, or an empty string on failure with C<last_error>
736 set. Downloading does not cost additional credits.
737 See L<https://browshot.com/api/documentation#screenshot_video>.
738  
739 Arguments:
740  
741 =over 4
742  
743 =item id
744  
745 Required. Screenshot ID belonging to your account.
746  
747 =back
748  
749 =cut
750  
751 21 ▶sub screenshot_video {
Subroutine screenshot_video: called CC=3 SCAR=11.0
Pod documented
752 21 my ($self, %args) = @_;
753 21 ▶ return $self->error("Missing id in screenshot_video") if (! defined($args{id}) || $args{id} eq '');
Branch: 1 branch
BranchTrueFalseTotal
if not defined $args{"id"} or $args{"id"} eq "" 1 20 21
Condition66%2/3
not defined $args{"id"} or $args{"id"} eq ""
l!l&&r!l&&!r
1020
MC/DC50%1/2
not defined $args{"id"} or $args{"id"} eq ""
not defined $args{"id"} $args{"id"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $args{"id"}B= $args{"id"} eq ""
ABresult
1X1
011
000
754  
755 20 return $self->return_binary(action => 'screenshot/video', parameters => { %args }, content_type => qr{^(?:video/(?:webm|mp4)|image/gif)(?:\s*;|\s*$)}i);
756 }
757  
758 =head2 screenshot_video_file()
759  
760 $browshot->screenshot_video_file(id => 12345, file => '/tmp/capture.webm');
761  
762 Retrieve the WebM or MP4 video, or animated GIF and save it in binary mode. Missing parent
763 directories are created as with C<screenshot_thumbnail_file()>.
764 Returns the file name on success, or an empty string on failure with
765 C<last_error> set. Failed API responses are not saved as files.
766  
767 Arguments:
768  
769 =over 4
770  
771 =item id
772  
773 Required. Screenshot ID belonging to your account.
774  
775 =item file
776  
777 Required. Local file name to write to. Use the extension for the recorded format.
778  
779 =back
780  
781 =cut
782  
783 10 ▶sub screenshot_video_file {
Subroutine screenshot_video_file: called CC=1 SCAR=0.0
Pod documented
784 10 my ($self, %args) = @_;
785  
786 10 return $self->save_binary(method => 'screenshot_video', parameters => { %args });
787 }
788  
789 =head2 screenshot_multiple()
790  
791 $browshot->screenshot_multiple(urls => ['http://mobilito.net/'], instances => [22, 30])
792  
793 Request multiple screenshots. See L<http://browshot.com/api/documentation#screenshot_multiple> for the response format.
794  
795 Arguments:
796  
797 =over 4
798  
799 =item urls
800  
801 Required. One or more URLs.
802  
803 =item instances
804  
805 Required. One or more instance_id.
806  
807 =back
808  
809 =cut
810  
811 1 ▶sub screenshot_multiple {
Subroutine screenshot_multiple: called CC=1 SCAR=0.0
Pod documented
812 1 my ($self, %args) = @_;
813 # my $urls = $args{urls} || $self->error("Missing urls in screenshot_multiple");
814 # my $instances = $args{instances} || $self->error("Missing instances in screenshot_multiple");
815  
816 1 return $self->return_reply(action => 'screenshot/multiple', parameters => { %args });
817 }
818  
819  
820 =head2 batch_create()
821  
822 $browshot->batch_create(file => '/my/file/urls.txt', instance_id => 65)
823  
824 Request multiple screenshots from a text file. See L<http://browshot.com/api/documentation#batch_create> for the response format.
825  
826 Arguments:
827  
828 =over 4
829  
830 =item file
831  
832 Required. Path to the text file which contains the list of URLs.
833  
834 =item instance_id
835  
836 Required. instance_id to use for all screenshots.
837  
838 =back
839  
840 =cut
841  
842 1 ▶sub batch_create {
Subroutine batch_create: called CC=3 SCAR=12.3
Pod documented
843 1 my ($self, %args) = @_;
844 1 ▶ my $file = $args{file} || $self->error("Missing file in batch_create");
Condition33%1/3
$args{'file'} || $self->error('Missing file in batch_create')
l!l&&r!l&&!r
100
MC/DC0%0/2
$args{'file'} || $self->error('Missing file in batch_create')
$args{'file'} $self->error('Missing file in batch_create')
Truth table33%1/3
A || B
A= $args{'file'}B= $self->error('Missing file in batch_create')
ABresult
1X1
011
000
845 1 ▶ my $instance_id = $args{instance_id} || $self->error("Missing instance_id in batch_create");
Condition33%1/3
$args{'instance_id'} || $self->error('Missing instance_id in batch_create')
l!l&&r!l&&!r
100
MC/DC0%0/2
$args{'instance_id'} || $self->error('Missing instance_id in batch_create')
$args{'instance_id'} $self->error('Missing instance_id in batch_create')
Truth table33%1/3
A || B
A= $args{'instance_id'}B= $self->error('Missing instance_id in batch_create')
ABresult
1X1
011
000
846  
847 1 delete $args{file};
848 1 return $self->return_post_reply(action => 'batch/create', parameters => { %args }, file => $file);
849 }
850  
851 =head2 batch_info()
852  
853 $browshot->batch_info(id => 5)
854  
855 Get information about a screenshot batch requested previously. See L<http://browshot.com/api/documentation#batch_info> for the response format.
856  
857 Arguments:
858  
859 =over 4
860  
861 =item id
862  
863 Required. Batch ID.
864  
865 =back
866  
867 =cut
868  
869 0 ▶sub batch_info {
Subroutine batch_info: not called CC=2 SCAR=17.9
Pod documented
870 0 my ($self, %args) = @_;
871 0 ▶ my $id = $args{id} || $self->error("Missing id in batch_info");
Condition0%0/3
$args{'id'} || $self->error('Missing id in batch_info')
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'id'} || $self->error('Missing id in batch_info')
$args{'id'} $self->error('Missing id in batch_info')
Truth table0%0/3
A || B
A= $args{'id'}B= $self->error('Missing id in batch_info')
ABresult
1X1
011
000
872  
873 0 return $self->return_reply(action => 'batch/info', parameters => { %args });
874 }
875  
876  
877 =head2 crawl_create()
878  
879 $browshot->crawl_create(domain => 'blitapp.com', url => 'https://blitapp.com/', max => 50, instance_id => 65)
880  
881 Crawl a domain and screenshot all pages. See L<http://browshot.com/api/documentation#crawl_create> for the response format.
882  
883 Arguments:
884  
885 =over 4
886  
887 =item domain
888  
889 Required. Domain to crawl.
890  
891 =item url
892  
893 Required. URl to start with.
894  
895 =item instance_id
896  
897 Required. instance_id to use for all screenshots.
898  
899 =back
900  
901 =cut
902  
903 1 ▶sub crawl_create {
Subroutine crawl_create: called CC=4 SCAR=16.6
Pod documented
904 1 my ($self, %args) = @_;
905 1 ▶ my $domain = $args{domain} || $self->error("Missing domain in crawl_create");
Condition33%1/3
$args{'domain'} || $self->error('Missing domain in crawl_create')
l!l&&r!l&&!r
100
MC/DC0%0/2
$args{'domain'} || $self->error('Missing domain in crawl_create')
$args{'domain'} $self->error('Missing domain in crawl_create')
Truth table33%1/3
A || B
A= $args{'domain'}B= $self->error('Missing domain in crawl_create')
ABresult
1X1
011
000
906 1 ▶ my $url = $args{url} || $self->error("Missing url in crawl_create");
Condition33%1/3
$args{'url'} || $self->error('Missing url in crawl_create')
l!l&&r!l&&!r
100
MC/DC0%0/2
$args{'url'} || $self->error('Missing url in crawl_create')
$args{'url'} $self->error('Missing url in crawl_create')
Truth table33%1/3
A || B
A= $args{'url'}B= $self->error('Missing url in crawl_create')
ABresult
1X1
011
000
907 1 ▶ my $instance_id = $args{instance_id} || $self->error("Missing instance_id in crawl_create");
Condition33%1/3
$args{'instance_id'} || $self->error('Missing instance_id in crawl_create')
l!l&&r!l&&!r
100
MC/DC0%0/2
$args{'instance_id'} || $self->error('Missing instance_id in crawl_create')
$args{'instance_id'} $self->error('Missing instance_id in crawl_create')
Truth table33%1/3
A || B
A= $args{'instance_id'}B= $self->error('Missing instance_id in crawl_create')
ABresult
1X1
011
000
908  
909 1 return $self->return_reply(action => 'crawl/create', parameters => { %args });
910 }
911  
912 =head2 crawl__info()
913  
914 $browshot->crawl__info(id => 5)
915  
916 Get information about a crawl_ requested previously. See L<http://browshot.com/api/documentation#crawl__info> for the response format.
917  
918 Arguments:
919  
920 =over 4
921  
922 =item id
923  
924 Required. Crawl ID.
925  
926 =back
927  
928 =cut
929  
930 0 ▶sub crawl_info {
Subroutine crawl_info: not called CC=2 SCAR=17.9
Pod undocumented
931 0 my ($self, %args) = @_;
932 0 ▶ my $id = $args{id} || $self->error("Missing id in crawl_info");
Condition0%0/3
$args{'id'} || $self->error('Missing id in crawl_info')
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'id'} || $self->error('Missing id in crawl_info')
$args{'id'} $self->error('Missing id in crawl_info')
Truth table0%0/3
A || B
A= $args{'id'}B= $self->error('Missing id in crawl_info')
ABresult
1X1
011
000
933  
934 0 return $self->return_reply(action => 'crawl/info', parameters => { %args });
935 }
936  
937 =head2 account_info()
938  
939 Return information about the user account. See L<http://browshot.com/api/documentation#account_info> for the response format.
940  
941 =cut
942  
943 0 ▶sub account_info {
Subroutine account_info: not called CC=1 SCAR=6.9
Pod documented
944 0 my ($self, %args) = @_;
945
946 0 return $self->return_reply(action => 'account/info', parameters => { %args });
947 }
948  
949  
950 # Private methods
951  
952 34 ▶sub return_binary {
Subroutine return_binary: called CC=9 SCAR=22.3
Pod undocumented
953 34 my ($self, %args) = @_;
954 34 my $url = $self->make_url(%args);
955 34 my $res;
956  
957 34 eval { $res = $self->{_ua}->get($url); };
958 34 ▶ return $self->error("Cannot retrieve $args{action}") if ($@ || ! defined($res));
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not defined $res 2 32 34
Condition66%2/3
$@ or not defined $res
l!l&&r!l&&!r
2032
MC/DC50%1/2
$@ or not defined $res
$@ not defined $res
Truth table66%2/3
A or not(B)
A= $@B= not defined $res
ABresult
1X1
011
000
959 32 ▶ return $self->error("Server sent back an error: " . $res->code) if (! $res->is_success);
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 8 24 32
960 24 ▶ return $self->error("Unexpected content type in $args{action}") if (($res->header('Content-Type') || '') !~ $args{content_type});
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->header("Content-Type") || "") =~ /$args{"content_type"}/ 8 16 24
Condition50%1/2
$res->header('Content-Type') || ''
l!l
240
MC/DC0%0/1
$res->header('Content-Type') || ''
$res->header('Content-Type')
Truth table50%1/2
A
A= $res->header('Content-Type')
Aresult
11
00
961  
962 16 my $content;
963 16 eval { $content = $res->decoded_content(charset => 'none'); };
964 16 ▶ return $self->error("No file retrieved from $args{action}") if ($@ || ! defined($content) || $content eq '');
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not defined $content or $content eq "" 2 14 16
Condition50%3/6
$@ or not defined $content or $content eq ""
l!l&&r!l&&!r
0214
$@ or not defined $content
l!l&&r!l&&!r
0016
MC/DC33%1/3
$@ or not defined $content or $content eq ""
$@ not defined $content $content eq ""
Truth table50%2/4
A or not(B) or C
A= $@B= not defined $contentC= $content eq ""
ABCresult
1XX1
01X1
0011
0000
965 14 return $content;
966 }
967  
968 18 ▶sub save_binary {
Subroutine save_binary: called CC=10 SCAR=23.4
Pod undocumented
969 18 my ($self, %args) = @_;
970 18 my %parameters = %{$args{parameters}};
971 18 my $method = $args{method};
972 18 my $file = delete $parameters{file};
973 18 ▶ return $self->error("Missing file in ${method}_file") if (! defined($file) || $file eq '');
Branch: 1 branch
BranchTrueFalseTotal
if not defined $file or $file eq "" 2 16 18
Condition66%2/3
not defined $file or $file eq ""
l!l&&r!l&&!r
2016
MC/DC50%1/2
not defined $file or $file eq ""
not defined $file $file eq ""
Truth table66%2/3
not(A) or B
A= not defined $fileB= $file eq ""
ABresult
1X1
011
000
974 16 ▶ return $self->error("You must specify a file path, not a folder, to save the file") if (-d $file);
Branch: 1 branch
BranchTrueFalseTotal
if -d $file 2 14 16
975  
976 14 my $content = $self->$method(%parameters);
977 14 ▶ return '' if ($content eq '');
Branch: 1 branch
BranchTrueFalseTotal
if $content eq "" 8 6 14
978 6 my $dir = dirname($file);
979 6 ▶ eval { make_path($dir) if (! -d $dir); };
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 6 0 6
980 6 ▶ return $self->error("Cannot create directory $dir") if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 4 6
981 4 ▶ open my $target, '>', $file or return $self->error("Cannot open $file for writing: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $target, ">", $file or return $self->error("Cannot open $file for writing: $!") 4 0 4
982 4 binmode $target;
983 4 my $written = print {$target} $content;
984 4 my $closed = close $target;
985 4 ▶ return $self->error("Cannot write $file: $!") if (! $written || ! $closed);
Branch: 1 branch
BranchTrueFalseTotal
unless $written and $closed 0 4 4
Condition33%1/3
$written and $closed
!ll&&!rl&&r
004
MC/DC0%0/2
$written and $closed
$written $closed
Truth table33%1/3
A and B
A= $writtenB= $closed
ABresult
0X0
100
111
986 4 return $file;
987 }
988  
989 6 ▶sub return_string {
Subroutine return_string: called CC=5 SCAR=17.1
Pod undocumented
990 6 my ($self, %args) = @_;
991  
992 6 my $url = $self->make_url(%args);
993
994 6 my $res;
995 6 my $try = 0;
996  
997 do {
998 6 $self->info("Try $try");
999 6 eval {
1000 6 $res = $self->{_ua}->get($url);
1001 };
1002 6 ▶ $self->error($@) if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
1003 6 $try++;
1004 }
1005 6 ▶ until($try < $self->{_retry} && defined $@);
Branch: 1 branch
BranchTrueFalseTotal
$try < $self->{"_retry"} and defined $@ or do { do { $self->info("Try $try"); eval { do { $res = $self->{"_ua"}->get($url) } }; $self->error($@) if $@; ++$try } } 6 0 6
Condition33%1/3
$try < $self->{"_retry"} and defined $@
!ll&&!rl&&r
006
MC/DC0%0/2
$try < $self->{"_retry"} and defined $@
$try < $self->{"_retry"} defined $@
Truth table33%1/3
A and B
A= $try < $self->{"_retry"}B= defined $@
ABresult
0X0
100
111
1006  
1007 6 ▶ if (! $res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 6 6
1008 0 $self->error("Server sent back an error: " . $res->code);
1009 }
1010
1011 6 return $res->decoded_content;
1012 }
1013  
1014 1 ▶sub return_post_string {
Subroutine return_post_string: called CC=7 SCAR=21.6
Pod undocumented
1015 1 my ($self, %args) = @_;
1016 1 ▶ my $file = $args{'file'} || '';
Condition50%1/2
$args{'file'} || ''
l!l
10
MC/DC0%0/1
$args{'file'} || ''
$args{'file'}
Truth table50%1/2
A
A= $args{'file'}
Aresult
11
00
1017  
1018 1 delete $args{'file'};
1019 1 my $url = $self->make_url(%args);
1020
1021 1 my $res;
1022 1 my $try = 0;
1023  
1024 do {
1025 1 $self->info("Try $try");
1026 1 eval {
1027 $res = $self->{_ua}->post(
1028 1 $url,
1029 Content_Type => 'form-data',
1030 Content => [
1031 file => [$file],
1032 ]
1033 );
1034 };
1035 1 ▶ $self->error($res->request->as_string) if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
1036 1 ▶ $self->error($@) if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
1037 1 $try++;
1038 }
1039 1 ▶ until($try < $self->{_retry} && defined $@);
Branch: 1 branch
BranchTrueFalseTotal
$try < $self->{"_retry"} and defined $@ or do { do { $self->info("Try $try"); eval { do { $res = $self->{"_ua"}->post($url, "Content_Type", "form-data", "Content", ["file", [$file]]) } }; $self->error($res->request->as_string) if $@; $self->error($@) if $@; ++$try } } 1 0 1
Condition33%1/3
$try < $self->{"_retry"} and defined $@
!ll&&!rl&&r
001
MC/DC0%0/2
$try < $self->{"_retry"} and defined $@
$try < $self->{"_retry"} defined $@
Truth table33%1/3
A and B
A= $try < $self->{"_retry"}B= defined $@
ABresult
0X0
100
111
1040  
1041 1 ▶ if (! $res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 1 1
1042 0 $self->error("Server sent back an error: " . $res->code);
1043 0 $self->info($res->request->as_string);
1044 0 $self->info($res->as_string);
1045 }
1046
1047 1 return $res->decoded_content;
1048 }
1049  
1050 6 ▶sub return_reply {
Subroutine return_reply: called CC=2 SCAR=7.3
Pod undocumented
1051 6 my ($self, %args) = @_;
1052  
1053 6 my $content = $self->return_string(%args);
1054  
1055 6 my $info;
1056 6 eval {
1057 6 $info = decode_json($content);
1058 };
1059 6 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 6 6
1060 0 $self->error("Invalid server response: " . $@);
1061 0 return $self->generic_error($@);
1062 }
1063  
1064 6 return $info;
1065 }
1066  
1067 1 ▶sub return_post_reply {
Subroutine return_post_reply: called CC=2 SCAR=7.3
Pod undocumented
1068 1 my ($self, %args) = @_;
1069  
1070 1 my $content = $self->return_post_string(%args);
1071  
1072 1 my $info;
1073 1 eval {
1074 1 $info = decode_json($content);
1075 };
1076 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
1077 0 $self->error("Invalid server response: " . $@);
1078 0 return $self->generic_error($@);
1079 }
1080  
1081 1 return $info;
1082 }
1083  
1084 41 ▶sub make_url {
Subroutine make_url: called CC=9 SCAR=22.1
Pod undocumented
1085 41 my ($self, %args) = @_;
1086 41 ▶ my $action = $args{action} || '';
Condition50%1/2
$args{'action'} || ''
l!l
410
MC/DC0%0/1
$args{'action'} || ''
$args{'action'}
Truth table50%1/2
A
A= $args{'action'}
Aresult
11
00
1087 41 ▶ my $parameters = $args{parameters} || { };
Condition50%1/2
$args{'parameters'} || {}
l!l
410
MC/DC0%0/1
$args{'parameters'} || {}
$args{'parameters'}
Truth table50%1/2
A
A= $args{'parameters'}
Aresult
11
00
1088  
1089 41 my $url = $self->{_base} . "$action?key=" . uri_encode($self->{_key}, 1);
1090  
1091  
1092 41 ▶ if (exists $parameters->{urls}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $parameters->{"urls"}) 1 40 41
1093 1 foreach my $uri (@{ $parameters->{urls} }) {
1094 1 $url .= '&url=' . uri_encode($uri, 1);
1095 }
1096 1 delete $parameters->{urls};
1097 }
1098  
1099 41 ▶ if (exists $parameters->{instances}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $parameters->{"instances"}) 1 40 41
1100 1 foreach my $instance_id (@{ $parameters->{instances} }) {
1101 1 $url .= '&instance_id=' . uri_encode($instance_id, 1);
1102 }
1103 1 delete $parameters->{instances};
1104 }
1105  
1106 41 foreach my $key (keys %$parameters) {
1107 127 ▶ $url .= '&' . uri_encode($key) . '=' . uri_encode($parameters->{$key}, 1) if (defined $parameters->{$key});
Branch: 1 branch
BranchTrueFalseTotal
if defined $parameters->{$key} 127 0 127
1108 }
1109  
1110 41 $self->info($url);
1111 41 return $url;
1112 }
1113  
1114 48 ▶sub info {
Subroutine info: called CC=2 SCAR=7.6
Pod undocumented
1115 48 my ($self, $message) = @_;
1116  
1117 48 ▶ if ($self->{_debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_debug"}) 0 48 48
1118 0 print $message, "\n";
1119 }
1120  
1121 48 return '';
1122 }
1123  
1124 28 ▶sub error {
Subroutine error: called CC=2 SCAR=7.4
Pod undocumented
1125 28 my ($self, $message) = @_;
1126  
1127 28 $self->{last_error} = $message;
1128  
1129 28 ▶ if ($self->{_debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_debug"}) 0 28 28
1130 0 print $message, "\n";
1131 }
1132  
1133 28 return '';
1134 }
1135  
1136 0 ▶sub generic_error {
Subroutine generic_error: not called CC=1 SCAR=6.9
Pod undocumented
1137 0 my ($self, $message) = @_;
1138  
1139  
1140 0 return { error => 1, message => $message };
1141 }
1142  
1143 =head1 CHANGES
1144  
1145 =over 4
1146  
1147 =item 1.32.0
1148  
1149 API 1.32. Add C<screenshot_pdf>, C<screenshot_pdf_file>, C<screenshot_video>,
1150 and C<screenshot_video_file>. Document new capture options and native instance IDs.
1151  
1152 =item 1.24.0
1153  
1154 C<screenshot_thumbnail_file> creates the directory structure as needed.
1155  
1156 =item 1.16.0
1157  
1158 Check if the file is not a folder in simple_file
1159  
1160 =item 1.14.1
1161  
1162 Remove deprecated API calls.
1163  
1164 =item 1.14.0
1165  
1166 Add C<batch_create> and C<batch_info> for API 1.14.
1167  
1168 =item 1.13.0
1169  
1170 Add C<screenshot_html> and C<screenshot_multiple> for API 1.13.
1171  
1172 =item 1.12
1173  
1174 Add C<screenshot_search> for API 1.12.
1175  
1176 =item 1.11.1
1177  
1178 Return Browshot response in case of error if the reply is valid JSON.
1179  
1180 =item 1.11
1181  
1182 Compatible with API 1.11. Optional HTTP timeout.
1183  
1184 =item 1.10
1185  
1186 Add C<screenshot_delete> for API 1.10.
1187  
1188 =item 1.9.4
1189  
1190 Fix status code in error messages.
1191  
1192 =item 1.9.3
1193  
1194 Keep backward compatibility for C<screenshot_thumbnail>.
1195  
1196 =item 1.9.0
1197  
1198 Add C<screenshot_share> for API 1.9.
1199  
1200 =item 1.8.0
1201  
1202 Update C<screenshot_thumbnail> to use new API.
1203  
1204 =item 1.7.0
1205  
1206 Update C<screenshot_info> to handle additional parameters
1207  
1208 =item 1.5.1
1209  
1210 Use binmode to create valid PNG files on Windows.
1211  
1212 =item 1.4.1
1213  
1214 Fix URI encoding.
1215  
1216 =item 1.4.0
1217  
1218 Add C<simple> and C<simple_file> methods.
1219  
1220 =item 1.3.1
1221  
1222 Retry requests (up to 2 times) to browshot.com in case of error
1223  
1224 =back
1225  
1226 =head1 SEE ALSO
1227  
1228 See L<http://browshot.com/api/documentation> for the API documentation.
1229  
1230 Create a free account at L<http://browshot.com/login> to get your free API key.
1231  
1232 Go to L<http://browshot.com/dashboard> to find your API key after you registered.
1233  
1234 =head1 AUTHOR
1235  
1236 Julien Sobrier, E<lt>julien@sobrier.netE<gt>
1237  
1238 =head1 COPYRIGHT AND LICENSE
1239  
1240 Copyright (C) 2015 by Julien Sobrier
1241  
1242 This library is free software; you can redistribute it and/or modify
1243 it under the same terms as Perl itself, either Perl version 5.8.8 or,
1244 at your option, any later version of Perl 5 you may have available.
1245  
1246  
1247 =cut
1248  
1249 3 1;
↑ Summary