blib/lib/WWW/Crawler/Mojo.pm untested

filter: Statementstmt 0.0% 0 / 137 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 11 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 226 CC 26 SCAR 36.2
CC
26
Cov
0%
init
90.0
_make_child
56.0
scrape
42.0
CRAP 37.3
1 package WWW::Crawler::Mojo;
2 use strict;
3 use warnings;
4 use Mojo::Base 'Mojo::EventEmitter';
5 use WWW::Crawler::Mojo::Job;
6 use WWW::Crawler::Mojo::Queue::Memory;
7 use WWW::Crawler::Mojo::UserAgent;
8 use WWW::Crawler::Mojo::ScraperUtil qw{
9 collect_urls_css html_handler_presets reduce_html_handlers resolve_href decoded_body};
10 use Mojo::Message::Request;
11 our $VERSION = '0.24';
12  
13 has clock_speed => 0.25;
14 has html_handlers => sub { html_handler_presets() };
15 has max_conn => 1;
16 has max_conn_per_host => 1;
17 has queue => sub { WWW::Crawler::Mojo::Queue::Memory->new };
18 has 'shuffle';
19 has ua => sub { WWW::Crawler::Mojo::UserAgent->new };
20 has ua_name =>
21 "www-crawler-mojo/$VERSION (+https://github.com/jamadam/www-crawler-mojo)";
22  
23 sub crawl {
24 my ($self) = @_;
25  
26 $self->init;
27  
28 die 'No job is given' unless ($self->queue->length);
29  
30 $self->emit('start');
31  
32 Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
33 }
34  
35 sub init {
36 my ($self) = @_;
37  
38 $self->on('empty', sub { say "Queue is drained out."; $self->stop })
39 unless $self->has_subscribers('empty');
40 $self->on(
41 'error',
42 sub {
43 say
44 sprintf("An error occured during crawling %s: %s", $_[2]->url, $_[1]);
45 }
46 ) unless $self->has_subscribers('error');
47 $self->on('res', sub { $_[1]->() }) unless $self->has_subscribers('res');
48  
49 $self->ua->transactor->name($self->ua_name);
50 $self->ua->max_redirects(5);
51  
52 $self->{_loop_ids} = [];
53  
54 my $id = Mojo::IOLoop->recurring(
55 $self->clock_speed => sub {
56 my $queue = $self->queue;
57  
58 if (!$queue->length) {
59 $self->emit('empty') unless ($self->ua->active_conn);
60 return;
61 }
62 return if $self->ua->active_conn >= $self->max_conn;
63 return
64 if $self->ua->active_host($queue->next->url)
65 >= $self->max_conn_per_host;
66 $self->process_job($queue->dequeue);
67 }
68 );
69  
70 push(@{$self->{_loop_ids}}, $id);
71  
72 if ($self->shuffle) {
73 my $id = Mojo::IOLoop->recurring(
74 $self->shuffle => sub {
75 $self->queue->shuffle;
76 }
77 );
78  
79 push(@{$self->{_loop_ids}}, $id);
80 }
81 }
82  
83 sub process_job {
84 my ($self, $job) = @_;
85  
86 my @args = ($job->method || 'get', $job->url);
87 push(@args, form => $job->tx_params->to_hash) if $job->tx_params;
88 my $tx = $self->ua->build_tx(@args);
89  
90 $self->emit('req', $job, $tx->req);
91  
92 $self->ua->start(
93 $tx => sub {
94 my ($ua, $tx) = @_;
95  
96 $job->redirect(_urls_redirect($tx));
97  
98 my $res = $tx->res;
99  
100 unless ($res->code) {
101 my $msg = $res->error ? $res->error->{message} : 'Unknown error';
102 $self->emit('error', $msg, $job);
103 return;
104 }
105  
106 $self->emit('res', sub { $self->scrape($res, $job, $_[0]) }, $job, $res);
107  
108 $job->close;
109 }
110 );
111 }
112  
113 sub say_start {
114 my $self = shift;
115  
116 print <<"EOF";
117 ----------------------------------------
118 Crawling is starting with @{[ $self->queue->next->url ]}
119 Max Connection : @{[ $self->max_conn ]}
120 User Agent : @{[ $self->ua_name ]}
121 ----------------------------------------
122 EOF
123 }
124  
125 sub scrape {
126 my ($self, $res, $job, $contexts) = @_;
127 my @ret;
128  
129 return unless $res->headers->content_length && $res->body;
130  
131 my $base = $job->url;
132 my $type = $res->headers->content_type;
133  
134 if ($type && $type =~ qr{^(text|application)/(html|xml|xhtml)}) {
135 if ((my $base_tag = $res->dom->at('base[href]'))) {
136 $base = resolve_href($base, $base_tag->attr('href'));
137 }
138 my $dom = Mojo::DOM->new(decoded_body($res));
139 my $handlers = reduce_html_handlers($self->html_handlers, $contexts);
140 for my $selector (sort keys %{$handlers}) {
141 $dom->find($selector)->each(
142 sub {
143 my $dom = shift;
144 for ($handlers->{$selector}->($dom)) {
145 push(@ret, $self->_make_child($_, $dom, $job, $base)) if $_;
146 }
147 }
148 );
149 }
150 }
151  
152 if ($type && $type =~ qr{text/css}) {
153 for (collect_urls_css(decoded_body($res))) {
154 push(@ret, $self->_make_child($_, $job->url, $job, $base));
155 }
156 }
157  
158 return @ret;
159 }
160  
161 sub stop {
162 my $self = shift;
163 while (my $id = shift @{$self->{_loop_ids}}) {
164 Mojo::IOLoop->remove($id);
165 }
166 Mojo::IOLoop->stop;
167 }
168  
169 sub _make_child {
170 my ($self, $url, $context, $job, $base) = @_;
171  
172 return unless $url;
173 ($url, my $method, my $params) = @$url if (ref $url);
174  
175 my $resolved = resolve_href($base, $url);
176  
177 return unless ($resolved->scheme =~ qr{^(http|https|ftp|ws|wss)$});
178  
179 my $child
180 = $job->child(url => $resolved, literal_uri => $url, context => $context);
181  
182 $child->method($method) if $method;
183  
184 if ($params) {
185 if ($method eq 'GET') {
186 $child->url->query->append($params);
187 }
188 else {
189 $child->tx_params($params);
190 }
191 }
192  
193 return $child;
194 }
195  
196 sub enqueue {
197 my ($self, @jobs) = @_;
198 return
199 map { $self->queue->enqueue(WWW::Crawler::Mojo::Job->upgrade($_)) } @jobs;
200 }
201  
202 sub requeue {
203 my ($self, @jobs) = @_;
204 return
205 map { $self->queue->requeue(WWW::Crawler::Mojo::Job->upgrade($_)) } @jobs;
206 }
207  
208 sub _urls_redirect {
209 my $tx = shift;
210 my @urls;
211 @urls = _urls_redirect($tx->previous) if ($tx->previous);
212 unshift(@urls, $tx->req->url->userinfo(undef));
213 return @urls;
214 }
215  
216 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