Crawl4AI.pm

filter: Statementstmt 80.7% 122 / 151 Branchbran 61.5% 32 / 52 Conditioncond 42.8% 12 / 28 MC/DCmcdc 5.8% 1 / 17 Subroutinesub 75.0% 30 / 40 Podpod 100% 14 / 14 totaltotal 69.8% 211 / 302 CC 38 SCAR 16.6
CC
38
Cov
72%
detect
20.0
_normalize_args
11.1
_failed_result
8.3
CRAP 5.3
« Crawl4AI/Attempt.pm ↑ Summary Crawl4AI/Result.pm »
1 package WWW::Crawl4AI;
2 # ABSTRACT: Perl client and fallback orchestrator for Crawl4AI
3 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use WWW::Crawl4AI::Client ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use WWW::Crawl4AI::Result ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use WWW::Crawl4AI::Attempt ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use WWW::Crawl4AI::Error ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use WWW::Crawl4AI::Detect ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use WWW::Crawl4AI::StrategyChain ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use WWW::Crawl4AI::DeepCrawlIterator ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 our $VERSION = '0.005';
16  
17  
18 2 has strategy_chain => (
19 is => 'lazy',
20 );
21  
22  
23 sub _build_strategy_chain {
24 16 ▶ my ( $self ) = @_;
Subroutine _build_strategy_chain: called CC=1 SCAR=0.0
25 16 return WWW::Crawl4AI::StrategyChain->new;
26 }
27  
28 # Deprecated: kept for backward compat. Returns the applicable strategy list
29 # the same way the old _build_strategies did.
30 sub _strategies_for {
31 16 ▶ my ( $self ) = @_;
Subroutine _strategies_for: called CC=6 SCAR=18.1
32 16 my $fb = $self->fallback;
33 16 my @all = @{ $self->strategy_chain->strategies };
34  
35 16 ▶ if ( ref $fb eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fb eq "ARRAY") 1 15 16
36 1 my %by_name = map { $_->name => $_ } @all;
37 1 ▶ return [ grep { $_ && $_->applicable($self) } map { $by_name{$_} } @$fb ];
Branch: 1 branch
BranchTrueFalseTotal
$_ and $_->applicable($self) 3 0 3
38 }
39 15 ▶ if ( !$fb || $fb eq 'none' || $fb eq 'plain' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $fb or $fb eq "none" or $fb eq "plain") 9 6 15
Condition50%3/6
not $fb or $fb eq "none" or $fb eq "plain"
l!l&&r!l&&!r
096
not $fb or $fb eq "none"
l!l&&r!l&&!r
0015
MC/DC33%1/3
not $fb or $fb eq "none" or $fb eq "plain"
not $fb $fb eq "none" $fb eq "plain"
Truth table50%2/4
not(A) or B or C
A= not $fbB= $fb eq "none"C= $fb eq "plain"
ABCresult
1XX1
01X1
0011
0000
40 9 return [ grep { $_->name eq 'crawl4ai_plain' } @all ];
41 }
42 6 return $self->strategy_chain->applicable($self);
43 }
44  
45 2 has strategies => ( is => 'lazy' );
46  
47  
48 16 ▶sub _build_strategies { $_[0]->_strategies_for }
Subroutine _build_strategies: called CC=1 SCAR=0.0
49  
50 has base_url => (
51 is => 'ro',
52 16 ▶ default => sub { $ENV{CRAWL4AI_URL} || $ENV{CRAWL4AI_BASE_URL} || 'http://localhost:11235' },
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"CRAWL4AI_URL"} or $ENV{"CRAWL4AI_BASE_URL"} or "http://localhost:11235" 16 0 16
Condition33%1/3
$ENV{"CRAWL4AI_URL"} or $ENV{"CRAWL4AI_BASE_URL"}
l!l&&r!l&&!r
0016
MC/DC0%0/2
$ENV{"CRAWL4AI_URL"} or $ENV{"CRAWL4AI_BASE_URL"}
$ENV{"CRAWL4AI_URL"} $ENV{"CRAWL4AI_BASE_URL"}
Truth table33%1/3
A or B
A= $ENV{"CRAWL4AI_URL"}B= $ENV{"CRAWL4AI_BASE_URL"}
ABresult
1X1
011
000
53 2 );
54  
55  
56 has api_token => (
57 is => 'ro',
58 16 ▶ default => sub { $ENV{CRAWL4AI_API_TOKEN} },
Subroutine __ANON__: called CC=1 SCAR=0.0
59 2 );
60  
61  
62 has cloakbrowser_url => (
63 is => 'ro',
64 15 ▶ default => sub { $ENV{CLOAKBROWSER_CDP_URL} },
Subroutine __ANON__: called CC=1 SCAR=0.0
65 2 );
66  
67  
68 has proxy_url => (
69 is => 'ro',
70 15 ▶ default => sub { $ENV{CRAWL4AI_PROXY_URL} },
Subroutine __ANON__: called CC=1 SCAR=0.0
71 2 );
72  
73  
74 2 has callback => ( is => 'ro' );
75  
76  
77 # 'auto' (full applicable chain), 'plain'/'none' (Plain only), or an arrayref
78 # of backend names to run in that explicit order.
79 6 ▶has fallback => ( is => 'ro', default => sub { 'auto' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
80  
81  
82 16 ▶has timeout => ( is => 'ro', default => sub { 120 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
83  
84  
85 2 has min_markdown => ( is => 'ro' );
86  
87  
88 2 has client => ( is => 'lazy' );
89  
90  
91 sub _build_client {
92 0 ▶ my ( $self ) = @_;
Subroutine _build_client: not called CC=1 SCAR=6.9
93 0 return WWW::Crawl4AI::Client->new(
94 base_url => $self->base_url,
95 api_token => $self->api_token,
96 timeout => $self->timeout,
97 );
98 }
99  
100 sub _normalize_args {
101 32 ▶ my ( $self, @args ) = @_;
Subroutine _normalize_args: called CC=6 SCAR=24.0
102 32 ▶ return () unless @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 32 32
103 # a single hashref: { url => ..., %opts }
104 32 ▶ if ( @args == 1 && ref $args[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1 and ref $args[0] eq "HASH") 0 32 32
Condition66%2/3
@args == 1 and ref $args[0] eq "HASH"
!ll&&!rl&&r
8240
MC/DC0%0/2
@args == 1 and ref $args[0] eq "HASH"
@args == 1 ref $args[0] eq "HASH"
Truth table66%2/3
A and B
A= @args == 1B= ref $args[0] eq "HASH"
ABresult
0X0
100
111
105 0 my %a = %{ $args[0] };
106 0 my $url = delete $a{url};
107 0 return ( $url, %a );
108 }
109 # a leading positional URL, optionally followed by named options
110 32 ▶ if ( @args % 2 == 1 && !ref $args[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args % 2 == 1 and not ref $args[0]) 32 0 32
Condition33%1/3
@args % 2 == 1 and not ref $args[0]
!ll&&!rl&&r
0032
MC/DC0%0/2
@args % 2 == 1 and not ref $args[0]
@args % 2 == 1 not ref $args[0]
Truth table33%1/3
A and not(B)
A= @args % 2 == 1B= not ref $args[0]
ABresult
0X0
100
111
111 32 my ( $url, %a ) = @args;
112 32 return ( $url, %a );
113 }
114 # all named: ( url => ..., %opts )
115 0 my %a = @args;
116 0 my $url = delete $a{url};
117 0 return ( $url, %a );
118 }
119  
120 # Build the detect option hash (just min_markdown for now) from a per-call
121 # %opts plus the instance default. Shared with Net::Async::Crawl4AI.
122 sub _detect_opts {
123 25 ▶ my ( $self, %opts ) = @_;
Subroutine _detect_opts: called CC=3 SCAR=11.0
124 25 ▶ my $min = defined $opts{min_markdown} ? $opts{min_markdown} : $self->min_markdown;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'min_markdown'} ? : 1 24 25
125 25 ▶ return defined $min ? { min_markdown => $min } : {};
Branch: 1 branch
BranchTrueFalseTotal
defined $min ? : 1 24 25
126 }
127  
128 # Turn one strategy run — its page, error, and timing — into an Attempt,
129 # classifying the page via Detect. Shared with Net::Async::Crawl4AI so the sync
130 # and async chains build identical attempt history.
131 #
132 # Classification is performed by overridable methods. To swap in a different
133 # classifier (e.g. Crawl4AI's own quality score), subclass and override
134 # classify_signals and classify_why_failed.
135 sub _attempt_for {
136 38 ▶ my ( $self, $strategy, $page, $err, $elapsed, $detect ) = @_;
Subroutine _attempt_for: called CC=5 SCAR=16.2
137 38 ▶ $detect ||= {};
Condition50%1/2
$detect ||= {}
l!l
380
MC/DC0%0/1
$detect ||= {}
$detect
Truth table50%1/2
A
A= $detect
Aresult
11
00
138 38 ▶ return WWW::Crawl4AI::Attempt->new(
Branch: 1 branch
BranchTrueFalseTotal
if $err 2 36 38
139 backend => $strategy->name,
140 cost_class => $strategy->cost_class,
141 ok => 0,
142 error => $err,
143 why_failed => 'error',
144 elapsed => $elapsed,
145 ) if $err;
146 36 ▶ return WWW::Crawl4AI::Attempt->new(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $page 0 36 36
147 backend => $strategy->name,
148 cost_class => $strategy->cost_class,
149 ok => 0,
150 why_failed => 'empty',
151 elapsed => $elapsed,
152 ) unless defined $page;
153  
154 36 my $signals = $self->classify_signals( $page, %$detect );
155 36 my $good = WWW::Crawl4AI::Detect::is_good( $page, %$detect );
156 36 ▶ return WWW::Crawl4AI::Attempt->new(
Branch: 1 branch
BranchTrueFalseTotal
$good ? : 23 13 36
157 backend => $strategy->name,
158 cost_class => $strategy->cost_class,
159 ok => $good,
160 page => $page,
161 signals => $signals,
162 why_failed => ( $good ? undef : $self->classify_why_failed( $page, %$detect ) ),
163 elapsed => $elapsed,
164 );
165 }
166  
167  
168 sub classify_signals {
169 36 ▶ my ( $self, $page, %opts ) = @_;
Subroutine classify_signals: called CC=1 SCAR=0.0
Pod documented
170 36 return WWW::Crawl4AI::Detect::signals( $page, %opts );
171 }
172  
173  
174 sub classify_why_failed {
175 13 ▶ my ( $self, $page, %opts ) = @_;
Subroutine classify_why_failed: called CC=1 SCAR=0.0
Pod documented
176 13 return WWW::Crawl4AI::Detect::why_failed( $page, %opts );
177 }
178  
179 sub crawl {
180 25 ▶ my ( $self, @args ) = @_;
Subroutine crawl: called CC=5 SCAR=16.2
Pod documented
181 25 my ( $url, %opts ) = $self->_normalize_args(@args);
182 25 ▶ croak "crawl needs a url" unless defined $url && length $url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url and length $url 0 25 25
Condition33%1/3
defined $url and length $url
!ll&&!rl&&r
0025
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
183 25 my $detect = $self->_detect_opts(%opts);
184  
185 25 my @attempts;
186 25 for my $strategy ( @{ $self->strategies } ) {
187 38 my $t0 = Time::HiRes::time();
188 38 my $page = eval { $strategy->crawl( $self, $url, %opts ) };
189 38 my $err = $@;
190 38 my $elapsed = sprintf( '%.3f', Time::HiRes::time() - $t0 ) + 0;
191  
192 38 my $attempt = $self->_attempt_for( $strategy, $page, $err, $elapsed, $detect );
193 38 push @attempts, $attempt;
194 38 ▶ return WWW::Crawl4AI::Result->from_attempt( $attempt, attempts => \@attempts ) if $attempt->ok;
Branch: 1 branch
BranchTrueFalseTotal
if $attempt->ok 23 15 38
195 }
196  
197 2 return $self->_failed_result( $url, \@attempts );
198 }
199  
200  
201 8 ▶sub markdown { my ( $self, @args ) = @_; return $self->crawl(@args) }
Subroutine markdown: called CC=1 SCAR=0.0
Pod documented
202  
203 # Drop the fragment so map apps (#5/lat/lon/...) and trailing-anchor links don't
204 # look like distinct pages during dedup.
205 sub _canon_url {
206 0 ▶ my ( $self, $url ) = @_;
Subroutine _canon_url: not called CC=2 SCAR=17.9
207 0 ▶ my $u = eval { URI->new($url) } or return $url;
Branch: 1 branch
BranchTrueFalseTotal
my $u = eval { do { "URI"->new($url) } } or return $url 0 0 0
208 0 $u->fragment(undef);
209 0 return $u->as_string;
210 }
211  
212 sub deep_crawl {
213 7 ▶ my ( $self, @args ) = @_;
Subroutine deep_crawl: called CC=7 SCAR=19.9
Pod documented
214 7 my ( $start, %opts ) = $self->_normalize_args(@args);
215 7 ▶ croak "deep_crawl needs a url" unless defined $start && length $start;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $start and length $start 0 7 7
Condition33%1/3
defined $start and length $start
!ll&&!rl&&r
007
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
ABresult
0X0
100
111
216  
217 my $iter = WWW::Crawl4AI::DeepCrawlIterator->new(
218 crawler => $self,
219 start_url => $start,
220 max_pages => ( exists $opts{max_pages} ? delete $opts{max_pages} : 25 ),
221 max_depth => ( exists $opts{max_depth} ? delete $opts{max_depth} : 2 ),
222 same_host => ( exists $opts{same_host} ? delete $opts{same_host} : 1 ),
223 url_filter => ( delete $opts{url_filter} ),
224 7 ▶ on_page => ( delete $opts{on_page} ),
Branch: 3 branches
BranchTrueFalseTotal
exists $opts{'max_pages'} ? : 1 6 7
exists $opts{'max_depth'} ? : 7 0 7
exists $opts{'same_host'} ? : 1 6 7
225 );
226  
227 7 my @results;
228 7 ▶ while ( my $page = $iter->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $page = $iter->next) 17 7 24
229 17 push @results, $page->[0];
230 }
231 7 return \@results;
232 }
233  
234  
235 sub _failed_result {
236 2 ▶ my ( $self, $url, $attempts ) = @_;
Subroutine _failed_result: called CC=6 SCAR=21.2
237 2 my $last = $attempts->[-1];
238 2 ▶ return WWW::Crawl4AI::Result->new(
Branch: 1 branch
BranchTrueFalseTotal
unless $last 0 2 2
239 ok => 0,
240 url => $url,
241 attempts => $attempts,
242 error => WWW::Crawl4AI::Error->new(
243 type => 'content',
244 message => 'all crawl strategies failed',
245 url => $url,
246 ),
247 why_failed => 'no_strategies',
248 ) unless $last;
249  
250 return WWW::Crawl4AI::Result->new(
251 ok => 0,
252 url => $url,
253 final_url => ( $last->page ? $last->page->{final_url} : undef ),
254 2 ▶ status => ( $last->page ? $last->page->{status_code} : undef ),
Branch: 2 branches
BranchTrueFalseTotal
$last->page ? : 2 0 2
$last->page ? : 2 0 2
Condition40%2/5
$last->error || 'WWW::Crawl4AI::Error'->new('type', 'content', 'message', 'all crawl strategies failed (last: ' . ($last->why_failed // 'unknown') . ')', 'url', $url, 'backend', $last->backend)
l!l&&r!l&&!r
020
$last->why_failed // "unknown"
l!l
20
MC/DC0%0/2
$last->error || 'WWW::Crawl4AI::Error'->new('type', 'content', 'message', 'all crawl strategies failed (last: ' . ($last->why_failed // 'unknown') . ')', 'url', $url, 'backend', $last->backend)
$last->error 'WWW::Crawl4AI::Error'->new('type', 'content', 'message', 'all crawl strategies failed (last: ' . ($last->why_failed // 'unknown') . ')', 'url', $url, 'backend', $last->backend)
MC/DC0%0/1
$last->why_failed // "unknown"
$last->why_failed
Truth table33%1/3
A || B
A= $last->errorB= 'WWW::Crawl4AI::Error'->new('type', 'content', 'message', 'all crawl strategies failed (last: ' . ($last->why_failed // 'unknown') . ')', 'url', $url, 'backend', $last->backend)
ABresult
1X1
011
000
Truth table50%1/2
A
A= $last->why_failed
Aresult
11
00
255 backend => $last->backend,
256 cost_class => $last->cost_class,
257 signals => $last->signals,
258 why_failed => $last->why_failed,
259 attempts => $attempts,
260 error => (
261 $last->error || WWW::Crawl4AI::Error->new(
262 type => 'content',
263 message => 'all crawl strategies failed (last: ' . ( $last->why_failed // 'unknown' ) . ')',
264 url => $url,
265 backend => $last->backend,
266 )
267 ),
268 );
269 }
270  
271 0 ▶sub health { $_[0]->client->health }
Subroutine health: not called CC=1 SCAR=6.9
Pod documented
272  
273  
274 0 ▶sub screenshot { my $self = shift; $self->client->screenshot(@_) }
Subroutine screenshot: not called CC=1 SCAR=6.9
Pod documented
275 0 ▶sub pdf { my $self = shift; $self->client->pdf(@_) }
Subroutine pdf: not called CC=1 SCAR=6.9
Pod documented
276 0 ▶sub html { my $self = shift; $self->client->html(@_) }
Subroutine html: not called CC=1 SCAR=6.9
Pod documented
277 0 ▶sub execute_js { my $self = shift; $self->client->execute_js(@_) }
Subroutine execute_js: not called CC=1 SCAR=6.9
Pod documented
278 0 ▶sub llm { my $self = shift; $self->client->llm(@_) }
Subroutine llm: not called CC=1 SCAR=6.9
Pod documented
279 0 ▶sub token { my $self = shift; $self->client->token(@_) }
Subroutine token: not called CC=1 SCAR=6.9
Pod documented
280  
281  
282 4 ▶sub available_backends { [ map { $_->name } @{ $_[0]->strategies } ] }
Subroutine available_backends: called CC=1 SCAR=0.0
Pod documented
283  
284  
285 sub detect {
286 0 ▶ my ( $self ) = @_;
Subroutine detect: not called CC=4 SCAR=30.0
Pod documented
287 return {
288 0 ▶ crawl4ai => $self->health,
Branch: 3 branches
BranchTrueFalseTotal
$self->cloakbrowser_url ? : 0 0 0
$self->proxy_url ? : 0 0 0
$self->callback ? : 0 0 0
289 crawl4ai_url => $self->base_url,
290 cloakbrowser => ( $self->cloakbrowser_url ? WWW::Crawl4AI::Detect::probe_cloakbrowser( $self->cloakbrowser_url ) : 0 ),
291 cloakbrowser_url => $self->cloakbrowser_url,
292 proxy => ( $self->proxy_url ? 1 : 0 ),
293 proxy_url => $self->proxy_url,
294 callback => ( $self->callback ? 1 : 0 ),
295 backends => $self->available_backends,
296 };
297 }
298  
299  
300  
301 2 1;
302  
303 __END__
« Crawl4AI/Attempt.pm ↑ Summary Crawl4AI/Result.pm »