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