| 1 | package TaskPipe::UserAgentManager::UserAgentHandler_PhantomJS; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | use Encode; | ||
| 5 | use WWW::Mechanize::PhantomJS; | ||
| 6 | use Module::Runtime 'require_module'; | ||
| 7 | use Log::Log4perl; | ||
| 8 | use Try::Tiny; | ||
| 9 | use Time::HiRes qw(usleep gettimeofday tv_interval); | ||
| 10 | use Web::Scraper; | ||
| 11 | use TaskPipe::PortManager; | ||
| 12 | |||
| 13 | with 'MooseX::ConfigCascade'; | ||
| 14 | extends 'TaskPipe::UserAgentManager::UserAgentHandler'; | ||
| 15 | |||
| 16 | # Note: WWW::Mechanize::PhantomJS, Selenium::Remote::Driver (which | ||
| 17 | # WWW::Mechanize::PhantomJS uses) and PhantomJS all have individual | ||
| 18 | # problems. 2 specific ones to be aware of before changing this module: | ||
| 19 | # | ||
| 20 | # - the 'content_encoding()' method in WWW::Mechanize::PhantomJS seems to return | ||
| 21 | # Content-Type not Content-Encoding | ||
| 22 | # - there appears to be a bug in PhantomJS (see here: | ||
| 23 | # https://github.com/ariya/phantomjs/issues/13621) which means "Accept-Encoding" | ||
| 24 | # needs to be set using a particular format of js, and thus the 'add_header' method | ||
| 25 | # in WWW::Mechanize::PhantomJS cannot be used. This module uses its own method | ||
| 26 | |||
| 27 | has phantom_settings => (is => 'rw', isa => __PACKAGE__.'::Settings', default => sub{ | ||
| 28 | my $module = __PACKAGE__.'::Settings'; | ||
| 29 | require_module( $module ); | ||
| 30 | $module->new; | ||
| 31 | }); | ||
| 32 | |||
| 33 | has port_manager => (is => 'ro', isa => 'TaskPipe::PortManager', lazy => 1, default => sub{ | ||
| 34 | my ($self) = @_; | ||
| 35 | |||
| 36 | TaskPipe::PortManager->new( | ||
| 37 | process_name => $self->phantom_settings->process_name, | ||
| 38 | base_port => $self->phantom_settings->base_port, | ||
| 39 | gm => $self->gm | ||
| 40 | ); | ||
| 41 | }); | ||
| 42 | |||
| 43 | |||
| 44 | has poll_for => (is => 'rw', isa => 'ArrayRef[Str]', default => sub{[]}); | ||
| 45 | |||
| 46 | sub build_ua{ | ||
| 47 | my ($self) = @_; | ||
| 48 | |||
| 49 | my $logger = Log::Log4perl->get_logger; | ||
| 50 | |||
| 51 | my $port = $self->port_manager->get_port; | ||
| 52 | my $ua = WWW::Mechanize::PhantomJS->new( | ||
| 53 | port => $port | ||
| 54 | ); | ||
| 55 | |||
| 56 | # WWW::Mechanize::PhantomJS does not seem to provide a method for | ||
| 57 | # returning the pid of the phantomjs instance. However, it's | ||
| 58 | # retrievable via $ua->{pid} (ugly, but.. unlikely to change?) | ||
| 59 | $logger->debug("Started PhantomJS process with pid ".$ua->{pid}." on port $port"); | ||
| 60 | |||
| 61 | $self->gm->table('spawned')->update_or_create({ | ||
| 62 | process_name => $self->phantom_settings->process_name, | ||
| 63 | thread_id => $self->run_info->thread_id, | ||
| 64 | job_id => $self->run_info->job_id, | ||
| 65 | used_by_pid => $$, | ||
| 66 | pid => $ua->{pid}, | ||
| 67 | port => $port | ||
| 68 | }); | ||
| 69 | |||
| 70 | my $timeout = $self->settings->timeout * 1000; | ||
| 71 | |||
| 72 | try { | ||
| 73 | $ua->eval_in_phantomjs(" | ||
| 74 | var page = this; | ||
| 75 | page.settings.userAgent = ${\$self->settings->agent}; | ||
| 76 | page.settings.resourceTimeout = $timeout; | ||
| 77 | "); | ||
| 78 | |||
| 79 | foreach my $header_name ( keys %{$self->settings->headers} ){ | ||
| 80 | |||
| 81 | my $header_val = $self->settings->headers->{$header_name}; | ||
| 82 | |||
| 83 | $ua->eval_in_phantomjs( qq| | ||
| 84 | var page = this; | ||
| 85 | page.customHeaders["$header_name"] = "$header_val"; | ||
| 86 | |); | ||
| 87 | }; | ||
| 88 | |||
| 89 | $ua->driver->debug_on if $self->phantom_settings->debug; | ||
| 90 | |||
| 91 | } catch { | ||
| 92 | |||
| 93 | confess "Error connecting to PhantomJS: ".$_; | ||
| 94 | |||
| 95 | }; | ||
| 96 | |||
| 97 | return $ua; | ||
| 98 | } | ||
| 99 | |||
| 100 | |||
| 101 | sub call{ | ||
| 102 | my ($self,$method,@params) = @_; | ||
| 103 | |||
| 104 | my $resp; | ||
| 105 | |||
| 106 | try { | ||
| 107 | |||
| 108 | if ( grep { $_ eq $method } @{$self->settings->request_methods} ){ | ||
| 109 | |||
| 110 | |||
| 111 | $resp = $self->request($method,@params); | ||
| 112 | |||
| 113 | |||
| 114 | } elsif ( $method eq 'proxy' ){ | ||
| 115 | |||
| 116 | $resp = $self->set_proxy(@params); | ||
| 117 | |||
| 118 | } elsif ( $method eq 'default_header' ){ | ||
| 119 | |||
| 120 | $resp = $self->default_header( @params ); | ||
| 121 | |||
| 122 | } elsif ( $method eq 'default_headers' ){ | ||
| 123 | |||
| 124 | $resp = $self->default_headers; | ||
| 125 | |||
| 126 | } else { | ||
| 127 | |||
| 128 | $resp = $self->SUPER::call( $method, @params ); | ||
| 129 | |||
| 130 | } | ||
| 131 | |||
| 132 | } catch { | ||
| 133 | |||
| 134 | confess "PhantomJS call failed: $_"; | ||
| 135 | |||
| 136 | }; | ||
| 137 | |||
| 138 | |||
| 139 | return $resp; | ||
| 140 | } | ||
| 141 | |||
| 142 | |||
| 143 | sub set_proxy{ | ||
| 144 | my ($self,$protocols,$url) = @_; | ||
| 145 | |||
| 146 | my $logger = Log::Log4perl->get_logger; | ||
| 147 | |||
| 148 | my ($scheme,$host,$port) = $url =~ m{^([^:]+)://([^:]+):([^:]+)$}; | ||
| 149 | |||
| 150 | $logger->debug("in set_proxy scheme=$scheme port=$port host=$host"); | ||
| 151 | |||
| 152 | my $type = $self->phantom_settings->proxy_schemes->{ $scheme }; | ||
| 153 | confess "attempt to set proxy with unrecognised scheme '$scheme'" unless $type; | ||
| 154 | |||
| 155 | try { | ||
| 156 | $self->ua->eval_in_phantomjs( qq| | ||
| 157 | var page = this; | ||
| 158 | phantom.setProxy("$host","$port","$type"); | ||
| 159 | |); | ||
| 160 | } catch { | ||
| 161 | "PhantomJS error setting proxy: $_"; | ||
| 162 | }; | ||
| 163 | } | ||
| 164 | |||
| 165 | |||
| 166 | sub default_header{ | ||
| 167 | my ($self,$header_name,$header_val) = @_; | ||
| 168 | |||
| 169 | confess "header_name is required" unless $header_name; | ||
| 170 | |||
| 171 | if ( $header_val ){ | ||
| 172 | |||
| 173 | $self->ua->eval_in_phantomjs( qq| | ||
| 174 | var page = this; | ||
| 175 | page.customHeaders["$header_name"] = "$header_val"; | ||
| 176 | |); | ||
| 177 | |||
| 178 | } else { | ||
| 179 | |||
| 180 | my $js = qq| | ||
| 181 | return this.customHeaders["$header_name"]; | ||
| 182 | |; | ||
| 183 | |||
| 184 | $header_val = $self->ua->eval_in_phantomjs( $js ); | ||
| 185 | |||
| 186 | } | ||
| 187 | |||
| 188 | return $header_val; | ||
| 189 | } | ||
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | sub default_headers{ | ||
| 194 | my ($self) = @_; | ||
| 195 | |||
| 196 | my $js = qq| | ||
| 197 | return this.customHeaders; | ||
| 198 | |; | ||
| 199 | |||
| 200 | return $self->ua->eval_in_phantomjs( $js ); | ||
| 201 | } | ||
| 202 | |||
| 203 | |||
| 204 | |||
| 205 | sub request{ | ||
| 206 | my ($self,$method,@params) = @_; | ||
| 207 | |||
| 208 | my @req_meths = @{$self->settings->request_methods}; | ||
| 209 | confess "invalid request method '$method'. Valid methods are: @req_meths" unless grep { $_ eq $method } @req_meths; | ||
| 210 | |||
| 211 | my $resp = $self->SUPER::call( $method, @params ); | ||
| 212 | $resp->remove_header( "Content-Encoding" ); | ||
| 213 | usleep ( $self->phantom_settings->page_load_wait_time * 1000 ); | ||
| 214 | |||
| 215 | my $content = $self->ua->driver->get_page_source; | ||
| 216 | |||
| 217 | my @poll_for = @{$self->poll_for}; | ||
| 218 | |||
| 219 | if ( @poll_for ){ | ||
| 220 | my $elapsed = 0; | ||
| 221 | my $t0 = [ gettimeofday ]; | ||
| 222 | while ( $elapsed < $self->phantom_settings->poll_for_timeout ){ | ||
| 223 | my $success = 1; | ||
| 224 | foreach my $poll_item (@poll_for){ | ||
| 225 | my $ws = scraper{ | ||
| 226 | process_first $poll_item, 'poll_item' => 'RAW'; | ||
| 227 | result 'poll_item'; | ||
| 228 | }; | ||
| 229 | my $poll_res = $ws->scrape($content); | ||
| 230 | if ( ! $poll_res ){ | ||
| 231 | $success = 0; | ||
| 232 | last; | ||
| 233 | } | ||
| 234 | } | ||
| 235 | print "elapsed $elapsed success $success\n"; | ||
| 236 | last if $success; | ||
| 237 | usleep( $self->phantom_settings->poll_for_interval ); | ||
| 238 | $elapsed = 1000 * tv_interval( $t0, [ gettimeofday ] ); | ||
| 239 | $content = $self->ua->driver->get_page_source; | ||
| 240 | } | ||
| 241 | } | ||
| 242 | |||
| 243 | $resp->content( encode("utf8",$content ) ); | ||
| 244 | |||
| 245 | return $resp; | ||
| 246 | } | ||
| 247 | |||
| 248 | |||
| 249 | sub clear_cookies{ | ||
| 250 | my ($self) = @_; | ||
| 251 | |||
| 252 | try { | ||
| 253 | $self->ua->eval_in_phantomjs( qq| phantom.clearCookies(); | ); | ||
| 254 | } catch { | ||
| 255 | confess "PhantomJS Clear cookies error: $_"; | ||
| 256 | }; | ||
| 257 | } | ||
| 258 | |||
| 259 | =head1 NAME | ||
| 260 | |||
| 261 | TaskPipe::UserAgentManager::UserAgentHandler_PhantomJS - useragent handler for phantomjs | ||
| 262 | |||
| 263 | =head1 DESCRIPTION | ||
| 264 | |||
| 265 | This is the useragent handler module for PhantomJS. You can tell L<TaskPipe::Task_Scrape> to use this module, by specifying | ||
| 266 | |||
| 267 | ua_handler_module: TaskPipe::UserAgentManager::UserAgentHandler_PhantomJS | ||
| 268 | |||
| 269 | in the L<TaskPipe::Task_Scrape::Settings> section of the project config | ||
| 270 | |||
| 271 | =head1 AUTHOR | ||
| 272 | |||
| 273 | Tom Gracey <tomgracey@gmail.com> | ||
| 274 | |||
| 275 | =head1 COPYRIGHT AND LICENSE | ||
| 276 | |||
| 277 | Copyright (c) Tom Gracey 2018 | ||
| 278 | |||
| 279 | TaskPipe is free software, licensed under | ||
| 280 | |||
| 281 | The GNU Public License Version 3 | ||
| 282 | |||
| 283 | =cut | ||
| 284 | |||
| 285 | __PACKAGE__->meta->make_immutable; | ||
| 286 | 1; |