TaskPipe/UserAgentManager_ProxyNet_Open.pm

filter: Statementstmt 47.0% 24 / 51 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 45.4% 5 / 11 Podpod 0.0% 0 / 5 totaltotal 37.1% 29 / 78 CC 5 SCAR 15.5
CC
5
Cov
40%
after_request
12.0
next_list_index
6.0
change_ip
6.0
CRAP 4.7
« TaskPipe/UserAgentManager_ProxyNet.pm ↑ Summary TaskPipe/InterpParam/MatchHandler_name.pm »
1 package TaskPipe::UserAgentManager_ProxyNet_Open;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Module::Runtime 'require_module';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use TaskPipe::OpenProxyManager;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 extends 'TaskPipe::UserAgentManager_ProxyNet';
9 1 with 'MooseX::ConfigCascade';
10  
11 1 has url => (is => 'rw', isa => 'Str');
12 1 has ips => (is => 'rw', isa => 'ArrayRef');
13 1 has current_proxy => (is => 'rw', isa => 'DBIx::Class::Core');
14  
15 has proxy_manager => (is => 'ro', isa => 'TaskPipe::OpenProxyManager', lazy => 1, default => sub{
16 0 ▶ TaskPipe::OpenProxyManager->new(
Subroutine __ANON__: not called CC=1 SCAR=6.9
17 gm => $_[0]->gm
18 )
19 1 });
20  
21  
22 1 has last_list_index => (is => 'rw', isa => 'Int', default => 0);
23  
24 sub next_list_index{
25 0 ▶ my ($self) = @_;
Subroutine next_list_index: not called CC=2 SCAR=17.9
Pod undocumented
26  
27 0 my $li = $self->last_list_index;
28 0 my $max_li = scalar(@{$self->proxy_manager->settings->ip_list_names}) - 1;
29 0 $li++;
30 0 ▶ $li = 0 if $li > $max_li;
Branch: 1 branch
BranchTrueFalseTotal
if $li > $max_li 0 0 0
31 }
32  
33  
34  
35 sub init{
36 0 ▶ my $self = shift;
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
37  
38 0 $self->change_ip;
39 }
40  
41  
42  
43  
44 sub change_ip{
45 0 ▶ my ($self) = @_;
Subroutine change_ip: not called CC=2 SCAR=17.9
Pod undocumented
46  
47 0 my $logger = Log::Log4perl->get_logger;
48  
49 0 ▶ if ( $self->current_proxy ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->current_proxy) 0 0 0
50 0 $self->current_proxy->update({
51 job_id => undef,
52 thread_id => undef
53 });
54 }
55  
56 0 my $proxy = $self->proxy_manager->next_available_proxy;
57  
58 0 my $proxy_uri = $self->proxy_manager->settings->proxy_scheme."://".$proxy->ip.':'.$proxy->port;
59  
60 0 $self->ua_handler->call('proxy', $self->proxy_manager->settings->protocols, $proxy_uri );
61 0 $self->current_proxy( $proxy );
62 0 $self->current_proxy->update({
63 job_id => $self->run_info->job_id,
64 thread_id => $self->run_info->thread_id
65 });
66  
67 0 $logger->debug("New Proxy $proxy_uri selected");
68 }
69  
70  
71  
72 sub before_request{
73 0 ▶ my ($self) = @_;
Subroutine before_request: not called CC=1 SCAR=6.9
Pod undocumented
74  
75 0 $self->current_proxy->update({
76 last_used_dt => DateTime->now
77 });
78 }
79  
80  
81 sub after_request{
82 0 ▶ my ($self,$resp) = @_;
Subroutine after_request: not called CC=3 SCAR=24.8
Pod undocumented
83  
84 0 my $logger = Log::Log4perl->get_logger;
85  
86 0 ▶ if (! $resp->is_success || ! $resp->decoded_content){
Branch: 1 branch
BranchTrueFalseTotal
if (not $resp->is_success && $resp->decoded_content) 0 0 0
Condition0%0/3
$resp->is_success && $resp->decoded_content
!ll&&!rl&&r
000
MC/DC0%0/2
$resp->is_success && $resp->decoded_content
$resp->is_success $resp->decoded_content
Truth table0%0/3
A && B
A= $resp->is_successB= $resp->decoded_content
ABresult
0X0
100
111
87  
88 0 $logger->debug("Changing IP - Response failed: ".$resp->status_line);
89 0 $self->current_proxy->update({
90 status => 'dud'
91 });
92 0 $self->change_ip;
93  
94 }
95 }
96  
97 =head1 NAME
98  
99 TaskPipe::UserAgentManager_ProxyNet_Open - useragent manager for making requests through an open proxy network
100  
101 =head1 DESCRIPTION
102  
103 This useragent manager uses TaskPipe's open proxy management system to make requests. You need to have run
104  
105 taskpipe fetch open proxies
106  
107 and also
108  
109 taskpipe test open proxies
110  
111 before using this package. See the help for those commands for further information.
112  
113 You can use this package with L<TaskPipe::Task_Scrape> by specifying it as the useragent manager to use in your project config.
114  
115 TaskPipe::Task_Scrape::Settings:
116 ua_mgr_module: TaskPipe::UserAgentManager_ProxyNet_Open
117 # ...
118  
119 =head1 SEE ALSO
120  
121 L<TaskPipe::UserAgentManager>
122 L<TaskPipe::UserAgentManager_ProxyNet>
123 L<TaskPipe::UserAgentManager::UserAgentHandler>
124  
125 =head1 AUTHOR
126  
127 Tom Gracey <tomgracey@gmail.com>
128  
129 =head1 COPYRIGHT AND LICENSE
130  
131 Copyright (c) Tom Gracey 2018
132  
133 TaskPipe is free software, licensed under
134  
135 The GNU Public License Version 3
136  
137 =cut
138  
139 1 __PACKAGE__->meta->make_immutable;
140 1 1;
141
142  
143  
144  
« TaskPipe/UserAgentManager_ProxyNet.pm ↑ Summary TaskPipe/InterpParam/MatchHandler_name.pm »