| 1 | package Net::Async::Crawl4AI; | ||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: IO::Async Crawl4AI client with an async strategy chain | ||||||||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use parent 'IO::Async::Notifier'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 10 | ▶ | use Time::HiRes (); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | 10 | ▶ | use URI (); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 11 | 10 | ▶ | use WWW::Crawl4AI (); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 12 | 10 | ▶ | use WWW::Crawl4AI::Request (); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 13 | 10 | ▶ | use WWW::Crawl4AI::Result (); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 14 | 10 | ▶ | use WWW::Crawl4AI::Error (); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 15 | 10 | ▶ | use Net::Async::HTTP (); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 16 | 10 | ▶ | use Future (); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 17 | 10 | ▶ | use Future::Utils qw( repeat fmap_void ); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||
| 19 | 10 | our $VERSION = '0.002'; | |||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | # Keys forwarded straight to the underlying WWW::Crawl4AI orchestrator. | ||||||||||||||||||||||||||||||||||
| 23 | 10 | my @WWW_KEYS = qw( | |||||||||||||||||||||||||||||||||
| 24 | base_url api_token cloakbrowser_url proxy_url callback | ||||||||||||||||||||||||||||||||||
| 25 | fallback timeout min_markdown client | ||||||||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||
| 28 | 40 | ▶ | sub _init { | ||||||||||||||||||||||||||||||||
|
Subroutine
_init: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||
| 29 | 40 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||||||||
| 30 | 40 | $self->SUPER::_init($args); | |||||||||||||||||||||||||||||||||
| 31 | 40 | ▶ | $self->{crawl4ai} = delete $args->{crawl4ai} if exists $args->{crawl4ai}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 32 | $self->{crawl4ai} ||= WWW::Crawl4AI->new( | ||||||||||||||||||||||||||||||||||
| 33 | 40 | ▶ | map { exists $args->{$_} ? ( $_ => delete $args->{$_} ) : () } @WWW_KEYS | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"crawl4ai"} ||= "WWW::Crawl4AI"->new(map({exists $args->{$_} ? ($_, delete $args->{$_}) : ();} @WWW_KEYS))
MC/DC0%0/2
$self->{"crawl4ai"} ||= "WWW::Crawl4AI"->new(map({exists $args->{$_} ? ($_, delete $args->{$_}) : ();} @WWW_KEYS))
$self->{"crawl4ai"}
"WWW::Crawl4AI"->new(map({exists $args->{$_} ? ($_, delete $args->{$_}) : ();} @WWW_KEYS))
Truth table66%2/3
A ||= B
A= $self->{"crawl4ai"}B= "WWW::Crawl4AI"->new(map({exists $args->{$_} ? ($_, delete $args->{$_}) : ();} @WWW_KEYS))
| |||||||||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||||||||
| 35 | 40 | ▶ | $self->{poll_interval} = exists $args->{poll_interval} ? delete $args->{poll_interval} : 2; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 36 | 40 | $self->{http} = delete $args->{http}; | |||||||||||||||||||||||||||||||||
| 37 | 40 | $self->{delay_sub} = delete $args->{delay_sub}; | |||||||||||||||||||||||||||||||||
| 38 | 40 | return; | |||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||
| 41 | 2 | ▶ | sub configure_unknown { | ||||||||||||||||||||||||||||||||
|
Subroutine
configure_unknown: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 42 | 2 | my ( $self, %args ) = @_; | |||||||||||||||||||||||||||||||||
| 43 | 2 | delete @args{ @WWW_KEYS, qw( crawl4ai poll_interval http delay_sub ) }; | |||||||||||||||||||||||||||||||||
| 44 | 2 | ▶ | return unless %args; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 45 | 0 | croak "Unknown configuration keys: " . join( ',', sort keys %args ); | |||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||
| 48 | 77 | ▶ | sub crawl4ai { $_[0]->{crawl4ai} } | ||||||||||||||||||||||||||||||||
|
Subroutine
crawl4ai: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||
| 51 | 134 | ▶ | sub client { $_[0]->{crawl4ai}->client } | ||||||||||||||||||||||||||||||||
|
Subroutine
client: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||
| 54 | 2 | ▶ | sub poll_interval { @_ > 1 ? ( $_[0]->{poll_interval} = $_[1] ) : $_[0]->{poll_interval} } | ||||||||||||||||||||||||||||||||
|
Subroutine
poll_interval: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||
| 57 | 2 | ▶ | sub available_backends { $_[0]->{crawl4ai}->available_backends } | ||||||||||||||||||||||||||||||||
|
Subroutine
available_backends: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||
| 60 | 63 | ▶ | sub http { | ||||||||||||||||||||||||||||||||
|
Subroutine
http: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 61 | 63 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||
| 62 | 63 | ▶ | return $self->{http} if $self->{http}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 63 | 27 | my $http = Net::Async::HTTP->new( | |||||||||||||||||||||||||||||||||
| 64 | user_agent => $self->client->user_agent_string, | ||||||||||||||||||||||||||||||||||
| 65 | max_connections_per_host => 4, | ||||||||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||||||||
| 67 | 27 | $self->add_child($http); | |||||||||||||||||||||||||||||||||
| 68 | 27 | return $self->{http} = $http; | |||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||
| 72 | 40 | ▶ | sub _add_to_loop { | ||||||||||||||||||||||||||||||||
|
Subroutine
_add_to_loop: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 73 | 40 | my ( $self, $loop ) = @_; | |||||||||||||||||||||||||||||||||
| 74 | 40 | $self->SUPER::_add_to_loop($loop); | |||||||||||||||||||||||||||||||||
| 75 | 40 | $self->http; # build + parent the Net::Async::HTTP child eagerly | |||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||
| 78 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 79 | # Request dispatch + retry (Future-based mirror of the client's policy) | ||||||||||||||||||||||||||||||||||
| 80 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||
| 82 | 11 | ▶ | sub _delay_future { | ||||||||||||||||||||||||||||||||
|
Subroutine
_delay_future: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||||||||
| 83 | 11 | my ( $self, $seconds ) = @_; | |||||||||||||||||||||||||||||||||
| 84 | 11 | ▶ | return $self->{delay_sub}->($seconds) if $self->{delay_sub}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 85 | 0 | return $self->loop->delay_future( after => $seconds ); | |||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||
| 88 | 13 | ▶ | sub do_request { | ||||||||||||||||||||||||||||||||
|
Subroutine
do_request: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 89 | 13 | my ( $self, $request, $backend ) = @_; | |||||||||||||||||||||||||||||||||
| 90 | 13 | ▶ | croak "do_request requires an HTTP::Request" unless $self->client->is_request($request); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 91 | 13 | return $self->_do_request_with_retry( $request, $backend, 1 ); | |||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||
| 95 | 10 | ▶ | sub _retry_delay { | ||||||||||||||||||||||||||||||||
|
Subroutine
_retry_delay: called CC=6 SCAR=18.6
| |||||||||||||||||||||||||||||||||||
| 96 | 10 | my ( $self, $attempt, $response ) = @_; | |||||||||||||||||||||||||||||||||
| 97 | 10 | my $backoff = $self->client->retry_backoff; | |||||||||||||||||||||||||||||||||
| 98 | 10 | ▶ | my $delay = $backoff->[ $attempt - 1 ] // $backoff->[-1] // 1; | ||||||||||||||||||||||||||||||||
|
Condition40%2/5
($$backoff[$attempt - 1] // $backoff->[-1]) // 1
$$backoff[$attempt - 1] // $backoff->[-1]
MC/DC0%0/2
($$backoff[$attempt - 1] // $backoff->[-1]) // 1
$$backoff[$attempt - 1]
$backoff->[-1]
Truth table33%1/3
A // B
A= $$backoff[$attempt - 1]B= $backoff->[-1]
| |||||||||||||||||||||||||||||||||||
| 99 | 10 | ▶ | if ( $response && ( my $ra = $response->header('Retry-After') ) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$response and my $ra = $response->header("Retry-After")
MC/DC100%2/2
$response and my $ra = $response->header("Retry-After")
$response
my $ra = $response->header("Retry-After")
Truth table100%3/3
A and B
A= $responseB= my $ra = $response->header("Retry-After")
| |||||||||||||||||||||||||||||||||||
| 100 | 1 | ▶ | $delay = $ra if $ra =~ /^\d+$/; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||
| 102 | 10 | return $delay; | |||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||
| 105 | 23 | ▶ | sub _do_request_with_retry { | ||||||||||||||||||||||||||||||||
|
Subroutine
_do_request_with_retry: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 106 | 23 | my ( $self, $request, $backend, $attempt ) = @_; | |||||||||||||||||||||||||||||||||
| 107 | 23 | my $client = $self->client; | |||||||||||||||||||||||||||||||||
| 108 | 23 | my $max = $client->max_attempts; | |||||||||||||||||||||||||||||||||
| 109 | return $self->http->do_request( | ||||||||||||||||||||||||||||||||||
| 110 | request => $request, | ||||||||||||||||||||||||||||||||||
| 111 | fail_on_error => 0, | ||||||||||||||||||||||||||||||||||
| 112 | max_redirects => 0, | ||||||||||||||||||||||||||||||||||
| 113 | timeout => $client->timeout, | ||||||||||||||||||||||||||||||||||
| 114 | )->then( | ||||||||||||||||||||||||||||||||||
| 115 | 14 | ▶ | sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||
| 116 | 14 | my ( $response ) = @_; | |||||||||||||||||||||||||||||||||
| 117 | 14 | ▶ | return Future->done($response) if $response->is_success; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 118 | 10 | my $code = $response->code; | |||||||||||||||||||||||||||||||||
| 119 | 10 | my $retryable = $client->_retry_status_set->{$code}; | |||||||||||||||||||||||||||||||||
| 120 | 10 | ▶ | if ( $retryable && $attempt < $max ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$retryable and $attempt < $max
MC/DC100%2/2
$retryable and $attempt < $max
$retryable
$attempt < $max
Truth table100%3/3
A and B
A= $retryableB= $attempt < $max
| |||||||||||||||||||||||||||||||||||
| 121 | 4 | my $delay = $self->_retry_delay( $attempt, $response ); | |||||||||||||||||||||||||||||||||
| 122 | 4 | ▶ | $client->on_retry->( $attempt, $delay, $response ) if $client->on_retry; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 123 | return $self->_delay_future($delay) | ||||||||||||||||||||||||||||||||||
| 124 | 4 | ▶ | ->then( sub { $self->_do_request_with_retry( $request, $backend, $attempt + 1 ) } ); | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||
| 126 | # Non-retryable / exhausted non-2xx: hand the response back so the | ||||||||||||||||||||||||||||||||||
| 127 | # endpoint's parser raises the proper api error (with status_code). | ||||||||||||||||||||||||||||||||||
| 128 | 6 | return Future->done($response); | |||||||||||||||||||||||||||||||||
| 129 | }, | ||||||||||||||||||||||||||||||||||
| 130 | 9 | ▶ | sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 131 | 9 | my ( $err ) = @_; # transport-level failure from Net::Async::HTTP | |||||||||||||||||||||||||||||||||
| 132 | 9 | ▶ | if ( $attempt < $max ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 133 | 6 | my $delay = $self->_retry_delay( $attempt, undef ); | |||||||||||||||||||||||||||||||||
| 134 | 6 | ▶ | $client->on_retry->( $attempt, $delay, $err ) if $client->on_retry; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 135 | return $self->_delay_future($delay) | ||||||||||||||||||||||||||||||||||
| 136 | 6 | ▶ | ->then( sub { $self->_do_request_with_retry( $request, $backend, $attempt + 1 ) } ); | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||
| 138 | 3 | return Future->fail( | |||||||||||||||||||||||||||||||||
| 139 | WWW::Crawl4AI::Error->new( | ||||||||||||||||||||||||||||||||||
| 140 | type => 'transport', | ||||||||||||||||||||||||||||||||||
| 141 | message => "Crawl4AI transport error: $err", | ||||||||||||||||||||||||||||||||||
| 142 | status_code => 0, | ||||||||||||||||||||||||||||||||||
| 143 | backend => $backend, | ||||||||||||||||||||||||||||||||||
| 144 | attempt => $attempt, | ||||||||||||||||||||||||||||||||||
| 145 | ), | ||||||||||||||||||||||||||||||||||
| 146 | 'crawl4ai', | ||||||||||||||||||||||||||||||||||
| 147 | ); | ||||||||||||||||||||||||||||||||||
| 148 | }, | ||||||||||||||||||||||||||||||||||
| 149 | 23 | ); | |||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||
| 152 | # Dispatch $http_req, then run $parse->($response). Parser exceptions become a | ||||||||||||||||||||||||||||||||||
| 153 | # failed Future carrying a WWW::Crawl4AI::Error. | ||||||||||||||||||||||||||||||||||
| 154 | 54 | ▶ | sub _parsed { | ||||||||||||||||||||||||||||||||
|
Subroutine
_parsed: called CC=1 SCAR=0.1
| |||||||||||||||||||||||||||||||||||
| 155 | 54 | my ( $self, $http_req, $parse, $backend ) = @_; | |||||||||||||||||||||||||||||||||
| 156 | 52 | ▶ | return $self->do_request( $http_req, $backend )->then( sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||||||||
| 157 | 52 | my ( $res ) = @_; | |||||||||||||||||||||||||||||||||
| 158 | 52 | my $out = eval { $parse->($res) }; | |||||||||||||||||||||||||||||||||
| 159 | 52 | ▶ | if ( my $e = $@ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 160 | 6 | ▶ | my $err = ( blessed($e) && $e->isa('WWW::Crawl4AI::Error') ) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($e) and $e->isa("WWW::Crawl4AI::Error")
MC/DC0%0/2
builtin::blessed($e) and $e->isa("WWW::Crawl4AI::Error")
builtin::blessed($e)
$e->isa("WWW::Crawl4AI::Error")
Truth table33%1/3
A and B
A= builtin::blessed($e)B= $e->isa("WWW::Crawl4AI::Error")
| |||||||||||||||||||||||||||||||||||
| 161 | ? $e | ||||||||||||||||||||||||||||||||||
| 162 | : WWW::Crawl4AI::Error->new( | ||||||||||||||||||||||||||||||||||
| 163 | type => 'api', message => "$e", response => $res, backend => $backend ); | ||||||||||||||||||||||||||||||||||
| 164 | 6 | return Future->fail( $err, 'crawl4ai' ); | |||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||
| 166 | 46 | return Future->done($out); | |||||||||||||||||||||||||||||||||
| 167 | 54 | } ); | |||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||
| 170 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 171 | # Low-level Future endpoints | ||||||||||||||||||||||||||||||||||
| 172 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||
| 174 | 36 | ▶ | sub crawl_once { | ||||||||||||||||||||||||||||||||
|
Subroutine
crawl_once: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 175 | 36 | my ( $self, $request, $backend ) = @_; | |||||||||||||||||||||||||||||||||
| 176 | 36 | my $client = $self->client; | |||||||||||||||||||||||||||||||||
| 177 | return $self->_parsed( | ||||||||||||||||||||||||||||||||||
| 178 | $client->crawl_request($request), | ||||||||||||||||||||||||||||||||||
| 179 | 36 | ▶ | sub { $client->parse_crawl_response( $_[0], $backend ) }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 180 | 36 | $backend, | |||||||||||||||||||||||||||||||||
| 181 | ); | ||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||
| 185 | 5 | ▶ | sub md { | ||||||||||||||||||||||||||||||||
|
Subroutine
md: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 186 | 5 | my ( $self, $url, %opts ) = @_; | |||||||||||||||||||||||||||||||||
| 187 | 5 | my $client = $self->client; | |||||||||||||||||||||||||||||||||
| 188 | 5 | ▶ | my $request = ( blessed($url) && $url->isa('WWW::Crawl4AI::Request') ) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($url) and $url->isa("WWW::Crawl4AI::Request")
MC/DC0%0/2
builtin::blessed($url) and $url->isa("WWW::Crawl4AI::Request")
builtin::blessed($url)
$url->isa("WWW::Crawl4AI::Request")
Truth table33%1/3
A and B
A= builtin::blessed($url)B= $url->isa("WWW::Crawl4AI::Request")
| |||||||||||||||||||||||||||||||||||
| 189 | ? $url | ||||||||||||||||||||||||||||||||||
| 190 | : WWW::Crawl4AI::Request->new( urls => $url, %opts ); | ||||||||||||||||||||||||||||||||||
| 191 | return $self->_parsed( | ||||||||||||||||||||||||||||||||||
| 192 | $client->md_request($request), | ||||||||||||||||||||||||||||||||||
| 193 | 3 | ▶ | sub { $client->parse_md_response( $_[0] ) }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 194 | 5 | ); | |||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||
| 198 | 2 | ▶ | sub job_submit { | ||||||||||||||||||||||||||||||||
|
Subroutine
job_submit: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 199 | 2 | my ( $self, $request ) = @_; | |||||||||||||||||||||||||||||||||
| 200 | 2 | my $client = $self->client; | |||||||||||||||||||||||||||||||||
| 201 | return $self->_parsed( | ||||||||||||||||||||||||||||||||||
| 202 | $client->job_submit_request($request), | ||||||||||||||||||||||||||||||||||
| 203 | 2 | ▶ | sub { $client->parse_job_submit_response( $_[0] ) }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 204 | 2 | ); | |||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||
| 208 | 4 | ▶ | sub job_status { | ||||||||||||||||||||||||||||||||
|
Subroutine
job_status: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 209 | 4 | my ( $self, $task_id ) = @_; | |||||||||||||||||||||||||||||||||
| 210 | 4 | my $client = $self->client; | |||||||||||||||||||||||||||||||||
| 211 | return $self->_parsed( | ||||||||||||||||||||||||||||||||||
| 212 | $client->job_status_request($task_id), | ||||||||||||||||||||||||||||||||||
| 213 | 4 | ▶ | sub { $client->parse_job_status_response( $_[0] ) }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 214 | 4 | ); | |||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||
| 218 | 7 | ▶ | sub health { | ||||||||||||||||||||||||||||||||
|
Subroutine
health: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 219 | 7 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||
| 220 | 7 | my $client = $self->client; | |||||||||||||||||||||||||||||||||
| 221 | return $self->do_request( $client->health_request )->then( | ||||||||||||||||||||||||||||||||||
| 222 | 6 | ▶ | sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 223 | 6 | my ( $res ) = @_; | |||||||||||||||||||||||||||||||||
| 224 | # The parser dies on a non-2xx (a 3xx included), as in the sync client. | ||||||||||||||||||||||||||||||||||
| 225 | 6 | ▶ | my $healthy = eval { $client->parse_health_response($res) ? 1 : 0 }; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 226 | 6 | ▶ | if ( my $err = $@ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 227 | 4 | $self->_warn_health_failure($err); | |||||||||||||||||||||||||||||||||
| 228 | 4 | return Future->done(0); | |||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||
| 230 | 2 | return Future->done($healthy); | |||||||||||||||||||||||||||||||||
| 231 | }, | ||||||||||||||||||||||||||||||||||
| 232 | 1 | ▶ | sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 233 | 1 | my ( $err ) = @_; | |||||||||||||||||||||||||||||||||
| 234 | 1 | $self->_warn_health_failure($err); | |||||||||||||||||||||||||||||||||
| 235 | 1 | return Future->done(0); | |||||||||||||||||||||||||||||||||
| 236 | }, | ||||||||||||||||||||||||||||||||||
| 237 | 7 | ); | |||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||
| 240 | # Same warning as WWW::Crawl4AI::Client::health: the reason on one line. | ||||||||||||||||||||||||||||||||||
| 241 | 8 | ▶ | sub _warn_health_failure { | ||||||||||||||||||||||||||||||||
|
Subroutine
_warn_health_failure: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||
| 242 | 8 | my ( $self, $err ) = @_; | |||||||||||||||||||||||||||||||||
| 243 | 8 | ▶ | my $message = blessed($err) && $err->isa('WWW::Crawl4AI::Error') ? $err->message : $err; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($err) and $err->isa("WWW::Crawl4AI::Error")
MC/DC50%1/2
builtin::blessed($err) and $err->isa("WWW::Crawl4AI::Error")
builtin::blessed($err)
$err->isa("WWW::Crawl4AI::Error")
Truth table66%2/3
A and B
A= builtin::blessed($err)B= $err->isa("WWW::Crawl4AI::Error")
| |||||||||||||||||||||||||||||||||||
| 244 | 8 | ▶ | $message = '' unless defined $message; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 245 | 8 | $message =~ s/\s*\n.*\z//s; | |||||||||||||||||||||||||||||||||
| 246 | 8 | $message =~ s/\s+\z//; | |||||||||||||||||||||||||||||||||
| 247 | 8 | ▶ | warn 'crawl4ai health check failed: ' . $message . "\n" if length $message; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 248 | 8 | return; | |||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||
| 252 | 2 | ▶ | sub screenshot { | ||||||||||||||||||||||||||||||||
|
Subroutine
screenshot: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 253 | 2 | my ( $self, $url, %opts ) = @_; | |||||||||||||||||||||||||||||||||
| 254 | 2 | my $client = $self->client; | |||||||||||||||||||||||||||||||||
| 255 | return $self->_parsed( | ||||||||||||||||||||||||||||||||||
| 256 | $client->screenshot_request( $url, %opts ), | ||||||||||||||||||||||||||||||||||
| 257 | 2 | ▶ | sub { $client->parse_screenshot_response( $_[0] ) }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 258 | 2 | ); | |||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||
| 262 | 1 | ▶ | sub pdf { | ||||||||||||||||||||||||||||||||
|
Subroutine
pdf: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 263 | 1 | my ( $self, $url, %opts ) = @_; | |||||||||||||||||||||||||||||||||
| 264 | 1 | my $client = $self->client; | |||||||||||||||||||||||||||||||||
| 265 | return $self->_parsed( | ||||||||||||||||||||||||||||||||||
| 266 | $client->pdf_request( $url, %opts ), | ||||||||||||||||||||||||||||||||||
| 267 | 1 | ▶ | sub { $client->parse_pdf_response( $_[0] ) }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 268 | 1 | ); | |||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||
| 271 | 1 | ▶ | sub html { | ||||||||||||||||||||||||||||||||
|
Subroutine
html: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 272 | 1 | my ( $self, $url ) = @_; | |||||||||||||||||||||||||||||||||
| 273 | 1 | my $client = $self->client; | |||||||||||||||||||||||||||||||||
| 274 | return $self->_parsed( | ||||||||||||||||||||||||||||||||||
| 275 | $client->html_request($url), | ||||||||||||||||||||||||||||||||||
| 276 | 1 | ▶ | sub { $client->parse_html_response( $_[0] ) }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 277 | 1 | ); | |||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||
| 280 | 1 | ▶ | sub execute_js { | ||||||||||||||||||||||||||||||||
|
Subroutine
execute_js: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 281 | 1 | my ( $self, $url, $scripts ) = @_; | |||||||||||||||||||||||||||||||||
| 282 | 1 | my $client = $self->client; | |||||||||||||||||||||||||||||||||
| 283 | return $self->_parsed( | ||||||||||||||||||||||||||||||||||
| 284 | $client->execute_js_request( $url, $scripts ), | ||||||||||||||||||||||||||||||||||
| 285 | 1 | ▶ | sub { $client->parse_execute_js_response( $_[0] ) }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 286 | 1 | ); | |||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||
| 289 | 1 | ▶ | sub llm { | ||||||||||||||||||||||||||||||||
|
Subroutine
llm: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 290 | 1 | my ( $self, $url, $query, %opts ) = @_; | |||||||||||||||||||||||||||||||||
| 291 | 1 | my $client = $self->client; | |||||||||||||||||||||||||||||||||
| 292 | return $self->_parsed( | ||||||||||||||||||||||||||||||||||
| 293 | $client->llm_request( $url, $query, %opts ), | ||||||||||||||||||||||||||||||||||
| 294 | 1 | ▶ | sub { $client->parse_llm_response( $_[0] ) }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 295 | 1 | ); | |||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||
| 298 | 1 | ▶ | sub token { | ||||||||||||||||||||||||||||||||
|
Subroutine
token: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 299 | 1 | my ( $self, $email, %opts ) = @_; | |||||||||||||||||||||||||||||||||
| 300 | 1 | my $client = $self->client; | |||||||||||||||||||||||||||||||||
| 301 | return $self->_parsed( | ||||||||||||||||||||||||||||||||||
| 302 | $client->token_request( $email, %opts ), | ||||||||||||||||||||||||||||||||||
| 303 | 1 | ▶ | sub { $client->parse_token_response( $_[0] ) }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 304 | 1 | ); | |||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||
| 307 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 308 | # Job flow helper | ||||||||||||||||||||||||||||||||||
| 309 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||
| 311 | 2 | ▶ | sub crawl_job_and_wait { | ||||||||||||||||||||||||||||||||
|
Subroutine
crawl_job_and_wait: called CC=3 SCAR=11.6
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 312 | 2 | my ( $self, $request, %opts ) = @_; | |||||||||||||||||||||||||||||||||
| 313 | 2 | my $interval = delete $opts{poll_interval}; | |||||||||||||||||||||||||||||||||
| 314 | 2 | ▶ | my $req = ( blessed($request) && $request->isa('WWW::Crawl4AI::Request') ) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($request) and $request->isa("WWW::Crawl4AI::Request")
MC/DC0%0/2
builtin::blessed($request) and $request->isa("WWW::Crawl4AI::Request")
builtin::blessed($request)
$request->isa("WWW::Crawl4AI::Request")
Truth table33%1/3
A and B
A= builtin::blessed($request)B= $request->isa("WWW::Crawl4AI::Request")
| |||||||||||||||||||||||||||||||||||
| 315 | ? $request | ||||||||||||||||||||||||||||||||||
| 316 | : WWW::Crawl4AI::Request->new( urls => $request, %opts ); | ||||||||||||||||||||||||||||||||||
| 317 | 2 | ▶ | return $self->job_submit($req)->then( sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 318 | 2 | my ( $job ) = @_; | |||||||||||||||||||||||||||||||||
| 319 | 2 | $self->_poll_job( $job->{task_id}, $interval ); | |||||||||||||||||||||||||||||||||
| 320 | 2 | } ); | |||||||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||
| 324 | 2 | ▶ | sub _poll_job { | ||||||||||||||||||||||||||||||||
|
Subroutine
_poll_job: called CC=4 SCAR=14.3
| |||||||||||||||||||||||||||||||||||
| 325 | 2 | my ( $self, $task_id, $interval ) = @_; | |||||||||||||||||||||||||||||||||
| 326 | 2 | ▶ | $interval = $self->poll_interval unless defined $interval; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 327 | 2 | ▶ | croak "not added to a loop yet" unless $self->loop || $self->{delay_sub}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->loop or $self->{"delay_sub"}
MC/DC0%0/2
$self->loop or $self->{"delay_sub"}
$self->loop
$self->{"delay_sub"}
Truth table33%1/3
A or B
A= $self->loopB= $self->{"delay_sub"}
| |||||||||||||||||||||||||||||||||||
| 328 | 3 | ▶ | return repeat { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 329 | 2 | ▶ | $self->job_status($task_id)->then( sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 330 | 2 | my ( $status ) = @_; | |||||||||||||||||||||||||||||||||
| 331 | 2 | ▶ | return Future->done($status) if ( $status->{status} // '' ) eq 'COMPLETED'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$status->{"status"} // ""
MC/DC0%0/1
$status->{"status"} // ""
$status->{"status"}
Truth table50%1/2
A
A= $status->{"status"}
| |||||||||||||||||||||||||||||||||||
| 332 | 1 | ▶ | return $self->_delay_future($interval)->then( sub { Future->done($status) } ); | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 333 | 3 | } ); | |||||||||||||||||||||||||||||||||
| 334 | 3 | ▶ | } until => sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||
| 335 | 3 | my ( $f ) = @_; | |||||||||||||||||||||||||||||||||
| 336 | 3 | ▶ | return 1 if $f->is_failed; # parser fails the Future on a FAILED job | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 337 | 2 | ▶ | return ( $f->get->{status} // '' ) eq 'COMPLETED'; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$f->get->{"status"} // ""
MC/DC0%0/1
$f->get->{"status"} // ""
$f->get->{"status"}
Truth table50%1/2
A
A= $f->get->{"status"}
| |||||||||||||||||||||||||||||||||||
| 338 | 2 | }; | |||||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||
| 341 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 342 | # Async strategy chain — the headline async API | ||||||||||||||||||||||||||||||||||
| 343 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||
| 345 | 41 | ▶ | sub _elapsed { sprintf( '%.3f', Time::HiRes::time() - $_[0] ) + 0 } | ||||||||||||||||||||||||||||||||
|
Subroutine
_elapsed: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||
| 347 | # Run a single strategy asynchronously, resolving to a normalized page (or | ||||||||||||||||||||||||||||||||||
| 348 | # undef). crawl4ai-backed strategies dispatch through Net::Async::HTTP; the | ||||||||||||||||||||||||||||||||||
| 349 | # external_callback strategy hands the URL to the user coderef, which may return | ||||||||||||||||||||||||||||||||||
| 350 | # a hashref or a Future of one. | ||||||||||||||||||||||||||||||||||
| 351 | 41 | ▶ | sub _run_strategy_future { | ||||||||||||||||||||||||||||||||
|
Subroutine
_run_strategy_future: called CC=7 SCAR=23.8
| |||||||||||||||||||||||||||||||||||
| 352 | 41 | my ( $self, $strategy, $url, %opts ) = @_; | |||||||||||||||||||||||||||||||||
| 353 | 41 | my $www = $self->crawl4ai; | |||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||
| 355 | 41 | ▶ | if ( $strategy->can('build_request') ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 356 | 40 | my $req = $strategy->build_request( $www, $url, %opts ); | |||||||||||||||||||||||||||||||||
| 357 | 40 | ▶ | return $self->crawl_once( $req, $strategy->name )->then( sub { Future->done( $_[0]->[0] ) } ); | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||
| 360 | 1 | ▶ | if ( $strategy->name eq 'external_callback' ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 361 | 1 | my $ret = $www->callback->( $url, %opts ); | |||||||||||||||||||||||||||||||||
| 362 | 1 | ▶ | my $normalize = sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=15.9
| |||||||||||||||||||||||||||||||||||
| 363 | 1 | my ( $page ) = @_; | |||||||||||||||||||||||||||||||||
| 364 | 1 | ▶ | return undef unless ref $page eq 'HASH'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 365 | 1 | ▶ | $page->{url} //= $url; | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
$page->{"url"} //= $url
MC/DC0%0/2
$page->{"url"} //= $url
$page->{"url"}
$url
Truth table33%1/3
A //= B
A= $page->{"url"}B= $url
| |||||||||||||||||||||||||||||||||||
| 366 | 1 | ▶ | $page->{final_url} //= $page->{url}; | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
$page->{"final_url"} //= $page->{"url"}
MC/DC0%0/2
$page->{"final_url"} //= $page->{"url"}
$page->{"final_url"}
$page->{"url"}
Truth table33%1/3
A //= B
A= $page->{"final_url"}B= $page->{"url"}
| |||||||||||||||||||||||||||||||||||
| 367 | 1 | return $page; | |||||||||||||||||||||||||||||||||
| 368 | 1 | }; | |||||||||||||||||||||||||||||||||
| 369 | 1 | ▶ | return $ret->then( sub { Future->done( $normalize->( $_[0] ) ) } ) | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 370 | 1 | ▶ | if blessed($ret) && $ret->isa('Future'); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($ret) and $ret->isa("Future")
MC/DC0%0/2
builtin::blessed($ret) and $ret->isa("Future")
builtin::blessed($ret)
$ret->isa("Future")
Truth table33%1/3
A and B
A= builtin::blessed($ret)B= $ret->isa("Future")
| |||||||||||||||||||||||||||||||||||
| 371 | 0 | return Future->done( $normalize->($ret) ); | |||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||
| 374 | # Any other custom strategy: run its sync crawl, allowing a Future return. | ||||||||||||||||||||||||||||||||||
| 375 | 0 | my $page = $strategy->crawl( $www, $url, %opts ); | |||||||||||||||||||||||||||||||||
| 376 | 0 | ▶ | return $page if blessed($page) && $page->isa('Future'); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
builtin::blessed($page) and $page->isa("Future")
MC/DC0%0/2
builtin::blessed($page) and $page->isa("Future")
builtin::blessed($page)
$page->isa("Future")
Truth table0%0/3
A and B
A= builtin::blessed($page)B= $page->isa("Future")
| |||||||||||||||||||||||||||||||||||
| 377 | 0 | return Future->done($page); | |||||||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||
| 380 | 29 | ▶ | sub crawl { | ||||||||||||||||||||||||||||||||
|
Subroutine
crawl: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 381 | 29 | my ( $self, @args ) = @_; | |||||||||||||||||||||||||||||||||
| 382 | 29 | my $www = $self->crawl4ai; | |||||||||||||||||||||||||||||||||
| 383 | 29 | my ( $url, %opts ) = $www->_normalize_args(@args); | |||||||||||||||||||||||||||||||||
| 384 | 29 | ▶ | croak "crawl needs a url" unless defined $url && length $url; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $url and length $url
MC/DC0%0/2
defined $url and length $url
defined $url
length $url
Truth table33%1/3
A and B
A= defined $urlB= length $url
| |||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||
| 386 | 29 | my $detect = $www->_detect_opts(%opts); | |||||||||||||||||||||||||||||||||
| 387 | 29 | my @strategies = @{ $www->strategies }; | |||||||||||||||||||||||||||||||||
| 388 | 29 | ▶ | return Future->done( $www->_failed_result( $url, [] ) ) unless @strategies; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||
| 390 | 29 | my @attempts; | |||||||||||||||||||||||||||||||||
| 391 | 29 | my $i = 0; | |||||||||||||||||||||||||||||||||
| 392 | return ( | ||||||||||||||||||||||||||||||||||
| 393 | 41 | ▶ | repeat { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 394 | 41 | my $strategy = $strategies[ $i++ ]; | |||||||||||||||||||||||||||||||||
| 395 | 41 | my $t0 = Time::HiRes::time(); | |||||||||||||||||||||||||||||||||
| 396 | $self->_run_strategy_future( $strategy, $url, %opts )->then( | ||||||||||||||||||||||||||||||||||
| 397 | 39 | ▶ | sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 398 | 39 | push @attempts, $www->_attempt_for( $strategy, $_[0], undef, _elapsed($t0), $detect ); | |||||||||||||||||||||||||||||||||
| 399 | 39 | Future->done( $attempts[-1] ); | |||||||||||||||||||||||||||||||||
| 400 | }, | ||||||||||||||||||||||||||||||||||
| 401 | 2 | ▶ | sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 402 | 2 | push @attempts, $www->_attempt_for( $strategy, undef, $_[0], _elapsed($t0), $detect ); | |||||||||||||||||||||||||||||||||
| 403 | 2 | Future->done( $attempts[-1] ); | |||||||||||||||||||||||||||||||||
| 404 | }, | ||||||||||||||||||||||||||||||||||
| 405 | 41 | ); | |||||||||||||||||||||||||||||||||
| 406 | 41 | ▶ | } while => sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 407 | 41 | my $attempt = $_[0]->get; | |||||||||||||||||||||||||||||||||
| 408 | 41 | ▶ | return 0 if $attempt->ok; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 409 | 14 | ▶ | return $i < @strategies ? 1 : 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||||||||
| 411 | 29 | ▶ | )->then( sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 412 | 29 | my ( $winner ) = grep { $_->ok } @attempts; | |||||||||||||||||||||||||||||||||
| 413 | 29 | ▶ | return Future->done( | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 414 | $winner | ||||||||||||||||||||||||||||||||||
| 415 | ? WWW::Crawl4AI::Result->from_attempt( $winner, attempts => \@attempts ) | ||||||||||||||||||||||||||||||||||
| 416 | : $www->_failed_result( $url, \@attempts ) | ||||||||||||||||||||||||||||||||||
| 417 | ); | ||||||||||||||||||||||||||||||||||
| 418 | 29 | } ); | |||||||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||
| 422 | 6 | ▶ | sub markdown { my ( $self, @args ) = @_; return $self->crawl(@args) } | ||||||||||||||||||||||||||||||||
|
Subroutine
markdown: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||
| 424 | 7 | ▶ | sub deep_crawl { | ||||||||||||||||||||||||||||||||
|
Subroutine
deep_crawl: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 425 | 7 | my ( $self, @args ) = @_; | |||||||||||||||||||||||||||||||||
| 426 | 7 | my $www = $self->crawl4ai; | |||||||||||||||||||||||||||||||||
| 427 | 7 | my ( $start, %opts ) = $www->_normalize_args(@args); | |||||||||||||||||||||||||||||||||
| 428 | 7 | ▶ | croak "deep_crawl needs a url" unless defined $start && length $start; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $start and length $start
MC/DC0%0/2
defined $start and length $start
defined $start
length $start
Truth table33%1/3
A and B
A= defined $startB= length $start
| |||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||
| 430 | 7 | ▶ | my $max_pages = exists $opts{max_pages} ? delete $opts{max_pages} : 25; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 431 | 7 | ▶ | my $max_depth = exists $opts{max_depth} ? delete $opts{max_depth} : 2; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 432 | 7 | ▶ | my $same_host = exists $opts{same_host} ? delete $opts{same_host} : 1; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 433 | 7 | ▶ | my $concurrency = exists $opts{concurrency} ? delete $opts{concurrency} : 4; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 434 | 7 | my $on_page = delete $opts{on_page}; | |||||||||||||||||||||||||||||||||
| 435 | 7 | my $url_filter = delete $opts{url_filter}; | |||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||
| 437 | 7 | my $start_host; # locked onto the host the start URL actually resolved to | |||||||||||||||||||||||||||||||||
| 438 | 7 | my %seen = ( $www->_canon_url($start) => 1 ); | |||||||||||||||||||||||||||||||||
| 439 | 7 | my $seq = 0; | |||||||||||||||||||||||||||||||||
| 440 | 7 | my %collected; # enqueue index => result, so the result list comes out in | |||||||||||||||||||||||||||||||||
| 441 | # breadth-first order regardless of which order a concurrent | ||||||||||||||||||||||||||||||||||
| 442 | # frontier happens to complete in | ||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||
| 444 | # Crawl one frontier level concurrently, gather the next level, recurse. | ||||||||||||||||||||||||||||||||||
| 445 | my $process_level; | ||||||||||||||||||||||||||||||||||
| 446 | 21 | ▶ | $process_level = sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||
| 447 | 21 | my ( $frontier ) = @_; | |||||||||||||||||||||||||||||||||
| 448 | 21 | my $remaining = $max_pages - keys %collected; | |||||||||||||||||||||||||||||||||
| 449 | 21 | ▶ | return Future->done unless @$frontier && $remaining > 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@$frontier and $remaining > 0
MC/DC100%2/2
@$frontier and $remaining > 0
@$frontier
$remaining > 0
Truth table100%3/3
A and B
A= @$frontierB= $remaining > 0
| |||||||||||||||||||||||||||||||||||
| 450 | # Trim to the budget up front so we never fire requests we'd only discard. | ||||||||||||||||||||||||||||||||||
| 451 | 14 | ▶ | $frontier = [ @{$frontier}[ 0 .. $remaining - 1 ] ] if @$frontier > $remaining; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||
| 453 | 14 | my @next; | |||||||||||||||||||||||||||||||||
| 454 | 20 | ▶ | my $level = fmap_void { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
| |||||||||||||||||||||||||||||||||||
| 455 | 20 | my ( $node ) = @_; | |||||||||||||||||||||||||||||||||
| 456 | 20 | ▶ | $self->crawl( $node->{url}, %opts )->then( sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=14 SCAR=27.2
| |||||||||||||||||||||||||||||||||||
| 457 | 20 | my ( $result ) = @_; | |||||||||||||||||||||||||||||||||
| 458 | 20 | $collected{ $node->{seq} } = $result; | |||||||||||||||||||||||||||||||||
| 459 | 20 | ▶ | $on_page->( $result, $node->{depth} ) if $on_page; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 460 | # Take the host from the start URL's actual final_url (depth 0), so a | ||||||||||||||||||||||||||||||||||
| 461 | # scheme-less start or a redirect to www. doesn't reject every link. | ||||||||||||||||||||||||||||||||||
| 462 | 20 | ▶ | $start_host //= lc( eval { URI->new( $result->final_url // $node->{url} )->host } // '' ); | ||||||||||||||||||||||||||||||||
|
Condition57%4/7
$start_host //= lc(eval {
do {
"URI"->new($result->final_url // $node->{"url"})->host
}
} // "")
eval {
do {
"URI"->new($result->final_url // $node->{"url"})->host
}
} // ""
$result->final_url // $node->{"url"}
MC/DC100%1/1
$start_host //= lc(eval {
do {
"URI"->new($result->final_url // $node->{"url"})->host
}
} // "")
$start_host
MC/DC0%0/2
$result->final_url // $node->{"url"}
$result->final_url
$node->{"url"}
Truth table100%2/2
A
A= $start_host
Truth table33%1/3
A // B
A= $result->final_urlB= $node->{"url"}
| |||||||||||||||||||||||||||||||||||
| 463 | 20 | ▶ | if ( $node->{depth} < $max_depth && $result->ok ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$node->{"depth"} < $max_depth and $result->ok
MC/DC50%1/2
$node->{"depth"} < $max_depth and $result->ok
$node->{"depth"} < $max_depth
$result->ok
Truth table66%2/3
A and B
A= $node->{"depth"} < $max_depthB= $result->ok
| |||||||||||||||||||||||||||||||||||
| 464 | 10 | for my $url ( @{ $result->urls } ) { | |||||||||||||||||||||||||||||||||
| 465 | 63 | my $canon = $www->_canon_url($url); | |||||||||||||||||||||||||||||||||
| 466 | 63 | ▶ | next if $seen{$canon}++; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 467 | 62 | ▶ | if ($same_host) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 468 | 61 | ▶ | my $host = lc( eval { URI->new($url)->host } // '' ); | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
eval {
do {
"URI"->new($url)->host
}
} // ""
MC/DC0%0/1
eval {
do {
"URI"->new($url)->host
}
} // ""
eval {
do {
"URI"->new($url)->host
}
}
Truth table50%1/2
A
A= eval {
do {
"URI"->new($url)->host
}
}
| |||||||||||||||||||||||||||||||||||
| 469 | 61 | ▶ | next unless $host eq $start_host; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 470 | } | ||||||||||||||||||||||||||||||||||
| 471 | 61 | ▶ | next if $url_filter && !$url_filter->($url); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$url_filter and not &$url_filter($url)
MC/DC0%0/2
$url_filter and not &$url_filter($url)
$url_filter
not &$url_filter($url)
Truth table33%1/3
A and not(B)
A= $url_filterB= not &$url_filter($url)
| |||||||||||||||||||||||||||||||||||
| 472 | 61 | push @next, { url => $url, depth => $node->{depth} + 1, seq => $seq++ }; | |||||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||||||||
| 475 | 20 | Future->done; | |||||||||||||||||||||||||||||||||
| 476 | 20 | } ); | |||||||||||||||||||||||||||||||||
| 477 | 14 | } foreach => [ @$frontier ], concurrent => $concurrency; | |||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||
| 479 | 14 | ▶ | return $level->then( sub { $process_level->( \@next ) } ); | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 480 | 7 | }; | |||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||
| 482 | my $f = $process_level->( [ { url => $start, depth => 0, seq => $seq++ } ] ) | ||||||||||||||||||||||||||||||||||
| 483 | 7 | ▶ | ->then( sub { Future->done( [ @collected{ sort { $a <=> $b } keys %collected } ] ) } ); | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 484 | 7 | ▶ | $f->on_ready( sub { undef $process_level } ); # break the self-referential cycle | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 485 | 7 | return $f; | |||||||||||||||||||||||||||||||||
| 486 | } | ||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||
| 490 | 10 | 1; | |||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||
| 492 | __END__ | ||||||||||||||||||||||||||||||||||