Crawl4AI/Client.pm

filter: Statementstmt 61.6% 162 / 263 Branchbran 39.5% 49 / 124 Conditioncond 19.8% 28 / 141 MC/DCmcdc 5.2% 4 / 76 Subroutinesub 65.7% 46 / 70 Podpod 100% 35 / 35 totaltotal 45.7% 324 / 709 CC 115 SCAR 38.8
CC
115
Cov
45%
do_request
342.0
parse_job_status_response
110.0
_result_list
37.1
CRAP 48.6
↑ Summary Crawl4AI/Detect.pm »
1 package WWW::Crawl4AI::Client;
2 # ABSTRACT: UA-agnostic REST client for the Crawl4AI Docker API
3 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use URI::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use WWW::Crawl4AI::Markdown qw( resolve_markdown_chain );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use WWW::Crawl4AI::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use WWW::Crawl4AI::Error ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 our $VERSION = '0.001';
16  
17  
18 has base_url => (
19 is => 'ro',
20 0 ▶ default => sub { $ENV{CRAWL4AI_URL} || $ENV{CRAWL4AI_BASE_URL} || 'http://localhost:11235' },
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"CRAWL4AI_URL"} or $ENV{"CRAWL4AI_BASE_URL"} or "http://localhost:11235" 0 0 0
Condition0%0/3
$ENV{"CRAWL4AI_URL"} or $ENV{"CRAWL4AI_BASE_URL"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{"CRAWL4AI_URL"} or $ENV{"CRAWL4AI_BASE_URL"}
$ENV{"CRAWL4AI_URL"} $ENV{"CRAWL4AI_BASE_URL"}
Truth table0%0/3
A or B
A= $ENV{"CRAWL4AI_URL"}B= $ENV{"CRAWL4AI_BASE_URL"}
ABresult
1X1
011
000
21 4 );
22  
23  
24 has api_token => (
25 is => 'ro',
26 3 ▶ default => sub { $ENV{CRAWL4AI_API_TOKEN} },
Subroutine __ANON__: called CC=1 SCAR=0.0
27 4 );
28  
29  
30 has _json => (
31 is => 'lazy',
32 4 ▶ default => sub { JSON::MaybeXS->new( utf8 => 1, canonical => 1, convert_blessed => 1 ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 4 );
34  
35 has user_agent_string => (
36 is => 'ro',
37 4 ▶ default => sub { "WWW-Crawl4AI/$VERSION" },
Subroutine __ANON__: called CC=1 SCAR=0.0
38 4 );
39  
40  
41 4 ▶has timeout => ( is => 'ro', default => sub { 120 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
42  
43  
44 4 has ua => ( is => 'lazy' );
45  
46  
47 4 ▶has max_attempts => ( is => 'ro', default => sub { 3 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
48  
49  
50 4 ▶has retry_backoff => ( is => 'ro', default => sub { [ 1, 2, 4 ] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
51  
52  
53 4 ▶has retry_statuses => ( is => 'ro', default => sub { [ 429, 502, 503, 504 ] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
54  
55  
56 4 has on_retry => ( is => 'ro' );
57  
58  
59 has sleep_sub => (
60 is => 'ro',
61 default => sub {
62 4 ▶ require Time::HiRes;
Subroutine __ANON__: called CC=1 SCAR=0.4
63 4 ▶ return sub { Time::HiRes::sleep( $_[0] ) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
64 },
65 4 );
66  
67  
68 4 has _retry_status_set => ( is => 'lazy' );
69 sub _build__retry_status_set {
70 0 ▶ return { map { $_ => 1 } @{ $_[0]->retry_statuses } };
Subroutine _build__retry_status_set: not called CC=1 SCAR=6.9
71 }
72  
73 sub _build_ua {
74 0 ▶ my ( $self ) = @_;
Subroutine _build_ua: not called CC=1 SCAR=6.9
75 0 require LWP::UserAgent;
76 0 return LWP::UserAgent->new(
77 agent => $self->user_agent_string,
78 timeout => $self->timeout,
79 );
80 }
81  
82 0 ▶sub is_request { $_[1]->$_isa('HTTP::Request') }
Subroutine is_request: not called CC=1 SCAR=6.9
Pod documented
83  
84  
85 sub _uri {
86 15 ▶ my ( $self, $path ) = @_;
Subroutine _uri: called CC=1 SCAR=0.0
87 15 ( my $base = $self->base_url ) =~ s{/+$}{};
88 15 return URI->new( $base . $path );
89 }
90  
91 sub _request {
92 15 ▶ my ( $self, $method, $path, $body ) = @_;
Subroutine _request: called CC=3 SCAR=11.0
93 15 my $req = HTTP::Request->new( $method => $self->_uri($path) );
94 15 $req->header( Accept => 'application/json' );
95 15 ▶ $req->header( Authorization => 'Bearer ' . $self->api_token ) if defined $self->api_token;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->api_token 5 10 15
96 15 ▶ if ( defined $body ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body) 12 3 15
97 12 $req->header( 'Content-Type' => 'application/json' );
98 12 $req->content( $self->_json->encode($body) );
99 }
100 15 return $req;
101 }
102  
103 #----------------------------------------------------------------------
104 # Request builders (no I/O)
105 #----------------------------------------------------------------------
106  
107 sub crawl_request {
108 4 ▶ my ( $self, $request ) = @_;
Subroutine crawl_request: called CC=1 SCAR=0.0
Pod documented
109 4 return $self->_request( POST => '/crawl', $self->_payload( $request, 'to_crawl_payload' ) );
110 }
111  
112  
113 sub md_request {
114 0 ▶ my ( $self, $request ) = @_;
Subroutine md_request: not called CC=1 SCAR=6.9
Pod documented
115 0 return $self->_request( POST => '/md', $self->_payload( $request, 'to_md_payload' ) );
116 }
117  
118 sub job_submit_request {
119 1 ▶ my ( $self, $request ) = @_;
Subroutine job_submit_request: called CC=1 SCAR=0.0
Pod documented
120 1 return $self->_request( POST => '/crawl/job', $self->_payload( $request, 'to_crawl_payload' ) );
121 }
122  
123 sub job_status_request {
124 1 ▶ my ( $self, $task_id ) = @_;
Subroutine job_status_request: called CC=3 SCAR=12.5
Pod documented
125 1 ▶ croak "job_status_request needs a task_id" unless defined $task_id && length $task_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $task_id and length $task_id 0 1 1
Condition33%1/3
defined $task_id and length $task_id
!ll&&!rl&&r
001
MC/DC0%0/2
defined $task_id and length $task_id
defined $task_id length $task_id
Truth table33%1/3
A and B
A= defined $task_idB= length $task_id
ABresult
0X0
100
111
126 1 return $self->_request( GET => '/crawl/job/' . $task_id );
127 }
128  
129 1 ▶sub health_request { $_[0]->_request( GET => '/health' ) }
Subroutine health_request: called CC=1 SCAR=0.0
Pod documented
130  
131 sub screenshot_request {
132 2 ▶ my ( $self, $url, %opts ) = @_;
Subroutine screenshot_request: called CC=7 SCAR=20.0
Pod documented
133 2 ▶ croak "screenshot_request needs a url" unless defined $url && length $url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url and length $url 0 2 2
Condition33%1/3
defined $url and length $url
!ll&&!rl&&r
002
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
ABresult
0X0
100
111
134 2 my %body = ( url => $url );
135 2 ▶ $body{screenshot_wait_for} = $opts{wait_for} if defined $opts{wait_for};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"wait_for"} 1 1 2
136 2 ▶ $body{output_path} = $opts{output_path} if defined $opts{output_path};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"output_path"} 1 1 2
137 $body{wait_for_images} =
138 $opts{wait_for_images} ? WWW::Crawl4AI::Request::JSON_true() : WWW::Crawl4AI::Request::JSON_false()
139 2 ▶ if exists $opts{wait_for_images};
Branch: 2 branches
BranchTrueFalseTotal
if exists $opts{"wait_for_images"} 1 1 2
$opts{'wait_for_images'} ? : 1 0 1
140 2 return $self->_request( POST => '/screenshot', \%body );
141 }
142  
143  
144 sub pdf_request {
145 1 ▶ my ( $self, $url, %opts ) = @_;
Subroutine pdf_request: called CC=4 SCAR=15.2
Pod documented
146 1 ▶ croak "pdf_request needs a url" unless defined $url && length $url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url and length $url 0 1 1
Condition33%1/3
defined $url and length $url
!ll&&!rl&&r
001
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
ABresult
0X0
100
111
147 1 my %body = ( url => $url );
148 1 ▶ $body{output_path} = $opts{output_path} if defined $opts{output_path};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"output_path"} 1 0 1
149 1 return $self->_request( POST => '/pdf', \%body );
150 }
151  
152 sub html_request {
153 1 ▶ my ( $self, $url ) = @_;
Subroutine html_request: called CC=3 SCAR=12.5
Pod documented
154 1 ▶ croak "html_request needs a url" unless defined $url && length $url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url and length $url 0 1 1
Condition33%1/3
defined $url and length $url
!ll&&!rl&&r
001
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
ABresult
0X0
100
111
155 1 return $self->_request( POST => '/html', { url => $url } );
156 }
157  
158 sub execute_js_request {
159 4 ▶ my ( $self, $url, $scripts ) = @_;
Subroutine execute_js_request: called CC=6 SCAR=18.2
Pod documented
160 4 ▶ croak "execute_js_request needs a url" unless defined $url && length $url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url and length $url 0 4 4
Condition33%1/3
defined $url and length $url
!ll&&!rl&&r
004
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
ABresult
0X0
100
111
161 4 ▶ $scripts = [$scripts] unless ref $scripts eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $scripts eq "ARRAY" 2 2 4
162 4 ▶ croak "execute_js_request needs at least one script" unless @$scripts && defined $scripts->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless @$scripts and defined $scripts->[0] 2 2 4
Condition100%3/3
@$scripts and defined $scripts->[0]
!ll&&!rl&&r
112
MC/DC100%2/2
@$scripts and defined $scripts->[0]
@$scripts defined $scripts->[0]
Truth table100%3/3
A and B
A= @$scriptsB= defined $scripts->[0]
ABresult
0X0
100
111
163 2 return $self->_request( POST => '/execute_js', { url => $url, scripts => $scripts } );
164 }
165  
166 sub llm_request {
167 1 ▶ my ( $self, $url, $query, %opts ) = @_;
Subroutine llm_request: called CC=8 SCAR=25.2
Pod documented
168 1 ▶ croak "llm_request needs a url" unless defined $url && length $url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url and length $url 0 1 1
Condition33%1/3
defined $url and length $url
!ll&&!rl&&r
001
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
ABresult
0X0
100
111
169 1 ▶ croak "llm_request needs a query" unless defined $query && length $query;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $query and length $query 0 1 1
Condition33%1/3
defined $query and length $query
!ll&&!rl&&r
001
MC/DC0%0/2
defined $query and length $query
defined $query length $query
Truth table33%1/3
A and B
A= defined $queryB= length $query
ABresult
0X0
100
111
170 # The page URL is a path segment ({url:path}); escape it so its own scheme and
171 # query string don't merge into ours. The question and tuning go in the query.
172 1 my $path = URI->new( '/llm/' . URI::Escape::uri_escape_utf8($url) );
173 $path->query_form(
174 q => $query,
175 ( defined $opts{provider} ? ( provider => $opts{provider} ) : () ),
176 ( defined $opts{temperature} ? ( temperature => $opts{temperature} ) : () ),
177 1 ▶ ( defined $opts{base_url} ? ( base_url => $opts{base_url} ) : () ),
Branch: 3 branches
BranchTrueFalseTotal
defined $opts{'provider'} ? : 1 0 1
defined $opts{'temperature'} ? : 1 0 1
defined $opts{'base_url'} ? : 0 1 1
178 );
179 1 return $self->_request( GET => $path->as_string );
180 }
181  
182 sub token_request {
183 1 ▶ my ( $self, $email, %opts ) = @_;
Subroutine token_request: called CC=4 SCAR=15.2
Pod documented
184 1 ▶ croak "token_request needs an email" unless defined $email && length $email;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $email and length $email 0 1 1
Condition33%1/3
defined $email and length $email
!ll&&!rl&&r
001
MC/DC0%0/2
defined $email and length $email
defined $email length $email
Truth table33%1/3
A and B
A= defined $emailB= length $email
ABresult
0X0
100
111
185 1 my %body = ( email => $email );
186 1 ▶ $body{api_token} = $opts{api_token} if defined $opts{api_token};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"api_token"} 0 1 1
187 1 return $self->_request( POST => '/token', \%body );
188 }
189  
190 sub _payload {
191 5 ▶ my ( $self, $request, $method ) = @_;
Subroutine _payload: called CC=3 SCAR=11.4
192 5 ▶ return $request->$method if $request->$_isa('WWW::Crawl4AI::Request');
Branch: 1 branch
BranchTrueFalseTotal
if $request->$WWW::Crawl4AI::Client::_isa("WWW::Crawl4AI::Request") 4 1 5
193 1 ▶ return $request if ref $request eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $request eq "HASH" 1 0 1
194 0 croak "expected a WWW::Crawl4AI::Request or hashref payload";
195 }
196  
197 #----------------------------------------------------------------------
198 # Response parsers (no I/O)
199 #----------------------------------------------------------------------
200  
201 sub _decode {
202 9 ▶ my ( $self, $res, $backend ) = @_;
Subroutine _decode: called CC=6 SCAR=19.0
203 9 my $code = $res->code;
204 9 ▶ my $body = $res->decoded_content // $res->content // '';
Condition40%2/5
($res->decoded_content // $res->content) // ""
l!l
90
$res->decoded_content // $res->content
l!l&&r!l&&!r
900
MC/DC0%0/2
($res->decoded_content // $res->content) // ""
$res->decoded_content $res->content
Truth table33%1/3
A // B
A= $res->decoded_contentB= $res->content
ABresult
1X1
011
000
205 9 ▶ my $data = eval { length $body ? $self->_json->decode($body) : undef };
Branch: 1 branch
BranchTrueFalseTotal
length $body ? : 9 0 9
206 9 ▶ if ( !$res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 1 8 9
207 1 ▶ die WWW::Crawl4AI::Error->new(
Condition50%1/2
$res->message // "error"
l!l
10
MC/DC0%0/1
$res->message // "error"
$res->message
Truth table50%1/2
A
A= $res->message
Aresult
11
00
208 type => 'api',
209 message => "Crawl4AI HTTP $code: " . ( $res->message // 'error' ),
210 response => $res,
211 data => $data,
212 status_code => $code,
213 backend => $backend,
214 );
215 }
216 8 return $data;
217 }
218  
219 sub parse_crawl_response {
220 1 ▶ my ( $self, $res, $backend ) = @_;
Subroutine parse_crawl_response: called CC=1 SCAR=0.0
Pod documented
221 1 my $data = $self->_decode( $res, $backend );
222 1 return [ map { $self->_normalize_page($_) } @{ $self->_result_list($data) } ];
223 }
224  
225  
226 sub parse_md_response {
227 0 ▶ my ( $self, $res, $backend ) = @_;
Subroutine parse_md_response: not called CC=5 SCAR=34.0
Pod documented
228 0 my $data = $self->_decode( $res, $backend );
229 0 ▶ return $data if !ref $data;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data 0 0 0
230 # /md commonly returns { markdown => ... } or { result => ... }
231 0 ▶ return $data->{markdown} // $data->{result} // $data->{md} // $data;
Condition0%0/9
(($data->{"markdown"} // $data->{"result"}) // $data->{"md"}) // $data
l!l&&r!l&&!r
000
($data->{"markdown"} // $data->{"result"}) // $data->{"md"}
l!l&&r!l&&!r
000
$data->{"markdown"} // $data->{"result"}
l!l&&r!l&&!r
000
MC/DC0%0/4
(($data->{"markdown"} // $data->{"result"}) // $data->{"md"}) // $data
$data->{"markdown"} $data->{"result"} $data->{"md"} $data
Truth table0%0/5
A // B // C // D
A= $data->{"markdown"}B= $data->{"result"}C= $data->{"md"}D= $data
ABCDresult
1XXX1
01XX1
001X1
00011
00000
232 }
233  
234 sub parse_job_submit_response {
235 0 ▶ my ( $self, $res, $backend ) = @_;
Subroutine parse_job_submit_response: not called CC=4 SCAR=30.0
Pod documented
236 0 my $data = $self->_decode( $res, $backend );
237 0 ▶ my $id = $data->{task_id} // $data->{job_id} // $data->{id};
Condition0%0/6
($data->{"task_id"} // $data->{"job_id"}) // $data->{"id"}
l!l&&r!l&&!r
000
$data->{"task_id"} // $data->{"job_id"}
l!l&&r!l&&!r
000
MC/DC0%0/3
($data->{"task_id"} // $data->{"job_id"}) // $data->{"id"}
$data->{"task_id"} $data->{"job_id"} $data->{"id"}
Truth table0%0/4
A // B // C
A= $data->{"task_id"}B= $data->{"job_id"}C= $data->{"id"}
ABCresult
1XX1
01X1
0011
0000
238 0 ▶ croak "Crawl4AI job submit returned no task_id" unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
239 0 return { task_id => $id, raw => $data };
240 }
241  
242 sub parse_job_status_response {
243 0 ▶ my ( $self, $res, $backend ) = @_;
Subroutine parse_job_status_response: not called CC=10 SCAR=47.0
Pod documented
244 0 my $data = $self->_decode( $res, $backend );
245 0 ▶ my $status = uc( $data->{status} // $data->{state} // 'UNKNOWN' );
Condition0%0/5
($data->{"status"} // $data->{"state"}) // "UNKNOWN"
l!l
00
$data->{"status"} // $data->{"state"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($data->{"status"} // $data->{"state"}) // "UNKNOWN"
$data->{"status"} $data->{"state"}
Truth table0%0/3
A // B
A= $data->{"status"}B= $data->{"state"}
ABresult
1X1
011
000
246 0 my $pages;
247 0 ▶ if ( $status eq 'COMPLETED' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq "COMPLETED") 0 0 0
248 0 ▶ my $results = $data->{results} // $data->{result} // $data->{data};
Condition0%0/6
($data->{"results"} // $data->{"result"}) // $data->{"data"}
l!l&&r!l&&!r
000
$data->{"results"} // $data->{"result"}
l!l&&r!l&&!r
000
MC/DC0%0/3
($data->{"results"} // $data->{"result"}) // $data->{"data"}
$data->{"results"} $data->{"result"} $data->{"data"}
Truth table0%0/4
A // B // C
A= $data->{"results"}B= $data->{"result"}C= $data->{"data"}
ABCresult
1XX1
01X1
0011
0000
249 0 ▶ $pages = [ map { $self->_normalize_page($_) } @{ $self->_result_list($results) } ]
Branch: 1 branch
BranchTrueFalseTotal
if defined $results 0 0 0
250 if defined $results;
251 }
252 0 ▶ if ( $status eq 'FAILED' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq "FAILED") 0 0 0
253 die WWW::Crawl4AI::Error->new(
254 type => 'job',
255 0 ▶ message => "Crawl4AI job failed: " . ( $data->{error} // $data->{detail} // 'unknown' ),
Condition0%0/5
($data->{"error"} // $data->{"detail"}) // "unknown"
l!l
00
$data->{"error"} // $data->{"detail"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($data->{"error"} // $data->{"detail"}) // "unknown"
$data->{"error"} $data->{"detail"}
Truth table0%0/3
A // B
A= $data->{"error"}B= $data->{"detail"}
ABresult
1X1
011
000
256 data => $data,
257 backend => $backend,
258 );
259 }
260 0 return { status => $status, pages => $pages, raw => $data };
261 }
262  
263 sub parse_health_response {
264 0 ▶ my ( $self, $res ) = @_;
Subroutine parse_health_response: not called CC=4 SCAR=30.0
Pod documented
265 0 ▶ return 0 unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
266 0 ▶ my $data = eval { $self->_json->decode( $res->decoded_content // '' ) };
Condition0%0/2
$res->decoded_content // ""
l!l
00
MC/DC0%0/1
$res->decoded_content // ""
$res->decoded_content
Truth table0%0/2
A
A= $res->decoded_content
Aresult
11
00
267 0 ▶ return $data if ref $data;
Branch: 1 branch
BranchTrueFalseTotal
if ref $data 0 0 0
268 0 return 1;
269 }
270  
271 sub parse_screenshot_response {
272 2 ▶ my ( $self, $res, $backend ) = @_;
Subroutine parse_screenshot_response: called CC=1 SCAR=0.0
Pod documented
273 2 return $self->_decode_b64_artifact( $self->_decode( $res, $backend ), 'screenshot', $backend );
274 }
275  
276  
277 sub parse_pdf_response {
278 2 ▶ my ( $self, $res, $backend ) = @_;
Subroutine parse_pdf_response: called CC=1 SCAR=0.0
Pod documented
279 2 return $self->_decode_b64_artifact( $self->_decode( $res, $backend ), 'pdf', $backend );
280 }
281  
282 # /screenshot and /pdf return { success => bool, <key> => base64 }. Decode to raw
283 # bytes; raise a content error if the server reported nothing usable.
284 sub _decode_b64_artifact {
285 3 ▶ my ( $self, $data, $key, $backend ) = @_;
Subroutine _decode_b64_artifact: called CC=4 SCAR=14.0
286 3 ▶ my $b64 = ref $data eq 'HASH' ? $data->{$key} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'HASH' ? : 3 0 3
287 3 ▶ unless ( defined $b64 && length $b64 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $b64 and length $b64) 1 2 3
Condition66%2/3
defined $b64 and length $b64
!ll&&!rl&&r
102
MC/DC50%1/2
defined $b64 and length $b64
defined $b64 length $b64
Truth table66%2/3
A and B
A= defined $b64B= length $b64
ABresult
0X0
100
111
288 1 die WWW::Crawl4AI::Error->new(
289 type => 'content',
290 message => "Crawl4AI returned no $key",
291 data => $data,
292 backend => $backend,
293 );
294 }
295 2 return MIME::Base64::decode_base64($b64);
296 }
297  
298 sub parse_html_response {
299 1 ▶ my ( $self, $res, $backend ) = @_;
Subroutine parse_html_response: called CC=2 SCAR=7.1
Pod documented
300 1 my $data = $self->_decode( $res, $backend );
301 1 ▶ return ref $data eq 'HASH' ? $data->{html} : $data;
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'HASH' ? : 1 0 1
302 }
303  
304  
305 sub parse_execute_js_response {
306 1 ▶ my ( $self, $res, $backend ) = @_;
Subroutine parse_execute_js_response: called CC=3 SCAR=11.3
Pod documented
307 1 my $data = $self->_decode( $res, $backend );
308 # /execute_js returns a single crawl result (same shape as one /crawl page)
309 # with the script output added under js_execution_result.
310 1 ▶ my $page = $self->_normalize_page( ref $data eq 'HASH' ? $data : { raw => $data } );
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'HASH' ? : 1 0 1
311 1 ▶ $page->{js_result} = ref $data eq 'HASH' ? $data->{js_execution_result} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'HASH' ? : 1 0 1
312 1 return $page;
313 }
314  
315  
316 sub parse_llm_response {
317 1 ▶ my ( $self, $res, $backend ) = @_;
Subroutine parse_llm_response: called CC=4 SCAR=17.1
Pod documented
318 1 my $data = $self->_decode( $res, $backend );
319 1 ▶ return ref $data eq 'HASH' ? ( $data->{answer} // $data->{result} // $data ) : $data;
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'HASH' ? : 1 0 1
Condition33%2/6
($data->{"answer"} // $data->{"result"}) // $data
l!l&&r!l&&!r
100
$data->{"answer"} // $data->{"result"}
l!l&&r!l&&!r
100
MC/DC0%0/3
($data->{"answer"} // $data->{"result"}) // $data
$data->{"answer"} $data->{"result"} $data
Truth table25%1/4
A // B // C
A= $data->{"answer"}B= $data->{"result"}C= $data
ABCresult
1XX1
01X1
0011
0000
320 }
321  
322  
323 sub parse_token_response {
324 1 ▶ my ( $self, $res, $backend ) = @_;
Subroutine parse_token_response: called CC=1 SCAR=0.0
Pod documented
325 1 return $self->_decode( $res, $backend );
326 }
327  
328  
329 # Crawl4AI has returned the page list in several shapes across versions;
330 # accept all of them rather than pin to one.
331 sub _result_list {
332 1 ▶ my ( $self, $data ) = @_;
Subroutine _result_list: called CC=11 SCAR=36.1
333 1 ▶ return [] unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 1 1
334 1 ▶ return $data if ref $data eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $data eq "ARRAY" 0 1 1
335 1 ▶ if ( ref $data eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "HASH") 1 0 1
336 1 for my $key (qw( results data result )) {
337 1 ▶ my $v = $data->{$key} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $v = $data->{$key} or next 0 1 1
338 1 ▶ return $v if ref $v eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $v eq "ARRAY" 1 0 1
339 0 ▶ return [$v] if ref $v eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $v eq "HASH" 0 0 0
340 }
341 # Looks like a single page itself.
342 0 ▶ return [$data] if exists $data->{markdown} || exists $data->{html} || exists $data->{success};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{"markdown"} or exists $data->{"html"} or exists $data->{"success"} 0 0 0
Condition0%0/6
exists $data->{"markdown"} or exists $data->{"html"} or exists $data->{"success"}
l!l&&r!l&&!r
000
exists $data->{"markdown"} or exists $data->{"html"}
l!l&&r!l&&!r
000
MC/DC0%0/3
exists $data->{"markdown"} or exists $data->{"html"} or exists $data->{"success"}
exists $data->{"markdown"} exists $data->{"html"} exists $data->{"success"}
Truth table0%0/4
A or B or C
A= exists $data->{"markdown"}B= exists $data->{"html"}C= exists $data->{"success"}
ABCresult
1XX1
01X1
0011
0000
343 }
344 0 return [];
345 }
346  
347 sub _normalize_page {
348 2 ▶ my ( $self, $page ) = @_;
Subroutine _normalize_page: called CC=11 SCAR=33.7
349 2 ▶ return { raw => $page } unless ref $page eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $page eq "HASH" 0 2 2
350 2 ▶ my $meta = $page->{metadata} || {};
Condition50%1/2
$page->{'metadata'} || {}
l!l
02
MC/DC0%0/1
$page->{'metadata'} || {}
$page->{'metadata'}
Truth table50%1/2
A
A= $page->{'metadata'}
Aresult
11
00
351 return {
352 success => $page->{success},
353 url => $meta->{sourceURL} // $page->{url} // $meta->{url},
354 final_url => $page->{redirected_url} // $page->{url} // $meta->{url},
355 status_code => $page->{status_code} // $page->{status} // $meta->{statusCode},
356 markdown => $self->_extract_markdown($page),
357 html => $page->{cleaned_html} // $page->{html},
358 raw_html => $page->{html},
359 title => $meta->{title},
360 links => $self->_extract_links($page),
361 metadata => $meta,
362 error => $page->{error_message} // $page->{error},
363 2 ▶ response_headers => $self->_lc_headers( $page->{response_headers} ),
Condition37%9/24
($meta->{"sourceURL"} // $page->{"url"}) // $meta->{"url"}
l!l&&r!l&&!r
200
$meta->{"sourceURL"} // $page->{"url"}
l!l&&r!l&&!r
020
($page->{"redirected_url"} // $page->{"url"}) // $meta->{"url"}
l!l&&r!l&&!r
200
$page->{"redirected_url"} // $page->{"url"}
l!l&&r!l&&!r
020
($page->{"status_code"} // $page->{"status"}) // $meta->{"statusCode"}
l!l&&r!l&&!r
200
$page->{"status_code"} // $page->{"status"}
l!l&&r!l&&!r
200
$page->{"cleaned_html"} // $page->{"html"}
l!l&&r!l&&!r
011
$page->{"error_message"} // $page->{"error"}
l!l&&r!l&&!r
002
MC/DC0%0/3
($meta->{"sourceURL"} // $page->{"url"}) // $meta->{"url"}
$meta->{"sourceURL"} $page->{"url"} $meta->{"url"}
MC/DC0%0/3
($page->{"redirected_url"} // $page->{"url"}) // $meta->{"url"}
$page->{"redirected_url"} $page->{"url"} $meta->{"url"}
MC/DC0%0/3
($page->{"status_code"} // $page->{"status"}) // $meta->{"statusCode"}
$page->{"status_code"} $page->{"status"} $meta->{"statusCode"}
MC/DC50%1/2
$page->{"cleaned_html"} // $page->{"html"}
$page->{"cleaned_html"} $page->{"html"}
MC/DC0%0/2
$page->{"error_message"} // $page->{"error"}
$page->{"error_message"} $page->{"error"}
Truth table25%1/4
A // B // C
A= $meta->{"sourceURL"}B= $page->{"url"}C= $meta->{"url"}
ABCresult
1XX1
01X1
0011
0000
Truth table25%1/4
A // B // C
A= $page->{"redirected_url"}B= $page->{"url"}C= $meta->{"url"}
ABCresult
1XX1
01X1
0011
0000
Truth table25%1/4
A // B // C
A= $page->{"status_code"}B= $page->{"status"}C= $meta->{"statusCode"}
ABCresult
1XX1
01X1
0011
0000
Truth table66%2/3
A // B
A= $page->{"cleaned_html"}B= $page->{"html"}
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $page->{"error_message"}B= $page->{"error"}
ABresult
1X1
011
000
364 raw => $page,
365 };
366 }
367  
368 # Lowercase all header keys for deterministic, case-insensitive matching by callers.
369 sub _lc_headers {
370 2 ▶ my ( $self, $h ) = @_;
Subroutine _lc_headers: called CC=2 SCAR=9.2
371 2 ▶ return {} unless ref $h eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $h eq "HASH" 2 0 2
372 0 return { map { lc($_) => $h->{$_} } keys %$h };
373 }
374  
375 # Crawl4AI returns links as { internal => [...], external => [...] }, each entry
376 # a hash with href/text/title (older servers may send bare strings). Normalize
377 # to a stable { internal => [{href,text,title}], external => [...] } shape so the
378 # Result can expose them without callers reaching into raw.
379 sub _extract_links {
380 2 ▶ my ( $self, $page ) = @_;
Subroutine _extract_links: called CC=2 SCAR=7.6
381 2 my $links = $page->{links};
382 2 ▶ return { internal => [], external => [] } unless ref $links eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $links eq "HASH" 2 0 2
383 return {
384 internal => $self->_normalize_link_list( $links->{internal} ),
385 0 external => $self->_normalize_link_list( $links->{external} ),
386 };
387 }
388  
389 sub _normalize_link_list {
390 0 ▶ my ( $self, $list ) = @_;
Subroutine _normalize_link_list: not called CC=2 SCAR=17.9
391 0 ▶ return [] unless ref $list eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list eq "ARRAY" 0 0 0
392 0 return [ map { $self->_normalize_link($_) } @$list ];
393 }
394  
395 sub _normalize_link {
396 0 ▶ my ( $self, $link ) = @_;
Subroutine _normalize_link: not called CC=4 SCAR=30.0
397 0 ▶ return { href => $link } unless ref $link eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $link eq "HASH" 0 0 0
398 0 my $text = $link->{text};
399 0 ▶ $text = undef unless defined $text && length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and length $text 0 0 0
Condition0%0/3
defined $text and length $text
!ll&&!rl&&r
000
MC/DC0%0/2
defined $text and length $text
defined $text length $text
Truth table0%0/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
400 return {
401 href => $link->{href},
402 text => $text,
403 title => $link->{title},
404 0 };
405 }
406  
407 # markdown is a plain string on old servers and a structured object on new ones.
408 # fit_markdown is preferred but is frequently an empty string (no content
409 # filter matched), so skip empty candidates instead of stopping at the first
410 # defined one.
411 sub _extract_markdown {
412 2 ▶ my ( $self, $page ) = @_;
Subroutine _extract_markdown: called CC=1 SCAR=0.0
413 2 return resolve_markdown_chain( $page->{markdown} );
414 }
415  
416 #----------------------------------------------------------------------
417 # I/O + retry
418 #----------------------------------------------------------------------
419  
420 sub do_request {
421 0 ▶ my ( $self, $req, $backend ) = @_;
Subroutine do_request: not called CC=18 SCAR=58.3
Pod documented
422 0 ▶ croak "do_request needs an HTTP::Request" unless $self->is_request($req);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_request($req) 0 0 0
423 0 my $max = $self->max_attempts;
424 0 my $res;
425 0 for my $attempt ( 1 .. $max ) {
426 0 $res = $self->ua->request($req);
427 0 ▶ last if $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $res->is_success 0 0 0
428 0 my $code = $res->code;
429 0 ▶ my $transport = $code == 599 || ( ( $res->header('Client-Warning') // '' ) eq 'Internal response' );
Condition0%0/5
$code == 599 || ($res->header('Client-Warning') // '') eq 'Internal response'
l!l&&r!l&&!r
000
$res->header("Client-Warning") // ""
l!l
00
MC/DC0%0/2
$code == 599 || ($res->header('Client-Warning') // '') eq 'Internal response'
$code == 599 $res->header("Client-Warning")
Truth table0%0/3
A || B
A= $code == 599B= $res->header("Client-Warning")
ABresult
1X1
011
000
430 0 ▶ my $retryable = $transport || $self->_retry_status_set->{$code};
Condition0%0/3
$transport || $self->_retry_status_set->{$code}
l!l&&r!l&&!r
000
MC/DC0%0/2
$transport || $self->_retry_status_set->{$code}
$transport $self->_retry_status_set->{$code}
Truth table0%0/3
A || B
A= $transportB= $self->_retry_status_set->{$code}
ABresult
1X1
011
000
431 0 ▶ if ( $retryable && $attempt < $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retryable and $attempt < $max) 0 0 0
Condition0%0/3
$retryable and $attempt < $max
!ll&&!rl&&r
000
MC/DC0%0/2
$retryable and $attempt < $max
$retryable $attempt < $max
Truth table0%0/3
A and B
A= $retryableB= $attempt < $max
ABresult
0X0
100
111
432 0 ▶ my $delay = $self->retry_backoff->[ $attempt - 1 ] // $self->retry_backoff->[-1] // 1;
Condition0%0/5
($self->retry_backoff->[$attempt - 1] // $self->retry_backoff->[-1]) // 1
l!l
00
$self->retry_backoff->[$attempt - 1] // $self->retry_backoff->[-1]
l!l&&r!l&&!r
000
MC/DC0%0/2
($self->retry_backoff->[$attempt - 1] // $self->retry_backoff->[-1]) // 1
$self->retry_backoff->[$attempt - 1] $self->retry_backoff->[-1]
Truth table0%0/3
A // B
A= $self->retry_backoff->[$attempt - 1]B= $self->retry_backoff->[-1]
ABresult
1X1
011
000
433 0 ▶ if ( my $ra = $res->header('Retry-After') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ra = $res->header("Retry-After")) 0 0 0
434 0 ▶ $delay = $ra if $ra =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $ra =~ /^\d+$/ 0 0 0
435 }
436 0 ▶ $self->on_retry->( $attempt, $delay, $res ) if $self->on_retry;
Branch: 1 branch
BranchTrueFalseTotal
if $self->on_retry 0 0 0
437 0 $self->sleep_sub->($delay);
438 0 next;
439 }
440 0 last;
441 }
442 0 ▶ if ( !$res->is_success && ( $res->code == 599 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $res->is_success and $res->code == 599) 0 0 0
Condition0%0/3
not $res->is_success and $res->code == 599
!ll&&!rl&&r
000
MC/DC0%0/2
not $res->is_success and $res->code == 599
not $res->is_success $res->code == 599
Truth table0%0/3
not(A) and B
A= not $res->is_successB= $res->code == 599
ABresult
0X0
100
111
443 0 ▶ die WWW::Crawl4AI::Error->new(
Condition0%0/5
($res->content // $res->message) // "unreachable"
l!l
00
$res->content // $res->message
l!l&&r!l&&!r
000
MC/DC0%0/2
($res->content // $res->message) // "unreachable"
$res->content $res->message
Truth table0%0/3
A // B
A= $res->contentB= $res->message
ABresult
1X1
011
000
444 type => 'transport',
445 message => "Crawl4AI transport error: " . ( $res->content // $res->message // 'unreachable' ),
446 response => $res,
447 status_code => 0,
448 backend => $backend,
449 );
450 }
451 0 return $res;
452 }
453  
454  
455 #----------------------------------------------------------------------
456 # Convenience (build + fire + parse)
457 #----------------------------------------------------------------------
458  
459 sub crawl {
460 0 ▶ my ( $self, $request, $backend ) = @_;
Subroutine crawl: not called CC=1 SCAR=6.9
Pod documented
461 0 return $self->parse_crawl_response( $self->do_request( $self->crawl_request($request), $backend ), $backend );
462 }
463  
464  
465 sub md {
466 0 ▶ my ( $self, $url, %opts ) = @_;
Subroutine md: not called CC=2 SCAR=17.9
Pod documented
467 0 ▶ my $request = $url->$_isa('WWW::Crawl4AI::Request')
Branch: 1 branch
BranchTrueFalseTotal
$url->$WWW::Crawl4AI::Client::_isa('WWW::Crawl4AI::Request') ? : 0 0 0
468 ? $url
469 : WWW::Crawl4AI::Request->new( urls => $url, %opts );
470 0 return $self->parse_md_response( $self->do_request( $self->md_request($request) ) );
471 }
472  
473 sub job_submit {
474 0 ▶ my ( $self, $request ) = @_;
Subroutine job_submit: not called CC=1 SCAR=6.9
Pod documented
475 0 return $self->parse_job_submit_response( $self->do_request( $self->job_submit_request($request) ) );
476 }
477  
478 sub job_status {
479 0 ▶ my ( $self, $task_id ) = @_;
Subroutine job_status: not called CC=1 SCAR=6.9
Pod documented
480 0 return $self->parse_job_status_response( $self->do_request( $self->job_status_request($task_id) ) );
481 }
482  
483 sub health {
484 0 ▶ my ( $self ) = @_;
Subroutine health: not called CC=4 SCAR=30.0
Pod documented
485 0 my $res = eval { $self->do_request( $self->health_request ) };
486 0 ▶ return 0 if $@ || !$res;
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not $res 0 0 0
Condition0%0/3
$@ or not $res
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or not $res
$@ not $res
Truth table0%0/3
A or not(B)
A= $@B= not $res
ABresult
1X1
011
000
487 0 ▶ return $self->parse_health_response($res) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->parse_health_response($res) ? : 0 0 0
488 }
489  
490 sub screenshot {
491 0 ▶ my ( $self, $url, %opts ) = @_;
Subroutine screenshot: not called CC=1 SCAR=6.9
Pod documented
492 0 return $self->parse_screenshot_response( $self->do_request( $self->screenshot_request( $url, %opts ) ) );
493 }
494  
495  
496 sub pdf {
497 0 ▶ my ( $self, $url, %opts ) = @_;
Subroutine pdf: not called CC=1 SCAR=6.9
Pod documented
498 0 return $self->parse_pdf_response( $self->do_request( $self->pdf_request( $url, %opts ) ) );
499 }
500  
501 sub html {
502 0 ▶ my ( $self, $url ) = @_;
Subroutine html: not called CC=1 SCAR=6.9
Pod documented
503 0 return $self->parse_html_response( $self->do_request( $self->html_request($url) ) );
504 }
505  
506 sub execute_js {
507 0 ▶ my ( $self, $url, $scripts ) = @_;
Subroutine execute_js: not called CC=1 SCAR=6.9
Pod documented
508 0 return $self->parse_execute_js_response( $self->do_request( $self->execute_js_request( $url, $scripts ) ) );
509 }
510  
511 sub llm {
512 0 ▶ my ( $self, $url, $query, %opts ) = @_;
Subroutine llm: not called CC=1 SCAR=6.9
Pod documented
513 0 return $self->parse_llm_response( $self->do_request( $self->llm_request( $url, $query, %opts ) ) );
514 }
515  
516 sub token {
517 0 ▶ my ( $self, $email, %opts ) = @_;
Subroutine token: not called CC=1 SCAR=6.9
Pod documented
518 0 return $self->parse_token_response( $self->do_request( $self->token_request( $email, %opts ) ) );
519 }
520  
521  
522 4 1;
523  
524 __END__
↑ Summary Crawl4AI/Detect.pm »