| 1 | package WWW::Crawl::Auto; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use parent 'WWW::Crawl'; | ||
| 7 | |||
| 8 | use URI; | ||
| 9 | use WWW::Crawl::Chromium; | ||
| 10 | |||
| 11 | our $VERSION = '0.5'; | ||
| 12 | # $VERSION = eval $VERSION; | ||
| 13 | |||
| 14 | sub new { | ||
| 15 | my $class = shift; | ||
| 16 | my %attrs = @_; | ||
| 17 | |||
| 18 | $attrs{'auto_min_bytes'} //= 512; | ||
| 19 | $attrs{'retry_count'} //= 0; | ||
| 20 | |||
| 21 | my $self = $class->SUPER::new(%attrs); | ||
| 22 | |||
| 23 | $self->{'_chromium'} = WWW::Crawl::Chromium->new(%attrs); | ||
| 24 | $self->{'_mode_by_authority'} = {}; | ||
| 25 | $self->{'_force_chromium'} = _normalize_host_list($attrs{'force_chromium'}); | ||
| 26 | $self->{'_force_http'} = _normalize_host_list($attrs{'force_http'}); | ||
| 27 | |||
| 28 | return $self; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub _fetch_page { | ||
| 32 | my ($self, $url) = @_; | ||
| 33 | print STDERR "DEBUG: _fetch_page called for $url. Debug flag: " . ($self->{'debug'} // 'undef') . "\n" if $self->{'debug'}; | ||
| 34 | |||
| 35 | my $authority = _authority_for($url); | ||
| 36 | |||
| 37 | if ($self->{'_force_chromium'}{$authority}) { | ||
| 38 | $self->{'_mode_by_authority'}{$authority} = 'chromium'; | ||
| 39 | return $self->{'_chromium'}->_fetch_page($url); | ||
| 40 | } | ||
| 41 | if ($self->{'_force_http'}{$authority}) { | ||
| 42 | $self->{'_mode_by_authority'}{$authority} = 'http'; | ||
| 43 | return $self->SUPER::_fetch_page($url); | ||
| 44 | } | ||
| 45 | |||
| 46 | my $mode = $self->{'_mode_by_authority'}{$authority}; | ||
| 47 | if ($mode && $mode eq 'chromium') { | ||
| 48 | print STDERR "Use Chromium (forced): $url\n" if $self->{'debug'}; | ||
| 49 | return $self->{'_chromium'}->_fetch_page($url); | ||
| 50 | } | ||
| 51 | if ($mode && $mode eq 'http') { | ||
| 52 | print STDERR "Use HTTP (forced): $url\n" if $self->{'debug'}; | ||
| 53 | return $self->SUPER::_fetch_page($url); | ||
| 54 | } | ||
| 55 | |||
| 56 | my $resp = $self->SUPER::_fetch_page($url); | ||
| 57 | if ($resp->{'success'}) { | ||
| 58 | if ($self->_should_use_chromium($url, $resp)) { | ||
| 59 | my $chromium_resp = $self->{'_chromium'}->_fetch_page($url); | ||
| 60 | if ($chromium_resp->{'success'}) { | ||
| 61 | $self->{'_mode_by_authority'}{$authority} = 'chromium'; | ||
| 62 | print STDERR "Switched to Chromium: $url\n" if $self->{'debug'}; | ||
| 63 | return $chromium_resp; | ||
| 64 | } | ||
| 65 | # If Chromium failed but we decided we SHOULD use it, we shouldn't fallback to HTTP | ||
| 66 | # because that returns raw dynamic logic which is useless. | ||
| 67 | # However, if we return failure here, the crawl stops for this page. | ||
| 68 | # But at least we don't poison the cache with 'http' mode. | ||
| 69 | return $chromium_resp; | ||
| 70 | } | ||
| 71 | $self->{'_mode_by_authority'}{$authority} = 'http'; | ||
| 72 | return $resp; | ||
| 73 | } | ||
| 74 | |||
| 75 | if ($resp->{'status'} && $resp->{'status'} == 404) { | ||
| 76 | $self->{'_mode_by_authority'}{$authority} = 'http'; | ||
| 77 | return $resp; | ||
| 78 | } | ||
| 79 | |||
| 80 | my $chromium_resp = $self->{'_chromium'}->_fetch_page($url); | ||
| 81 | if ($chromium_resp->{'success'}) { | ||
| 82 | $self->{'_mode_by_authority'}{$authority} = 'chromium'; | ||
| 83 | print STDERR "Use Chromium (fallback): $url\n" if $self->{'debug'}; | ||
| 84 | return $chromium_resp; | ||
| 85 | } | ||
| 86 | |||
| 87 | return $resp; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub _should_use_chromium { | ||
| 91 | my ($self, $url, $resp) = @_; | ||
| 92 | print STDERR "DEBUG: _should_use_chromium checking $url\n" if $self->{'debug'}; | ||
| 93 | |||
| 94 | if ($self->{'auto_decider'} && ref $self->{'auto_decider'} eq 'CODE') { | ||
| 95 | return $self->{'auto_decider'}->($url, $resp, $self) ? 1 : 0; | ||
| 96 | } | ||
| 97 | |||
| 98 | my $content = $resp->{'content'} // ''; | ||
| 99 | return 0 if $content eq ''; | ||
| 100 | |||
| 101 | my $headers = $resp->{'headers'} || {}; | ||
| 102 | my $ctype = $headers->{'content-type'} || $headers->{'Content-Type'} || ''; | ||
| 103 | if ($ctype ne '' && $ctype !~ m{\btext/html\b}i) { | ||
| 104 | print STDERR "DEBUG: Not text/html ($ctype)\n" if $self->{'debug'}; | ||
| 105 | return 0; | ||
| 106 | } | ||
| 107 | |||
| 108 | if ($content =~ /data-capo/i) { | ||
| 109 | print STDERR "DEBUG: Found data-capo\n" if $self->{'debug'}; | ||
| 110 | return 1; | ||
| 111 | } | ||
| 112 | |||
| 113 | if ($content =~ /<noscript[^>]*>.*?(enable javascript|requires javascript|turn on javascript|javascript required)/is) { | ||
| 114 | print STDERR "DEBUG: Found noscript requirement\n" if $self->{'debug'}; | ||
| 115 | return 1; | ||
| 116 | } | ||
| 117 | if ($content =~ /id\s*=\s*["'](?:app|root|__next|__nuxt|svelte|react-root)["']/i) { | ||
| 118 | print STDERR "DEBUG: Found SPA ID\n" if $self->{'debug'}; | ||
| 119 | return 1; | ||
| 120 | } | ||
| 121 | if ($content =~ /window\.(?:__NUXT__|__NEXT_DATA__)/i) { | ||
| 122 | print STDERR "DEBUG: Found window.__NUXT__ or similar\n" if $self->{'debug'}; | ||
| 123 | return 1; | ||
| 124 | } | ||
| 125 | if (length($content) < $self->{'auto_min_bytes'} && $content =~ /<script\b/i) { | ||
| 126 | print STDERR "DEBUG: Small content with script\n" if $self->{'debug'}; | ||
| 127 | return 1; | ||
| 128 | } | ||
| 129 | |||
| 130 | print STDERR "DEBUG: No indicators found. Content length: " . length($content) . "\n" if $self->{'debug'}; | ||
| 131 | return 0; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub _authority_for { | ||
| 135 | my ($url) = @_; | ||
| 136 | my $uri = URI->new($url); | ||
| 137 | return $uri ? ($uri->authority || $url) : $url; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub _normalize_host_list { | ||
| 141 | my ($list) = @_; | ||
| 142 | return {} unless $list; | ||
| 143 | my @hosts = ref $list eq 'ARRAY' ? @$list : ($list); | ||
| 144 | my %map = map { $_ => 1 } @hosts; | ||
| 145 | return \%map; | ||
| 146 | } | ||
| 147 | |||
| 148 | 1; | ||
| 149 | |||
| 150 | __END__ |