line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::WsScreenshot; |
2
|
1
|
|
|
1
|
|
2869
|
use Moo; |
|
1
|
|
|
|
|
16635
|
|
|
1
|
|
|
|
|
5
|
|
3
|
1
|
|
|
1
|
|
2922
|
use URI::Encode qw( uri_encode ); |
|
1
|
|
|
|
|
18195
|
|
|
1
|
|
|
|
|
78
|
|
4
|
1
|
|
|
1
|
|
799
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
45182
|
|
|
1
|
|
|
|
|
44
|
|
5
|
1
|
|
|
1
|
|
7
|
use JSON::MaybeXS qw( decode_json ); |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
67
|
|
6
|
1
|
|
|
1
|
|
8
|
use URI; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
752
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.002'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
has base_url => ( |
11
|
|
|
|
|
|
|
is => 'rw', |
12
|
|
|
|
|
|
|
required => 1, |
13
|
|
|
|
|
|
|
); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has res_x => ( |
16
|
|
|
|
|
|
|
is => 'rw', |
17
|
|
|
|
|
|
|
default => sub { 1280 }, |
18
|
|
|
|
|
|
|
); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
has res_y => ( |
21
|
|
|
|
|
|
|
is => 'rw', |
22
|
|
|
|
|
|
|
default => sub { 900 }, |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
has out_format => ( |
26
|
|
|
|
|
|
|
is => 'rw', |
27
|
|
|
|
|
|
|
default => sub { 'jpg' }, |
28
|
|
|
|
|
|
|
); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
has is_full_page => ( |
31
|
|
|
|
|
|
|
is => 'rw', |
32
|
|
|
|
|
|
|
default => sub { 'false' }, |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
has wait_time => ( |
36
|
|
|
|
|
|
|
is => 'rw', |
37
|
|
|
|
|
|
|
default => sub { 100 }, |
38
|
|
|
|
|
|
|
); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
has ua => ( |
41
|
|
|
|
|
|
|
is => 'ro', |
42
|
|
|
|
|
|
|
lazy => 1, |
43
|
|
|
|
|
|
|
builder => sub { |
44
|
0
|
|
|
0
|
|
|
return LWP::UserAgent->new( timeout => 300, headers => [ 'Accept-Encoding' => '' ]); |
45
|
|
|
|
|
|
|
}, |
46
|
|
|
|
|
|
|
); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
has mech => ( |
49
|
|
|
|
|
|
|
is => 'ro', |
50
|
|
|
|
|
|
|
lazy => 1, |
51
|
|
|
|
|
|
|
builder => sub { |
52
|
0
|
|
|
0
|
|
|
my $mech = WWW::Mechanize->new( timeout => 300 ); |
53
|
0
|
|
|
|
|
|
$mech->add_header( 'Accept-Encoding' => '' ); |
54
|
0
|
|
|
|
|
|
return $mech; |
55
|
|
|
|
|
|
|
}, |
56
|
|
|
|
|
|
|
); |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub create_screenshot_url { |
59
|
0
|
|
|
0
|
1
|
|
my ( $self, @in ) = @_; |
60
|
|
|
|
|
|
|
|
61
|
0
|
0
|
|
|
|
|
my $args = ref $in[0] eq 'HASH' ? $in[0] : { @in }; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
die "Error: create_screenshot_url() requires a url argument.\n" unless |
64
|
0
|
0
|
|
|
|
|
$args->{url}; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
die "Error: create_screenshot_url() must be http(s)\n" |
67
|
0
|
0
|
|
|
|
|
unless URI->new($args->{url})->scheme =~ /^https?$/; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
return sprintf( "%s/api/screenshot?resX=%d&resY=%d&outFormat=%s&waitTime=%d&isFullPage=%s&url=%s", |
70
|
|
|
|
|
|
|
$self->base_url, |
71
|
|
|
|
|
|
|
exists $args->{res_x} ? $args->{res_x} : $self->res_x, |
72
|
|
|
|
|
|
|
exists $args->{res_y} ? $args->{res_y} : $self->res_y, |
73
|
|
|
|
|
|
|
exists $args->{out_format} ? $args->{out_format} : $self->out_format, |
74
|
|
|
|
|
|
|
exists $args->{wait_time} ? $args->{wait_time} : $self->wait_time, |
75
|
|
|
|
|
|
|
exists $args->{is_full_page} ? $args->{is_full_page} : $self->is_full_page, |
76
|
|
|
|
|
|
|
uri_encode($args->{url}) |
77
|
0
|
0
|
|
|
|
|
); |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub fetch_screenshot { |
81
|
0
|
|
|
0
|
1
|
|
my ( $self, @in ) = @_; |
82
|
|
|
|
|
|
|
|
83
|
0
|
0
|
|
|
|
|
my $args = ref $in[0] eq 'HASH' ? $in[0] : { @in }; |
84
|
|
|
|
|
|
|
|
85
|
0
|
|
|
|
|
|
my $res = $self->ua->get( $self->create_screenshot_url( $args ) ); |
86
|
|
|
|
|
|
|
|
87
|
0
|
0
|
|
|
|
|
if ( $res->content_type eq 'application/json' ) { |
88
|
0
|
|
|
|
|
|
die "Error: " . decode_json($res->decoded_content)->{details}; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
return $res; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub store_screenshot { |
95
|
0
|
|
|
0
|
1
|
|
my ( $self, @in ) = @_; |
96
|
|
|
|
|
|
|
|
97
|
0
|
0
|
|
|
|
|
my $args = ref $in[0] eq 'HASH' ? $in[0] : { @in }; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
die "Error: store_screenshot() requires an out_file argument.\n" unless |
100
|
0
|
0
|
|
|
|
|
$args->{out_file}; |
101
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
|
my $res = $self->fetch_screenshot( $args ); |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
open my $sf, ">", $args->{out_file} |
105
|
0
|
0
|
|
|
|
|
or die "Failed to open handle to " . $args->{out_file} . ": $!"; |
106
|
0
|
|
|
|
|
|
print $sf $res->decoded_content; |
107
|
0
|
|
|
|
|
|
close $sf; |
108
|
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
return $res; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
1; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=encoding utf8 |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 NAME |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
WebService::WsScreenshot - API client For ws-screenshot |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 DESCRIPTION |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
WebService::WsScreenshot is an api client for L. It |
123
|
|
|
|
|
|
|
makes it simple to get URLs, or download screenshots in a Perl application, using the backend |
124
|
|
|
|
|
|
|
provided by L. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head1 SYNOPSIS |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
#!/usr/bin/env perl |
130
|
|
|
|
|
|
|
use warnings; |
131
|
|
|
|
|
|
|
use strict; |
132
|
|
|
|
|
|
|
use WebService::WsScreenshot; |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
# Run the backend with.... |
135
|
|
|
|
|
|
|
# $ docker run --name ws-screenshot -d --restart always -p 3000:3000 -it elestio/ws-screenshot.slim |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
my $screenshot = WebService::WsScreenshot->new( |
138
|
|
|
|
|
|
|
base_url => 'http://127.0.0.1:3000', |
139
|
|
|
|
|
|
|
); |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
$screenshot->store_screenshot( |
142
|
|
|
|
|
|
|
url => 'https://modfoss.com/', |
143
|
|
|
|
|
|
|
out_file => 'modfoss.jpg', |
144
|
|
|
|
|
|
|
); |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head1 CONSTRUCTOR |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
The following options may be passed to the constructor. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head2 base_url |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
This is the URL that ws-screenshot is running at. It is required. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head2 res_x |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
The horizontal pixel size for the screenshot. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Default: 1280. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head2 res_y |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
The vertical pixel size for the screenshot. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Default: 900 |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head2 out_format |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
The output format. |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
Valid options are: jpg png pdf |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
Default: jpg |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head2 is_full_page |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
If the screenshot should include the full page |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
Valid options are: true false |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
Default: false |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=head2 wait_time |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
How long to wait before capuring the screenshot, in ms. |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
Default: 100 |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=head1 METHODS |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head2 create_screenshot_url |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
This method will return the full URL to the screen shot. It could be used |
194
|
|
|
|
|
|
|
for embedding the screenshot, for example. |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
You must pass C with the URL to be used for the screenshot. |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
my $img_url = $screenshot->create_screenshot_url( |
199
|
|
|
|
|
|
|
url => 'http://modfoss.com', |
200
|
|
|
|
|
|
|
); |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=head2 fetch_screenshot |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
This method will construct the URL for the screenshot, and then |
206
|
|
|
|
|
|
|
fetch the screenshot, making the API call to the ws-screenshot |
207
|
|
|
|
|
|
|
server. |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
It will return the HTTP::Response object from the API call. |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
If there is any error, it will die. |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
You must pass C with the URL to be used for the screenshot. |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
my $res = $screenshot->fetch_screenshot( |
216
|
|
|
|
|
|
|
url => 'http://modfoss.com', |
217
|
|
|
|
|
|
|
); |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=head2 store_screenshot |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
This method is the same as fetch_screenshot, however the screenshot |
222
|
|
|
|
|
|
|
itself will be written to disk. |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
You must pass C with the URL to be used for the screenshot, as |
225
|
|
|
|
|
|
|
well as C for the path the file is to be written to. |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
my $res = $screenshot->fetch_screenshot( |
228
|
|
|
|
|
|
|
url => 'http://modfoss.com', |
229
|
|
|
|
|
|
|
out_file => 'modfoss-screenshot.jpg', |
230
|
|
|
|
|
|
|
); |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=head1 AUTHOR |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
Kaitlyn Parkhurst (SymKat) Isymkat@symkat.comE> ( Blog: L ) |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=head1 COPYRIGHT |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
Copyright (c) 2021 the WebService::WsScreenshot L, L, and L as listed above. |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=head1 LICENSE |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
This library is free software and may be distributed under the same terms as perl itself. |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=head1 AVAILABILITY |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
The most current version of App::dec can be found at L |