| 1 | package TaskPipe::UserAgentManager; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | with 'MooseX::ConfigCascade'; | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use URI::Escape; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Log::Log4perl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Carp qw(confess longmess); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use TaskPipe::UserAgentManager::CheckIPSettings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use TaskPipe::UserAgentManager::Settings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Module::Runtime 'require_module'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | has settings => (is => 'ro', isa => 'TaskPipe::UserAgentManager::Settings', lazy => 1, default => sub{ | ||||||||||||||||||||||||||||
| 18 | 0 | ▶ | TaskPipe::UserAgentManager::Settings->new; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 19 | 1 | }); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | has check_ip_settings => (is => 'ro', isa => 'TaskPipe::UserAgentManager::CheckIPSettings', default => sub{ | ||||||||||||||||||||||||||||
| 22 | 4 | ▶ | TaskPipe::UserAgentManager::CheckIPSettings->new; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 1 | }); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | has run_info => (is => 'rw', isa => 'TaskPipe::RunInfo', default => sub{ | ||||||||||||||||||||||||||||
| 27 | 4 | ▶ | TaskPipe::RunInfo->new; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 1 | }); | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | has ua_handler => (is => 'rw', isa => __PACKAGE__.'::UserAgentHandler', lazy => 1, default => sub{ | ||||||||||||||||||||||||||||
| 32 | 0 | ▶ | my $module = __PACKAGE__.'::UserAgentHandler'; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 33 | 0 | require_module( $module ); | |||||||||||||||||||||||||||
| 34 | 0 | my $handler = $module->new( | |||||||||||||||||||||||||||
| 35 | gm => $_[0]->gm | ||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||
| 37 | 0 | return $handler; | |||||||||||||||||||||||||||
| 38 | 1 | }); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 1 | has gm => (is => 'rw', isa => 'TaskPipe::SchemaManager'); | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | ▶ | sub init{} | |||||||||||||||||||||||||||
|
Subroutine
init: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | sub check_ip{ | ||||||||||||||||||||||||||||
| 46 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
check_ip: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | my $logger = Log::Log4perl->get_logger; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | my $resp; | |||||||||||||||||||||||||||
| 51 | my $ip; | ||||||||||||||||||||||||||||
| 52 | 0 | for my $i (1..$self->check_ip_settings->max_retries){ | |||||||||||||||||||||||||||
| 53 | 0 | ($ip,$resp) = $self->check_ip_attempt; | |||||||||||||||||||||||||||
| 54 | 0 | ▶ | last if $ip; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | $logger->debug("IP check #$i failed. Response: ".$resp->decoded_content ); | |||||||||||||||||||||||||||
| 56 | 0 | sleep($self->check_ip_settings->retry_delay); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | if ( $ip ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 0 | $logger->debug("IP checked and found to be: $ip" ); | |||||||||||||||||||||||||||
| 61 | } else{ | ||||||||||||||||||||||||||||
| 62 | 0 | $logger->warn("Could not get new IP details: ".$resp->status_line."\n"); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 0 | return $ip; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | sub check_ip_attempt{ | ||||||||||||||||||||||||||||
| 69 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
check_ip_attempt: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | my $logger = Log::Log4perl->get_logger; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | my $resp = $self->ua_handler->call('get',$self->check_ip_settings->url); | |||||||||||||||||||||||||||
| 74 | 0 | my $regex_text = $self->check_ip_settings->regex; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | my $regex = qr/$regex_text/; | |||||||||||||||||||||||||||
| 77 | 0 | my ($ip) = $resp->decoded_content =~ /$regex/s; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | return ($ip,$resp); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | sub delay{ | ||||||||||||||||||||||||||||
| 85 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
delay: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | my $logger = Log::Log4perl->get_logger; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | my $delay = 0; | |||||||||||||||||||||||||||
| 90 | 0 | $delay = $self->settings->delay_base + int( rand($self->settings->delay_max_rand) + 0.5 ); | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | if ($delay > 0){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | $logger->info("Sleeping for $delay secs..."); | |||||||||||||||||||||||||||
| 94 | 0 | sleep($delay); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | sub request{ | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | my ($self,$method,@params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
request: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | #$self->mu->record("request: at start"); | ||||||||||||||||||||||||||||
| 104 | 0 | my $logger = Log::Log4perl->get_logger; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | my @req_meths = @{$self->ua_handler->settings->request_methods}; | |||||||||||||||||||||||||||
| 107 | 0 | ▶ | confess "invalid request method '$method'. Valid methods are: @req_meths" unless grep { $_ eq $method } @req_meths; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | #$self->mu->record("request: after req meths"); | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | $self->delay if $self->settings->delay_base || $self->settings->delay_max_rand; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->settings->delay_base or $self->settings->delay_max_rand
MC/DC0%0/2
$self->settings->delay_base or $self->settings->delay_max_rand
$self->settings->delay_base
$self->settings->delay_max_rand
Truth table0%0/3
A or B
A= $self->settings->delay_baseB= $self->settings->delay_max_rand
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | my $resp; | |||||||||||||||||||||||||||
| 114 | #$self->mu->record("request: after delay"); | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | $self->before_request($method,@params); | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | #$self->mu->record("request: after before_request"); | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | try { | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | $resp = $self->ua_handler->call($method,@params); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | } catch { | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | $logger->warn("->request returned an error: $_\nStacktrace: ".longmess('')); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | }; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | #$self->mu->record("request: after call"); | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | $self->after_request($resp,$method,@params); | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | #$self->mu->record("request: at end"); | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | #$logger->debug("MEM: ".$self->mu->report); | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | return $resp; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | ▶ | sub before_request{} | |||||||||||||||||||||||||||
|
Subroutine
before_request: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 145 | ▶ | sub after_request{} #override? | |||||||||||||||||||||||||||
|
Subroutine
after_request: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | sub refresh{ | ||||||||||||||||||||||||||||
| 150 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
refresh: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | #$self->ua_handler->ua( $self->ua_handler->build_ua ); | ||||||||||||||||||||||||||||
| 153 | 0 | $self->ua_handler->clear_cookies; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | sub clear_cookies{ | ||||||||||||||||||||||||||||
| 158 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
clear_cookies: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 0 | $self->ua_handler->call( 'cookie_jar' => {} ); | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =head1 NAME | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | TaskPipe::UserAgentManager - base class for managing requests in TaskPipe | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | You can use this class with L<TaskPipe::Task_Scrape> by specifying it as the useragent manager to use in your project config. The useragent handler module specified in the C<ua_handler_module> setting in your config should provide the useragent handler L<TaskPipe::UserAgentManager> will use. E.g.: | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | TaskPipe::Task_Scrape::Settings: | ||||||||||||||||||||||||||||
| 173 | ua_mgr_module: TaskPipe::UserAgentManager | ||||||||||||||||||||||||||||
| 174 | ua_handler_module: TaskPipe::UserAgentManager::UserAgentHandler | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | # ... | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | If you want to modify the way TaskPipe makes requests you can inherit from L<TaskPipe::UserAgentManager> and create your own UserAgentManager. The format for your custom useragent manager might be as follows: | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | package TaskPipe::UserAgentManager_MyUAManager; | ||||||||||||||||||||||||||||
| 181 | use Moose; | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | sub init{ | ||||||||||||||||||||||||||||
| 184 | my ($self) = @_; | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | # do something on initialisation | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | sub before_request($self,method,@params){ | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | # method = 'get', 'post', ... | ||||||||||||||||||||||||||||
| 194 | # params = whatever params were passed when | ||||||||||||||||||||||||||||
| 195 | # the request was made (url etc) | ||||||||||||||||||||||||||||
| 196 | # | ||||||||||||||||||||||||||||
| 197 | # Do something directly before the request happens | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | sub after_request($self,$resp,$method,@params){ | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | # resp = the HTTP::Response object | ||||||||||||||||||||||||||||
| 205 | # | ||||||||||||||||||||||||||||
| 206 | # Do something directly after the request happens | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | __PACKAGE__->meta->make_immutable; | ||||||||||||||||||||||||||||
| 211 | 1; | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | If you are thinking of inheriting from TaskPipe::UserAgentManager, see the modules included in C<TaskPipe> already for exmples of how to do this: | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | L<TaskPipe::UserAgentManager_ProxyNet> | ||||||||||||||||||||||||||||
| 218 | L<TaskPipe::UserAgentManager_ProxyNet_TOR> | ||||||||||||||||||||||||||||
| 219 | L<TaskPipe::UserAgentManager_ProxyNet_Open> | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | See L<TaskPipe::UserAgentManager::UserAgentHandler> for more information about handlers. | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | Tom Gracey <tomgracey@gmail.com> | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | Copyright (c) Tom Gracey 2018 | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | TaskPipe is free software, licensed under | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | The GNU Public License Version 3 | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | =cut | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 1 | 1; | |||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||