| 1 | package WWW::Crawler::Mojo; | ||||||||||||||||||||||||||||
| 2 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use Mojo::Base 'Mojo::EventEmitter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use WWW::Crawler::Mojo::Job; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use WWW::Crawler::Mojo::Queue::Memory; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use WWW::Crawler::Mojo::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 10 | use WWW::Crawler::Mojo::ScraperUtil qw{ | |||||||||||||||||||||||||||
| 9 | 10 | ▶ | collect_urls_css html_handler_presets reduce_html_handlers resolve_href decoded_body}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 10 | ▶ | use Mojo::Message::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 10 | our $VERSION = '0.24'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 10 | has clock_speed => 0.25; | |||||||||||||||||||||||||||
| 14 | 16 | ▶ | has html_handlers => sub { html_handler_presets() }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 10 | has max_conn => 1; | |||||||||||||||||||||||||||
| 16 | 10 | has max_conn_per_host => 1; | |||||||||||||||||||||||||||
| 17 | 15 | ▶ | has queue => sub { WWW::Crawler::Mojo::Queue::Memory->new }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 10 | has 'shuffle'; | |||||||||||||||||||||||||||
| 19 | 13 | ▶ | has ua => sub { WWW::Crawler::Mojo::UserAgent->new }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 10 | has ua_name => | |||||||||||||||||||||||||||
| 21 | "www-crawler-mojo/$VERSION (+https://github.com/jamadam/www-crawler-mojo)"; | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | sub crawl { | ||||||||||||||||||||||||||||
| 24 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
crawl: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | $self->init; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | die 'No job is given' unless ($self->queue->length); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | $self->emit('start'); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | Mojo::IOLoop->start unless Mojo::IOLoop->is_running; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | sub init { | ||||||||||||||||||||||||||||
| 36 | 12 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=5 SCAR=17.2
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 2 | ▶ | $self->on('empty', sub { say "Queue is drained out."; $self->stop }) | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 12 | ▶ | unless $self->has_subscribers('empty'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | $self->on( | ||||||||||||||||||||||||||||
| 41 | 'error', | ||||||||||||||||||||||||||||
| 42 | sub { | ||||||||||||||||||||||||||||
| 43 | 0 | ▶ | say | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 44 | sprintf("An error occured during crawling %s: %s", $_[2]->url, $_[1]); | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 12 | ▶ | ) unless $self->has_subscribers('error'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 12 | ▶ | $self->on('res', sub { $_[1]->() }) unless $self->has_subscribers('res'); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 12 | $self->ua->transactor->name($self->ua_name); | |||||||||||||||||||||||||||
| 50 | 12 | $self->ua->max_redirects(5); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 12 | $self->{_loop_ids} = []; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | my $id = Mojo::IOLoop->recurring( | ||||||||||||||||||||||||||||
| 55 | $self->clock_speed => sub { | ||||||||||||||||||||||||||||
| 56 | 15 | ▶ | my $queue = $self->queue; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.5
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 15 | ▶ | if (!$queue->length) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 2 | ▶ | $self->emit('empty') unless ($self->ua->active_conn); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 2 | return; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 13 | ▶ | return if $self->ua->active_conn >= $self->max_conn; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | return | ||||||||||||||||||||||||||||
| 64 | 13 | ▶ | if $self->ua->active_host($queue->next->url) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | >= $self->max_conn_per_host; | ||||||||||||||||||||||||||||
| 66 | 13 | $self->process_job($queue->dequeue); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 12 | ); | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 12 | push(@{$self->{_loop_ids}}, $id); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 12 | ▶ | if ($self->shuffle) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | my $id = Mojo::IOLoop->recurring( | ||||||||||||||||||||||||||||
| 74 | $self->shuffle => sub { | ||||||||||||||||||||||||||||
| 75 | 0 | ▶ | $self->queue->shuffle; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | 0 | ); | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | push(@{$self->{_loop_ids}}, $id); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | sub process_job { | ||||||||||||||||||||||||||||
| 84 | 13 | ▶ | my ($self, $job) = @_; | ||||||||||||||||||||||||||
|
Subroutine
process_job: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 13 | ▶ | my @args = ($job->method || 'get', $job->url); | ||||||||||||||||||||||||||
|
Condition100%2/2
$job->method || 'get'
MC/DC100%1/1
$job->method || 'get'
$job->method
Truth table100%2/2
A
A= $job->method
| |||||||||||||||||||||||||||||
| 87 | 13 | ▶ | push(@args, form => $job->tx_params->to_hash) if $job->tx_params; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 13 | my $tx = $self->ua->build_tx(@args); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 13 | $self->emit('req', $job, $tx->req); | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | $self->ua->start( | ||||||||||||||||||||||||||||
| 93 | $tx => sub { | ||||||||||||||||||||||||||||
| 94 | 13 | ▶ | my ($ua, $tx) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=13.1
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 13 | $job->redirect(_urls_redirect($tx)); | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 13 | my $res = $tx->res; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 13 | ▶ | unless ($res->code) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | my $msg = $res->error ? $res->error->{message} : 'Unknown error'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | $self->emit('error', $msg, $job); | |||||||||||||||||||||||||||
| 103 | 0 | return; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 13 | ▶ | $self->emit('res', sub { $self->scrape($res, $job, $_[0]) }, $job, $res); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 13 | $job->close; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | 13 | ); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | sub say_start { | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
say_start: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | print <<"EOF"; | |||||||||||||||||||||||||||
| 117 | ---------------------------------------- | ||||||||||||||||||||||||||||
| 118 | 0 | Crawling is starting with @{[ $self->queue->next->url ]} | |||||||||||||||||||||||||||
| 119 | 0 | Max Connection : @{[ $self->max_conn ]} | |||||||||||||||||||||||||||
| 120 | 0 | User Agent : @{[ $self->ua_name ]} | |||||||||||||||||||||||||||
| 121 | ---------------------------------------- | ||||||||||||||||||||||||||||
| 122 | EOF | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | sub scrape { | ||||||||||||||||||||||||||||
| 126 | 26 | ▶ | my ($self, $res, $job, $contexts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
scrape: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||
| 127 | 26 | my @ret; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 26 | ▶ | return unless $res->headers->content_length && $res->body; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$res->headers->content_length and $res->body
MC/DC50%1/2
$res->headers->content_length and $res->body
$res->headers->content_length
$res->body
Truth table66%2/3
A and B
A= $res->headers->content_lengthB= $res->body
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 25 | my $base = $job->url; | |||||||||||||||||||||||||||
| 132 | 25 | my $type = $res->headers->content_type; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 25 | ▶ | if ($type && $type =~ qr{^(text|application)/(html|xml|xhtml)}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$type and $type =~ qr"^(text|application)/(html|xml|xhtml)"u
MC/DC50%1/2
$type and $type =~ qr"^(text|application)/(html|xml|xhtml)"u
$type
$type =~ qr"^(text|application)/(html|xml|xhtml)"u
Truth table66%2/3
A and B
A= $typeB= $type =~ qr"^(text|application)/(html|xml|xhtml)"u
| |||||||||||||||||||||||||||||
| 135 | 23 | ▶ | if ((my $base_tag = $res->dom->at('base[href]'))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 4 | $base = resolve_href($base, $base_tag->attr('href')); | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 23 | my $dom = Mojo::DOM->new(decoded_body($res)); | |||||||||||||||||||||||||||
| 139 | 23 | my $handlers = reduce_html_handlers($self->html_handlers, $contexts); | |||||||||||||||||||||||||||
| 140 | 23 | for my $selector (sort keys %{$handlers}) { | |||||||||||||||||||||||||||
| 141 | $dom->find($selector)->each( | ||||||||||||||||||||||||||||
| 142 | sub { | ||||||||||||||||||||||||||||
| 143 | 83 | ▶ | my $dom = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 144 | 83 | for ($handlers->{$selector}->($dom)) { | |||||||||||||||||||||||||||
| 145 | 86 | ▶ | push(@ret, $self->_make_child($_, $dom, $job, $base)) if $_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 360 | ); | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 25 | ▶ | if ($type && $type =~ qr{text/css}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$type and $type =~ qr"text/css"u
MC/DC50%1/2
$type and $type =~ qr"text/css"u
$type
$type =~ qr"text/css"u
Truth table66%2/3
A and B
A= $typeB= $type =~ qr"text/css"u
| |||||||||||||||||||||||||||||
| 153 | 1 | for (collect_urls_css(decoded_body($res))) { | |||||||||||||||||||||||||||
| 154 | 1 | push(@ret, $self->_make_child($_, $job->url, $job, $base)); | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 25 | return @ret; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | sub stop { | ||||||||||||||||||||||||||||
| 162 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
stop: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 163 | 2 | ▶ | while (my $id = shift @{$self->{_loop_ids}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 2 | Mojo::IOLoop->remove($id); | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | 2 | Mojo::IOLoop->stop; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | sub _make_child { | ||||||||||||||||||||||||||||
| 170 | 84 | ▶ | my ($self, $url, $context, $job, $base) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_make_child: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 84 | ▶ | return unless $url; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 84 | ▶ | ($url, my $method, my $params) = @$url if (ref $url); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 84 | my $resolved = resolve_href($base, $url); | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 84 | ▶ | return unless ($resolved->scheme =~ qr{^(http|https|ftp|ws|wss)$}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 73 | my $child | |||||||||||||||||||||||||||
| 180 | = $job->child(url => $resolved, literal_uri => $url, context => $context); | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 73 | ▶ | $child->method($method) if $method; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 73 | ▶ | if ($params) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 6 | ▶ | if ($method eq 'GET') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 3 | $child->url->query->append($params); | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | else { | ||||||||||||||||||||||||||||
| 189 | 3 | $child->tx_params($params); | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 73 | return $child; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | sub enqueue { | ||||||||||||||||||||||||||||
| 197 | 59 | ▶ | my ($self, @jobs) = @_; | ||||||||||||||||||||||||||
|
Subroutine
enqueue: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 198 | return | ||||||||||||||||||||||||||||
| 199 | 59 | map { $self->queue->enqueue(WWW::Crawler::Mojo::Job->upgrade($_)) } @jobs; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | sub requeue { | ||||||||||||||||||||||||||||
| 203 | 1 | ▶ | my ($self, @jobs) = @_; | ||||||||||||||||||||||||||
|
Subroutine
requeue: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 204 | return | ||||||||||||||||||||||||||||
| 205 | 1 | map { $self->queue->requeue(WWW::Crawler::Mojo::Job->upgrade($_)) } @jobs; | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | sub _urls_redirect { | ||||||||||||||||||||||||||||
| 209 | 13 | ▶ | my $tx = shift; | ||||||||||||||||||||||||||
|
Subroutine
_urls_redirect: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 210 | 13 | my @urls; | |||||||||||||||||||||||||||
| 211 | 13 | ▶ | @urls = _urls_redirect($tx->previous) if ($tx->previous); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | 13 | unshift(@urls, $tx->req->url->userinfo(undef)); | |||||||||||||||||||||||||||
| 213 | 13 | return @urls; | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 10 | 1; | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =head1 NAME | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | WWW::Crawler::Mojo - A web crawling framework for Perl | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | use strict; | ||||||||||||||||||||||||||||
| 225 | use warnings; | ||||||||||||||||||||||||||||
| 226 | use WWW::Crawler::Mojo; | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | my $bot = WWW::Crawler::Mojo->new; | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | $bot->on(res => sub { | ||||||||||||||||||||||||||||
| 231 | my ($bot, $scrape, $job, $res) = @_; | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | $bot->enqueue($_) for $scrape->('#context'); | ||||||||||||||||||||||||||||
| 234 | }); | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | $bot->enqueue('http://example.com/'); | ||||||||||||||||||||||||||||
| 237 | $bot->crawl; | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | L<WWW::Crawler::Mojo> is a web crawling framework for those who are familiar with | ||||||||||||||||||||||||||||
| 242 | L<Mojo>::* APIs. | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | Althogh the module is only well tested for "focused crawl" at this point, | ||||||||||||||||||||||||||||
| 245 | you can also use it for endless crawling by taking special care of memory usage. | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | L<WWW::Crawler::Mojo> inherits all attributes from L<Mojo::EventEmitter> and | ||||||||||||||||||||||||||||
| 250 | implements the following new ones. | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | =head2 clock_speed | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | A number of main event loop interval in milliseconds. Defaults to 0.25. | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | $bot->clock_speed(2); | ||||||||||||||||||||||||||||
| 257 | my $clock = $bot->clock_speed; # 2 | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | =head2 html_handlers | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | Sets HTML handlers of scrapper. Defaults to | ||||||||||||||||||||||||||||
| 262 | WWW::Crawler::Mojo::ScraperUtil::html_handler_presets. | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | $bot->html_handlers( { | ||||||||||||||||||||||||||||
| 265 | 'a[href]' => sub { return $_[0]->{href} }, | ||||||||||||||||||||||||||||
| 266 | 'img[src]' => sub { return $_[0]->{src} }, | ||||||||||||||||||||||||||||
| 267 | } ); | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | =head2 max_conn | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | An amount of max connections. | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | $bot->max_conn(5); | ||||||||||||||||||||||||||||
| 274 | say $bot->max_conn; # 5 | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | =head2 max_conn_per_host | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | An amount of max connections per host. | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | $bot->max_conn_per_host(5); | ||||||||||||||||||||||||||||
| 281 | say $bot->max_conn_per_host; # 5 | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | =head2 queue | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | L<WWW::Crawler::Mojo::Queue::Memory> object for default. | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | $bot->queue(WWW::Crawler::Mojo::Queue::Memory->new); | ||||||||||||||||||||||||||||
| 288 | $bot->queue->enqueue($job); | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | =head2 shuffle | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | An interval in seconds to shuffle the job queue. It also evalutated as boolean | ||||||||||||||||||||||||||||
| 293 | for disabling/enabling the feature. Defaults to undef, meaning disable. | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | $bot->shuffle(5); | ||||||||||||||||||||||||||||
| 296 | say $bot->shuffle; # 5 | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | =head2 ua | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | A L<WWW::Crawler::Mojo::UserAgent> instance. | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | my $ua = $bot->ua; | ||||||||||||||||||||||||||||
| 303 | $bot->ua(WWW::Crawler::Mojo::UserAgent->new); | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | =head2 ua_name | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | Name of crawler for User-Agent header. | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | $bot->ua_name('my-bot/0.01 (+https://example.com/)'); | ||||||||||||||||||||||||||||
| 310 | say $bot->ua_name; # 'my-bot/0.01 (+https://example.com/)' | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | =head1 EVENTS | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | L<WWW::Crawler::Mojo> inherits all events from L<Mojo::EventEmitter> and | ||||||||||||||||||||||||||||
| 315 | implements the following new ones. | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | =head2 req | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | Emitted right before crawler perform request to servers. The callback takes 3 | ||||||||||||||||||||||||||||
| 320 | arguments. | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | $bot->on(req => sub { | ||||||||||||||||||||||||||||
| 323 | my ($bot, $job, $req) = @_; | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | # DO NOTHING | ||||||||||||||||||||||||||||
| 326 | }); | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | =head2 res | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | Emitted when crawler got response from server. The callback takes 4 arguments. | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | $bot->on(res => sub { | ||||||||||||||||||||||||||||
| 333 | my ($bot, $scrape, $job, $res) = @_; | ||||||||||||||||||||||||||||
| 334 | if (...) { | ||||||||||||||||||||||||||||
| 335 | $bot->enqueue($_) for $scrape->(); | ||||||||||||||||||||||||||||
| 336 | } else { | ||||||||||||||||||||||||||||
| 337 | # DO NOTHING | ||||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||
| 339 | }); | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | =head3 $bot | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | L<WWW::Crawler::Mojo> instance. | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | =head3 $scrape | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | Scraper code reference for current document. The code takes optional argument | ||||||||||||||||||||||||||||
| 348 | CSS selector for context and returns new jobs. | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | for my $job ($scrape->($context)) { | ||||||||||||||||||||||||||||
| 351 | $bot->enqueue($job) | ||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | Optionally you can specify a scraping target container in CSS selector. | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | @jobs = $scrape->('#container'); | ||||||||||||||||||||||||||||
| 357 | @jobs = $scrape->(['#container1', '#container2']); | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | =head3 $job | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | L<WWW::Crawler::Mojo::Job> instance. | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | =head3 $res | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | L<Mojo::Message::Response> instance. | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | =head2 empty | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | Emitted when queue length gets zero. | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | $bot->on(empty => sub { | ||||||||||||||||||||||||||||
| 372 | my ($bot) = @_; | ||||||||||||||||||||||||||||
| 373 | say "Queue is drained out."; | ||||||||||||||||||||||||||||
| 374 | }); | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | =head2 error | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | Emitted when user agent returns no status code for request. Possibly caused by | ||||||||||||||||||||||||||||
| 379 | network errors or un-responsible servers. | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | $bot->on(error => sub { | ||||||||||||||||||||||||||||
| 382 | my ($bot, $error, $job) = @_; | ||||||||||||||||||||||||||||
| 383 | say "error: $error"; | ||||||||||||||||||||||||||||
| 384 | if (...) { # until failur occures 3 times | ||||||||||||||||||||||||||||
| 385 | $bot->requeue($job); | ||||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||
| 387 | }); | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | Note that server errors such as 404 or 500 cannot be catched with the event. | ||||||||||||||||||||||||||||
| 390 | Consider res event for the use case instead of this. | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | =head2 start | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | Emitted right before crawl is started. | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | $bot->on(start => sub { | ||||||||||||||||||||||||||||
| 397 | my $self = shift; | ||||||||||||||||||||||||||||
| 398 | ... | ||||||||||||||||||||||||||||
| 399 | }); | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | =head1 METHODS | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | L<WWW::Crawler::Mojo> inherits all methods from L<Mojo::EventEmitter> and | ||||||||||||||||||||||||||||
| 404 | implements the following new ones. | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | =head2 crawl | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | Starts crawling loop. | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | $bot->crawl; | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | =head2 init | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | Initializes crawler settings. | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | $bot->init; | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | =head2 process_job | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | Processes a job. | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | $bot->process_job; | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | =head2 say_start | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | Displays starting messages to STDOUT | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | $bot->say_start; | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | =head2 scrape | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | Parses and discovers links in a web page and CSS. This performs scraping. | ||||||||||||||||||||||||||||
| 433 | With the optional 4th argument, you can specify a CSS selector to container | ||||||||||||||||||||||||||||
| 434 | you would collect URLs within. | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | $bot->scrape($res, $job, ); | ||||||||||||||||||||||||||||
| 437 | $bot->scrape($res, $job, $selector); | ||||||||||||||||||||||||||||
| 438 | $bot->scrape($res, $job, [$selector1, $selector2]); | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | =head2 stop | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | Stop crawling. | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | $bot->stop; | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | =head2 enqueue | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | Appends one or more URLs or L<WWW::Crawler::Mojo::Job> objects. Returns the jobs | ||||||||||||||||||||||||||||
| 449 | actually added. | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | my @jobs = $bot->enqueue('http://example.com/index1.html'); | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | OR | ||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | my @jobs = $bot->enqueue($job1, $job2); | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | OR | ||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | my @jobs = $bot->enqueue( | ||||||||||||||||||||||||||||
| 460 | 'http://example.com/index1.html', | ||||||||||||||||||||||||||||
| 461 | 'http://example.com/index2.html', | ||||||||||||||||||||||||||||
| 462 | 'http://example.com/index3.html', | ||||||||||||||||||||||||||||
| 463 | ); | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | =head2 requeue | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | Appends one or more URLs or jobs for re-try. This accepts same arguments as | ||||||||||||||||||||||||||||
| 468 | enqueue method. Returns the jobs actually added. | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | $self->on(error => sub { | ||||||||||||||||||||||||||||
| 471 | my ($self, $msg, $job) = @_; | ||||||||||||||||||||||||||||
| 472 | if (...) { # until failur occures 3 times | ||||||||||||||||||||||||||||
| 473 | $bot->requeue($job); | ||||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||
| 475 | }); | ||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | =head1 EXAMPLE | ||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | L<https://github.com/jamadam/WWW-Flatten> | ||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | Keita Sugama, E<lt>sugama@jamadam.comE<gt> | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | Copyright (C) jamadam | ||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | This program is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 490 | modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||
| 492 | =cut | ||||||||||||||||||||||||||||