| 1 | package TaskPipe::Task_Scrape; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use DateTime; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Log::Log4perl; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Module::Runtime qw(require_module); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Cwd 'abs_path'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use File::Basename; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use TaskPipe::Task_Scrape::Settings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | extends 'TaskPipe::Task'; | |||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | with 'MooseX::ConfigCascade'; | |||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | has scrape_settings => (is => 'ro', isa => __PACKAGE__.'::Settings', default => sub{ | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | ▶ | my $module = __PACKAGE__.'::Settings'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | require_module( $module ); | |||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | $module->new; | |||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | }); | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | has ua_mgr => (is => 'rw', isa => 'TaskPipe::UserAgentManager', lazy => 1, default => sub{ | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | my $ua_handler_mod = $self->scrape_settings->ua_handler_module; | |||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | require_module( $ua_handler_mod ); | |||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | my $ua_handler = $ua_handler_mod->new( | |||||||||||||||||||||||||||||||||||||||||
| 30 | gm => $self->gm | ||||||||||||||||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | $ua_handler->poll_for( $self->poll_for ) if $self->can('poll_for') && $ua_handler->can('poll_for'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->can("poll_for") and $ua_handler->can("poll_for")
MC/DC0%0/2
$self->can("poll_for") and $ua_handler->can("poll_for")
$self->can("poll_for")
$ua_handler->can("poll_for")
Truth table0%0/3
A and B
A= $self->can("poll_for")B= $ua_handler->can("poll_for")
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | my $ua_mod = $self->scrape_settings->ua_mgr_module; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | require_module( $ua_mod ); | |||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | my $ua_mgr = $ua_mod->new( | |||||||||||||||||||||||||||||||||||||||||
| 37 | gm => $self->gm, | ||||||||||||||||||||||||||||||||||||||||||
| 38 | ua_handler => $ua_handler | ||||||||||||||||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | $ua_mgr->init; | |||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | return $ua_mgr; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | }); | |||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | has run_info => (is => 'rw', isa => 'TaskPipe::RunInfo', default => sub{ | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | ▶ | TaskPipe::RunInfo->new; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | }); | |||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 1 | has url => (is => 'rw', isa => 'Str'); | |||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | has page_content => (is => 'rw', isa => 'Str'); | |||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | has status => (is => 'rw', isa => 'Str'); | |||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | sub scrape{ | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
scrape: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | #$self->log_mem("Top of scrape "); | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | my $scraped = $self->ws->scrape( $self->page_content, $self->url ) || []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->ws->scrape($self->page_content, $self->url) || []
MC/DC0%0/1
$self->ws->scrape($self->page_content, $self->url) || []
$self->ws->scrape($self->page_content, $self->url)
Truth table0%0/2
A
A= $self->ws->scrape($self->page_content, $self->url)
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | #$self->log_mem("After ws scrape "); | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | $scraped = $self->post_process( $scraped ) if $self->can('post_process'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | #$self->log_mem("After post process "); | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | confess "->ws should return an array ref, but instead returned a ".ref( $scraped ) unless ref( $scraped ) =~ /array/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | return $scraped; | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | sub _verbose_fail{ | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | my ($self,$msg) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_verbose_fail: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | my $fail_msg = $self->run_info->as_string.": ".$msg." Inputs: ".Dumper( $self->input )."\nInput History: ".Dumper( $self->input_history ); | |||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | confess $fail_msg; | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | sub action{ | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
action: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | #$self->log_mem("Top of action "); | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | my $logger = Log::Log4perl->get_logger; | |||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | if ( ! $self->pinterp->{url} ){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | $self->_verbose_fail("url required but none provided") if $self->scrape_settings->require_url; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | return []; | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | $self->url( $self->pinterp->{url} ); | |||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | $logger->info("Getting ".$self->pinterp->{url}); | |||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if ( $self->pinterp->{page_content} ){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | $self->page_content( $self->pinterp->{page_content} ) | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | } else { | |||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | $self->request_page_content; | |||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | #$self->log_mem("After request page content "); | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | my $scraped = $self->scrape; | |||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | #$self->log_mem("After scrape "); | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | my $tries = 0; | |||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | while( $self->url && $self->retry_condition( $scraped ) && $tries < $self->scrape_settings->max_retries ){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$self->url and $self->retry_condition($scraped) and $tries < $self->scrape_settings->max_retries
$self->url and $self->retry_condition($scraped)
MC/DC0%0/3
$self->url and $self->retry_condition($scraped) and $tries < $self->scrape_settings->max_retries
$self->url
$self->retry_condition($scraped)
$tries < $self->scrape_settings->max_retries
Truth table0%0/4
A and B and C
A= $self->urlB= $self->retry_condition($scraped)C= $tries < $self->scrape_settings->max_retries
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | #$self->ua_mgr->refresh; | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | $self->request_page_content; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | $scraped = $self->scrape; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | $tries++; | |||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | $logger->debug("Retry $tries"); | |||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | $self->_verbose_fail("Fail condition persisted despite ".$self->scrape_settings->max_retries." retry attempts") if $self->fail_condition( $scraped ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | $self->ws( undef ) if defined $self->ws; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | $self->page_content(''); | |||||||||||||||||||||||||||||||||||||||||
| 124 | #$self->log_mem("End of sub "); | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | return $scraped; | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | sub retry_condition{ #override in child? | ||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | my ($self,$scraped) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
retry_condition: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | ▶ | return 1 unless $self->status =~ /^200/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | my $retry = 1; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | $retry = 0 if $scraped && @$scraped; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$scraped and @$scraped
MC/DC0%0/2
$scraped and @$scraped
$scraped
@$scraped
Truth table0%0/3
A and B
A= $scrapedB= @$scraped
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | return $retry; | |||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | sub fail_condition{ | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | my ($self,$scraped) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fail_condition: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | return 1 unless $self->status =~ /^200/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | my $fail = 1; | |||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | $fail = 0 if $scraped; # && @$scraped; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | return $fail; | |||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | sub request_page_content{ | ||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
request_page_content: not called CC=11 SCAR=48.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | #$self->log_mem("top of request_page_content sub "); | ||||||||||||||||||||||||||||||||||||||||||
| 150 | #$self->mu->record("request_page_content: at start") if $self->can('mu'); | ||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | my $logger = Log::Log4perl->get_logger; | |||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | my $headers = $self->pinterp->{headers} || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->pinterp->{'headers'} || {}
MC/DC0%0/1
$self->pinterp->{'headers'} || {}
$self->pinterp->{'headers'}
Truth table0%0/2
A
A= $self->pinterp->{'headers'}
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | if ( $self->scrape_settings->require_referer && ! $headers->{Referer} ){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->scrape_settings->require_referer and not $headers->{"Referer"}
MC/DC0%0/2
$self->scrape_settings->require_referer and not $headers->{"Referer"}
$self->scrape_settings->require_referer
not $headers->{"Referer"}
Truth table0%0/3
A and not(B)
A= $self->scrape_settings->require_refererB= not $headers->{"Referer"}
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | $self->_verbose_fail("Cannot make request: No Referer"); | |||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | #$self->mu->record("request_page_content: before set headers") if $self->can('mu'); | ||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | foreach my $header_name ( keys %$headers ){ | |||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | $self->ua_mgr->ua_handler->call('default_header',$header_name, $headers->{$header_name}); | |||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | #$self->log_mem("After default headers "); | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | #$self->mu->record("request_page_content: before get") if $self->can('mu'); | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | my $content; | |||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | my $resp = $self->ua_mgr->request( 'get', $self->url ); | |||||||||||||||||||||||||||||||||||||||||
| 171 | #$self->mu->record("request_page_content: after get") if $self->can('mu'); | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | #$self->log_mem("After request "); | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | ▶ | if ( ! $resp || ! $resp->is_success ){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$resp && $resp->is_success
MC/DC0%0/2
$resp && $resp->is_success
$resp
$resp->is_success
Truth table0%0/3
A && B
A= $respB= $resp->is_success
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | my $message = "Request to ".$self->url." failed. "; | |||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | ▶ | $message.="No response was returned" unless $resp; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | ▶ | $message.="Response was: ".$resp->status_line if $resp; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | $logger->warn( $message ); | |||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | $content = $resp->decoded_content; | |||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | #$self->log_mem("After test resp "); | ||||||||||||||||||||||||||||||||||||||||||
| 188 | #$self->mu->record("request_page_content: after is_success") if $self->can('mu'); | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | ▶ | if ( $content ){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | $self->page_content( $content ); | |||||||||||||||||||||||||||||||||||||||||
| 192 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | $self->page_content( '' ); | |||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | my $status = '400'; | |||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | ▶ | $status = $resp->status_line if $resp; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | $self->status( $status ); | |||||||||||||||||||||||||||||||||||||||||
| 199 | #$self->mu->record("request_page_contetn: at end") if $self->can('mu'); | ||||||||||||||||||||||||||||||||||||||||||
| 200 | #$self->log_mem("End of request_page_content sub "); | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | sub add_test_output{ | ||||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_test_output: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | my $output = "=== Page Content Follows ===\n\n"; | |||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | $output.= $self->page_content."\n\n"; | |||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | $output.= "=== End of Page Content ===\n"; | |||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | return $output; | |||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | TaskPipe::Task_Scrape - Base TaskPipe class for scraping a webpage | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | This is the standard building block for creating a webpage-scraping task. To do this inherit from L<Task::Scrape> using the following package format: | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | package TaskPipe::Task_Scrape_MyScraper; | ||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | use Moose; | ||||||||||||||||||||||||||||||||||||||||||
| 227 | use Web::Scraper; | ||||||||||||||||||||||||||||||||||||||||||
| 228 | extends 'TaskPipe::Task_Scrape'; | ||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | has test_pinterp => (is => 'ro', isa => 'ArrayRef[HashRef], default => sub{[ | ||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | { | ||||||||||||||||||||||||||||||||||||||||||
| 233 | url => 'https://www.example.com/some-test-url', | ||||||||||||||||||||||||||||||||||||||||||
| 234 | headers => { | ||||||||||||||||||||||||||||||||||||||||||
| 235 | Referer => 'https://www.example.com/some-referer-url' | ||||||||||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | ]}); | ||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | has ws => (is => 'ro', isa => 'Web::Scraper', default => sub{ | ||||||||||||||||||||||||||||||||||||||||||
| 243 | scraper { | ||||||||||||||||||||||||||||||||||||||||||
| 244 | process 'div.some-class', 'results' => 'TEXT'; | ||||||||||||||||||||||||||||||||||||||||||
| 245 | result 'results' | ||||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||
| 247 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | sub post_process { # may or may not be necessary, depending | ||||||||||||||||||||||||||||||||||||||||||
| 250 | # on what is returned by ws | ||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||
| 252 | my ($self,$results) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | # do something with the results returned from the web scraper | ||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | return $results; | ||||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | C<test_pinterp> allows you to specify test data which you can run the task against by typing | ||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | taskpipe test task --name=Scrape_MyScraper | ||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | at the command line. | ||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | It is assumed you want to use a L<Web::Scraper> to scrape your page. If this is the case, just define a C<ws> attribute as above. See the L<Web::Scraper> manpage for more information on how to define a L<Web::Scraper>. | ||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | Your task needs to return an arrayref of results (each result being a hashref). It's great if you can get C<ws> to return this directly. Sometimes it is not possible to persuade your L<Web::Scraper> to return results in this format. To make format corrections (remove records from the data etc) you can include a C<post_process> subroutine. C<post_process> receives the output from ws. Do what is needed, and make sure you return your C<results> arrayref at the end. | ||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | Tom Gracey <tomgracey@gmail.com> | ||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | Copyright (c) Tom Gracey 2018 | ||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | TaskPipe is free software, licensed under | ||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | The GNU Public License Version 3 | ||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||
| 284 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||