Scrappy/Scraper.pm

filter: Statementstmt 17.4% 62 / 355 Branchbran 0.0% 0 / 162 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 37.7% 23 / 61 Podpod 0.0% 0 / 27 totaltotal 13.2% 85 / 640 CC 93 SCAR 47.8
CC
93
Cov
12%
page_match
506.0
download
272.0
pause
90.0
CRAP 119.1
« Scrappy/Logger.pm ↑ Summary Scrappy/Scraper/Control.pm »
1 package Scrappy::Scraper;
2  
3 10 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 $Scrappy::Scraper::VERSION = '0.94112090';
5 }
6  
7 # load OO System
8 10 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # load other libraries
11 10 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use File::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Scrappy::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use Scrappy::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use Scrappy::Queue;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 10 ▶use Scrappy::Scraper::Control;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 10 ▶use Scrappy::Scraper::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 10 ▶use Scrappy::Scraper::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 10 ▶use Scrappy::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 10 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 10 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 10 ▶use Web::Scraper;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 10 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # html content attribute
26 10 has 'content' => (
27 is => 'rw',
28 isa => 'Any'
29 );
30  
31 # access control object
32 has 'control' => (
33 is => 'ro',
34 isa => 'Scrappy::Scraper::Control',
35 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
36 2 Scrappy::Scraper::Control->new;
37 }
38 10 );
39  
40 # debug attribute
41 10 has 'debug' => (
42 is => 'rw',
43 isa => 'Bool',
44 default => 1
45 );
46  
47 # log object
48 has 'logger' => (
49 is => 'ro',
50 isa => 'Scrappy::Logger',
51 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
52 2 Scrappy::Logger->new;
53 }
54 10 );
55  
56 # parser object
57 has 'parser' => (
58 is => 'ro',
59 isa => 'Scrappy::Scraper::Parser',
60 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
61 2 Scrappy::Scraper::Parser->new;
62 }
63 10 );
64  
65 # plugins object
66 has 'plugins' => (
67 is => 'ro',
68 isa => 'Any',
69 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
70 2 Scrappy::Plugin->new;
71 }
72 10 );
73  
74 # queue object
75 has 'queue' => (
76 is => 'ro',
77 isa => 'Scrappy::Queue',
78 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
79 2 Scrappy::Queue->new;
80 }
81 10 );
82  
83 # session object
84 has 'session' => (
85 is => 'ro',
86 isa => 'Scrappy::Session',
87 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
88 2 Scrappy::Session->new;
89 }
90 10 );
91  
92 # user-agent object
93 has 'user_agent' => (
94 is => 'ro',
95 isa => 'Scrappy::Scraper::UserAgent',
96 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
97 2 Scrappy::Scraper::UserAgent->new;
98 }
99 10 );
100  
101 # www-mechanize object (does most of the heavy lifting, gets passed around alot)
102 has 'worker' => (
103 is => 'ro',
104 isa => 'WWW::Mechanize',
105 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
106 2 WWW::Mechanize->new;
107 }
108 10 );
109  
110 0 ▶sub back {
Subroutine back: not called CC=4 SCAR=30.0
Pod undocumented
111 0 my $self = shift;
112  
113 # specify user-agent
114 0 ▶ $self->worker->add_header("User-Agent" => $self->user_agent->name)
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->user_agent->name 0 0 0
115 if defined $self->user_agent->name;
116  
117 # set html response
118 0 $self->content('');
119 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
120 0 $self->worker->back;
121 0 $self->content($self->worker->content);
122 }
123 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
124 0 $self->log("error", "navigating to the previous page failed");
125 0 };
126  
127 0 ▶ return unless $self->content;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->content 0 0 0
128  
129 0 $self->log("info", "navigated back to " . $self->url . " successfully");
130  
131 0 ▶ $self->stash->{history} = [] unless defined $self->stash->{history};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->stash->{"history"} 0 0 0
132 0 push @{$self->stash->{history}}, $self->url;
133 $self->worker->{cookie_jar}->scan(
134 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
135  
136 0 my ($version, $key, $val, $path,
137 $domain, $port, $path_spec, $secure,
138 $expires, $discard, $hash
139 ) = @_;
140  
141 0 ▶ $self->session->stash('cookies' => {})
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->session->stash("cookies") 0 0 0
142 unless defined $self->session->stash('cookies');
143  
144 0 $self->session->stash->{'cookies'}->{$domain}->{$key} = {
145 version => $version,
146 key => $key,
147 val => $val,
148 path => $path,
149 domain => $domain,
150 port => $port,
151 path_spec => $path_spec,
152 secure => $secure,
153 expires => $expires,
154 discard => $discard,
155 hash => $hash
156 };
157  
158 0 $self->session->write;
159  
160 }
161 0 );
162  
163 0 return $self->url;
164 }
165  
166 0 ▶sub cookies {
Subroutine cookies: not called CC=2 SCAR=17.9
Pod undocumented
167 0 my $self = shift;
168 0 ▶ $self->worker->{cookie_jar} = $_[0] if defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] 0 0 0
169 0 return $self->worker->{cookie_jar};
170 }
171  
172 0 ▶sub domain {
Subroutine domain: not called CC=1 SCAR=6.9
Pod undocumented
173 0 return shift->worker->base->host;
174 }
175  
176 0 ▶sub download {
Subroutine download: not called CC=16 SCAR=56.1
Pod undocumented
177 0 my $self = shift;
178 0 my ($url, $dir, $file) = @_;
179  
180 0 $url = URI->new(@_);
181  
182 # specify user-agent
183 0 ▶ $self->worker->add_header("User-Agent" => $self->user_agent->name)
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->user_agent->name 0 0 0
184 if defined $self->user_agent->name;
185  
186 # set html response
187 0 ▶ if ($url && $dir && $file) {
Branch: 3 branches
BranchTrueFalseTotal
if ($url and $dir and $file) { } 0 0 0
elsif ($url and $dir) { } 0 0 0
elsif ($url) { } 0 0 0
Condition0%0/6
$url and $dir and $file
!ll&&!rl&&r
000
$url and $dir
!ll&&!rl&&r
000
MC/DC0%0/3
$url and $dir and $file
$url $dir $file
Truth table0%0/4
A and B and C
A= $urlB= $dirC= $file
ABCresult
0XX0
10X0
1100
1111
188 0 $dir =~ s/[\\\/]+$//;
189 0 ▶ return unless $self->get($url);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get($url) 0 0 0
190 0 $self->store(join '/', $dir, $file);
191 0 $self->log("info",
192 "$url was downloaded to "
193 . join('/', $dir, $file)
194 . " successfully");
195 0 $self->back;
196 }
197 ▶ elsif ($url && $dir) {
Condition0%0/3
$url and $dir
!ll&&!rl&&r
000
MC/DC0%0/2
$url and $dir
$url $dir
Truth table0%0/3
A and B
A= $urlB= $dir
ABresult
0X0
100
111
198 0 $dir =~ s/[\\\/]+$//;
199 0 ▶ return unless $self->get($url);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get($url) 0 0 0
200 0 my @chars = ('a' .. 'z', 'A' .. 'Z', 0 .. 9);
201 0 my $filename = $self->worker->response->filename;
202 0 ▶ $filename =
Branch: 1 branch
BranchTrueFalseTotal
unless $filename 0 0 0
203 $chars[rand(@chars)]
204 . $chars[rand(@chars)]
205 . $chars[rand(@chars)]
206 . $chars[rand(@chars)]
207 . $chars[rand(@chars)]
208 . $chars[rand(@chars)]
209 . '.downlaod'
210 unless $filename;
211 0 $self->store(join '/', $dir, $filename);
212 0 $self->log("info",
213 "$url was downloaded to "
214 . join('/', $dir, $filename)
215 . " successfully");
216 0 $self->back;
217 }
218 elsif ($url) {
219 0 ▶ return unless $self->get($url);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get($url) 0 0 0
220 0 my @chars = ('a' .. 'z', 'A' .. 'Z', 0 .. 9);
221 0 my $filename = $self->worker->response->filename;
222 0 ▶ $filename =
Branch: 1 branch
BranchTrueFalseTotal
unless $filename 0 0 0
223 $chars[rand(@chars)]
224 . $chars[rand(@chars)]
225 . $chars[rand(@chars)]
226 . $chars[rand(@chars)]
227 . $chars[rand(@chars)]
228 . $chars[rand(@chars)]
229 . '.downlaod'
230 unless $filename;
231 0 $dir = $url->path;
232 0 $dir =~ s/^\///g;
233 0 $dir =~ s/\/$filename$//;
234 0 ▶ File::Util->new->make_dir($dir) unless -d $dir || !$dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir or not $dir 0 0 0
Condition0%0/3
-d $dir or not $dir
l!l&&r!l&&!r
000
MC/DC0%0/2
-d $dir or not $dir
-d $dir not $dir
Truth table0%0/3
A or not(B)
A= -d $dirB= not $dir
ABresult
1X1
011
000
235 0 $self->store(join '/', $dir, $filename);
236 0 $self->log("info",
237 "$url was downloaded to "
238 . join('/', $dir, $filename)
239 . " successfully");
240 0 $self->back;
241 }
242 else {
243 0 croak(
244 "To download data from a URI you must supply at least a valid URI "
245 . "and download directory path");
246 }
247  
248 0 ▶ $self->stash->{history} = [] unless defined $self->stash->{history};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->stash->{"history"} 0 0 0
249 0 push @{$self->stash->{history}}, $url;
250  
251 0 $self->worker->{params} = {};
252 $self->worker->{params} =
253 0 {map { ($_ => $url->query_form($_)) } $url->query_form};
254  
255 0 sleep $self->pause;
256  
257 0 return $self;
258 }
259  
260 0 ▶sub dumper {
Subroutine dumper: not called CC=1 SCAR=6.9
Pod undocumented
261 0 shift;
262 0 return Data::Dumper::Dumper(@_);
263 }
264  
265 0 ▶sub form {
Subroutine form: not called CC=4 SCAR=30.0
Pod undocumented
266 0 my $self = shift;
267 0 my $url = URI->new($self->url);
268  
269 # TODO: need to figure out how to determine the form action before submit
270  
271 # specify user-agent
272 0 ▶ $self->worker->add_header("User-Agent" => $self->user_agent->name)
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->user_agent->name 0 0 0
273 if defined $self->user_agent->name;
274  
275 # set html response
276 0 $self->content('');
277 0 my @args = @_;
278 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
279 0 $self->content($self->worker->submit_form(@args));
280 0 };
281 0 ▶ if ($self->content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->content) { } 0 0 0
282  
283 # access control
284 0 ▶ if ($self->control->is_allowed($self->content)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->control->is_allowed($self->content)) { } 0 0 0
285 0 $self->log("warn", "$url was not fetched, the url is prohibited");
286 0 return 0;
287 }
288 else {
289 0 $self->log("info", "form posted from $url successfully", @_);
290 }
291 }
292 else {
293 0 $self->log("error", "error POSTing form from $url", @_);
294 }
295  
296 #$self->stash->{history} = [] unless defined $self->stash->{history};
297 #push @{$self->stash->{history}}, $url;
298  
299 $self->worker->{cookie_jar}->scan(
300 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
301  
302 0 my ($version, $key, $val, $path,
303 $domain, $port, $path_spec, $secure,
304 $expires, $discard, $hash
305 ) = @_;
306  
307 0 ▶ $self->session->stash('cookies' => {})
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->session->stash("cookies") 0 0 0
308 unless defined $self->session->stash('cookies');
309  
310 0 $self->session->stash->{'cookies'}->{$domain}->{$key} = {
311 version => $version,
312 key => $key,
313 val => $val,
314 path => $path,
315 domain => $domain,
316 port => $port,
317 path_spec => $path_spec,
318 secure => $secure,
319 expires => $expires,
320 discard => $discard,
321 hash => $hash
322 };
323  
324 0 $self->session->write;
325  
326 }
327 0 );
328  
329 0 $self->worker->{params} = {};
330 $self->worker->{params} =
331 0 {map { ($_ => $url->query_form($_)) } $url->query_form};
332  
333 0 sleep $self->pause;
334  
335 0 return $self;
336 }
337  
338 0 ▶sub get {
Subroutine get: not called CC=6 SCAR=37.4
Pod undocumented
339 0 my $self = shift;
340 0 my $url = URI->new(@_);
341  
342 # specify user-agent
343 0 ▶ $self->worker->add_header("User-Agent" => $self->user_agent->name)
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->user_agent->name 0 0 0
344 if defined $self->user_agent->name;
345  
346 # set html response
347 0 $self->content('');
348 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
349 0 $self->content($self->worker->get($url));
350 0 };
351 0 ▶ if ($self->content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->content) { } 0 0 0
352  
353 # access control
354 0 ▶ if (!$self->control->is_allowed($self->content)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->control->is_allowed($self->content)) { } 0 0 0
355 0 $self->log("warn", "$url was not fetched, the url is prohibited");
356 0 return 0;
357 }
358 else {
359 0 $self->log("info", "$url was fetched successfully");
360 }
361 }
362 else {
363 0 $self->log("error", "error GETing $url");
364 }
365  
366 0 ▶ $self->stash->{history} = [] unless defined $self->stash->{history};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->stash->{"history"} 0 0 0
367 0 push @{$self->stash->{history}}, $url;
368 $self->worker->{cookie_jar}->scan(
369 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
370  
371 0 my ($version, $key, $val, $path,
372 $domain, $port, $path_spec, $secure,
373 $expires, $discard, $hash
374 ) = @_;
375  
376 0 ▶ $self->session->stash('cookies' => {})
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->session->stash("cookies") 0 0 0
377 unless defined $self->session->stash('cookies');
378  
379 0 $self->session->stash->{'cookies'}->{$domain}->{$key} = {
380 version => $version,
381 key => $key,
382 val => $val,
383 path => $path,
384 domain => $domain,
385 port => $port,
386 path_spec => $path_spec,
387 secure => $secure,
388 expires => $expires,
389 discard => $discard,
390 hash => $hash
391 };
392  
393 0 $self->session->write;
394  
395 }
396 0 ▶ ) if $self->session->file;
Branch: 1 branch
BranchTrueFalseTotal
if $self->session->file 0 0 0
397  
398 0 $self->worker->{params} = {};
399 $self->worker->{params} =
400 0 {map { ($_ => $url->query_form($_)) } $url->query_form};
401  
402 0 sleep $self->pause;
403  
404 0 return $self;
405 }
406  
407 0 ▶sub page_data {
Subroutine page_data: not called CC=4 SCAR=30.0
Pod undocumented
408 0 my $self = shift;
409 0 my ($data, @args);
410  
411 0 ▶ if (scalar(@_) % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@_) % 2) { } 0 0 0
412 0 $data = shift;
413 0 @args = @_;
414 }
415 else {
416 0 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 0 0 0
417 0 @args = @_;
418 }
419 else {
420 0 $data = shift;
421 }
422 }
423  
424 0 ▶ if ($data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data) 0 0 0
425 0 $self->worker->update_html($data);
426 }
427  
428 0 return $self->worker->content(@args);
429 }
430  
431 0 ▶sub page_content_type {
Subroutine page_content_type: not called CC=1 SCAR=6.9
Pod undocumented
432 0 return shift->worker->content_type;
433 }
434  
435 0 ▶sub page_ishtml {
Subroutine page_ishtml: not called CC=1 SCAR=6.9
Pod undocumented
436 0 return shift->worker->is_html;
437 }
438  
439 0 ▶sub page_loaded {
Subroutine page_loaded: not called CC=1 SCAR=6.9
Pod undocumented
440 0 return shift->worker->success;
441 }
442  
443 0 ▶sub page_match {
Subroutine page_match: not called CC=22 SCAR=62.3
Pod undocumented
444 0 my $self = shift;
445 0 my $pattern = shift;
446 0 ▶ my $url = shift || $self->url;
Condition0%0/3
shift() || $self->url
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->url
shift() $self->url
Truth table0%0/3
A || B
A= shift()B= $self->url
ABresult
1X1
011
000
447 0 $url = URI->new($url);
448 0 ▶ my $options = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
449  
450 0 ▶ croak("route can't be defined without a valid URL pattern")
Branch: 1 branch
BranchTrueFalseTotal
unless $pattern 0 0 0
451 unless $pattern;
452  
453 0 my $route = $self->stash->{patterns}->{$pattern};
454  
455 # does route definition already exist?
456 0 ▶ unless (keys %{$route}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %{$route;}) 0 0 0
457  
458 0 $route->{on_match} = $options->{on_match};
459  
460 # define options
461 0 ▶ if (my $host = $options->{host}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $host = $options->{"host"}) 0 0 0
462 0 $route->{host} = $host;
463 0 ▶ $route->{host_re} = ref $host ? $host : qr(^\Q$host\E$);
Branch: 1 branch
BranchTrueFalseTotal
ref $host ? : 0 0 0
464 }
465  
466 0 $route->{pattern} = $pattern;
467  
468 # compile pattern
469 0 my @capture;
470 0 $route->{pattern_re} = do {
471 0 ▶ if (ref $pattern) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pattern) { } 0 0 0
472 0 $route->{_regexp_capture} = 1;
473 0 $pattern;
474 }
475 else {
476 0 $pattern =~ s!
477 \{((?:\{[0-9,]+\}|[^{}]+)+)\} | # /blog/{year:\d{4}}
478 :([A-Za-z0-9_]+) | # /blog/:year
479 (\*) | # /blog/*/*
480 ([^{:*]+) # normal string
481 !
482 0 ▶ if ($1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($1) { } 0 0 0
elsif ($2) { } 0 0 0
elsif ($3) { } 0 0 0
483 0 my ($name, $pattern) = split /:/, $1, 2;
484 0 push @capture, $name;
485 0 ▶ $pattern ? "($pattern)" : "([^/]+)";
Branch: 1 branch
BranchTrueFalseTotal
$pattern ? : 0 0 0
486 } elsif ($2) {
487 0 push @capture, $2;
488 0 "([^/]+)";
489 } elsif ($3) {
490 0 push @capture, '__splat__';
491 0 "(.+)";
492 } else {
493 0 quotemeta($4);
494 }
495 !gex;
496 0 qr{^$pattern$};
497 }
498 };
499 0 $route->{capture} = \@capture;
500 0 $self->stash->{patterns}->{$route->{pattern}} = $route;
501 }
502  
503 # match
504 0 ▶ if ($route->{host_re}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($route->{"host_re"}) 0 0 0
505 0 ▶ unless ($url->host =~ $route->{host_re}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url->host =~ /$route->{"host_re"}/) 0 0 0
506 0 return 0;
507 }
508 }
509  
510 0 ▶ if (my @captured = ($url->path =~ $route->{pattern_re})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@captured) = $url->path =~ /$route->{"pattern_re"}/) 0 0 0
511 0 my %args;
512 my @splat;
513 0 ▶ if ($route->{_regexp_capture}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($route->{'_regexp_capture'}) { } 0 0 0
514 0 push @splat, @captured;
515 }
516 else {
517 0 for my $i (0 .. @{$route->{capture}} - 1) {
518 0 ▶ if ($route->{capture}->[$i] eq '__splat__') {
Branch: 1 branch
BranchTrueFalseTotal
if ($route->{'capture'}[$i] eq '__splat__') { } 0 0 0
519 0 push @splat, $captured[$i];
520 }
521 else {
522 0 $args{$route->{capture}->[$i]} = $captured[$i];
523 }
524 }
525 }
526 my $match = +{
527 0 ▶ (label => $route->{label}),
Branch: 1 branch
BranchTrueFalseTotal
@splat ? : 0 0 0
528 %args,
529 (@splat ? (splat => \@splat) : ())
530 };
531 0 ▶ if ($route->{on_match}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($route->{"on_match"}) 0 0 0
532 0 my $ret = $route->{on_match}->($self, $match);
533 0 ▶ return 0 unless $ret;
Branch: 1 branch
BranchTrueFalseTotal
unless $ret 0 0 0
534 }
535 0 $match->{params} = {%args};
536 0 ▶ $match->{params}->{splat} = \@splat if @splat;
Branch: 1 branch
BranchTrueFalseTotal
if @splat 0 0 0
537  
538 0 return $match;
539 }
540  
541 0 return 0;
542 }
543  
544 0 ▶sub page_reload {
Subroutine page_reload: not called CC=4 SCAR=30.0
Pod undocumented
545 0 my $self = shift;
546  
547 # specify user-agent
548 0 ▶ $self->worker->add_header("User-Agent" => $self->user_agent->name)
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->user_agent->name 0 0 0
549 if defined $self->user_agent->name;
550  
551 # set html response
552 0 $self->content('');
553 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
554 0 $self->content($self->worker->reload);
555 0 };
556 0 ▶ $self->content
Branch: 1 branch
BranchTrueFalseTotal
$self->content ? : 0 0 0
557 ? $self->log("info", "page reloaded successfully")
558 : $self->log("error", "error reloading page");
559  
560 0 my $url = $self->url;
561  
562 0 ▶ $self->stash->{history} = [] unless defined $self->stash->{history};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->stash->{"history"} 0 0 0
563 0 push @{$self->stash->{history}}, $url;
564 $self->worker->{cookie_jar}->scan(
565 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
566  
567 0 my ($version, $key, $val, $path,
568 $domain, $port, $path_spec, $secure,
569 $expires, $discard, $hash
570 ) = @_;
571  
572 0 ▶ $self->session->stash('cookies' => {})
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->session->stash("cookies") 0 0 0
573 unless defined $self->session->stash('cookies');
574  
575 0 $self->session->stash->{'cookies'}->{$domain}->{$key} = {
576 version => $version,
577 key => $key,
578 val => $val,
579 path => $path,
580 domain => $domain,
581 port => $port,
582 path_spec => $path_spec,
583 secure => $secure,
584 expires => $expires,
585 discard => $discard,
586 hash => $hash
587 };
588  
589 0 $self->session->write;
590  
591 }
592 0 );
593  
594 0 return $self;
595 }
596  
597 0 ▶sub page_status {
Subroutine page_status: not called CC=1 SCAR=6.9
Pod undocumented
598 0 return shift->worker->status;
599 }
600  
601 0 ▶sub page_text {
Subroutine page_text: not called CC=1 SCAR=6.9
Pod undocumented
602 0 return shift->page_data(format => 'text');
603 }
604  
605 0 ▶sub page_title {
Subroutine page_title: not called CC=1 SCAR=6.9
Pod undocumented
606 0 return shift->worker->title;
607 }
608  
609 0 ▶sub plugin {
Subroutine plugin: not called CC=2 SCAR=17.9
Pod undocumented
610 0 my ($self, @plugins) = @_;
611 0 foreach (@plugins) {
612 0 with $self->plugins->load_plugin($_);
613 }
614 0 return $self;
615 }
616  
617 0 ▶sub post {
Subroutine post: not called CC=6 SCAR=37.4
Pod undocumented
618 0 my $self = shift;
619 0 my $url = URI->new($_[0]);
620  
621 # access control
622 0 ▶ unless ($self->control->is_allowed($url)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->control->is_allowed($url)) 0 0 0
623 0 $self->log("warn", "$url was not fetched, the url is prohibited");
624 0 return 0;
625 }
626  
627 # specify user-agent
628 0 ▶ $self->worker->add_header("User-Agent" => $self->user_agent->name)
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->user_agent->name 0 0 0
629 if defined $self->user_agent->name;
630  
631 # set html response
632 0 $self->content('');
633 0 my @args = @_;
634 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
635 0 $self->content($self->worker->post(@args));
636 0 };
637 0 ▶ if ($self->content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->content) { } 0 0 0
638  
639 # access control
640 0 ▶ if ($self->control->is_allowed($self->content)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->control->is_allowed($self->content)) { } 0 0 0
641 0 $self->log("warn", "$url was not fetched, the url is prohibited");
642 0 return 0;
643 }
644 else {
645 0 $self->log("info", "posted data to $_[0] successfully", @_);
646 }
647 }
648 else {
649 0 $self->log("error", "error POSTing data to $_[0]", @_);
650 }
651  
652 0 ▶ $self->stash->{history} = [] unless defined $self->stash->{history};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->stash->{"history"} 0 0 0
653 0 push @{$self->stash->{history}}, $url;
654 $self->worker->{cookie_jar}->scan(
655 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
656  
657 0 my ($version, $key, $val, $path,
658 $domain, $port, $path_spec, $secure,
659 $expires, $discard, $hash
660 ) = @_;
661  
662 0 ▶ $self->session->stash('cookies' => {})
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->session->stash("cookies") 0 0 0
663 unless defined $self->session->stash('cookies');
664  
665 0 $self->session->stash->{'cookies'}->{$domain}->{$key} = {
666 version => $version,
667 key => $key,
668 val => $val,
669 path => $path,
670 domain => $domain,
671 port => $port,
672 path_spec => $path_spec,
673 secure => $secure,
674 expires => $expires,
675 discard => $discard,
676 hash => $hash
677 };
678  
679 0 $self->session->write;
680  
681 }
682 0 );
683  
684 0 $self->worker->{params} = {};
685 $self->worker->{params} =
686 0 {map { ($_ => $url->query_form($_)) } $url->query_form};
687  
688 0 sleep $self->pause;
689  
690 0 return $self;
691 }
692  
693 0 ▶sub proxy {
Subroutine proxy: not called CC=1 SCAR=6.9
Pod undocumented
694 0 my $self = shift;
695 0 my $proxy = pop @_;
696 0 my @protocol = @_;
697 0 $self->worker->proxy([@protocol], $proxy);
698 0 $self->log("info", "Set proxy $proxy using protocol(s) " . join ' and ',
699 @protocol);
700 0 return $self;
701 }
702  
703 0 ▶sub request_denied {
Subroutine request_denied: not called CC=2 SCAR=17.9
Pod undocumented
704 0 my $self = shift;
705 0 my ($last) = reverse @{$self->stash->{history}};
706 0 ▶ return 1 if ($self->url ne $last);
Branch: 1 branch
BranchTrueFalseTotal
if $self->url ne $last 0 0 0
707 }
708  
709 0 ▶sub select {
Subroutine select: not called CC=2 SCAR=17.9
Pod undocumented
710 0 my ($self, $selector, $html) = @_;
711 0 my $parser = Scrappy::Scraper::Parser->new;
712 0 ▶ $parser->html($html ? $html : $self->content);
Branch: 1 branch
BranchTrueFalseTotal
$html ? : 0 0 0
713 0 return $parser->select($selector);
714 }
715  
716 0 ▶sub log {
Subroutine log: not called CC=5 SCAR=34.0
Pod undocumented
717 0 my $self = shift;
718 0 my $type = shift;
719 0 my @args = @_;
720  
721 0 ▶ if ($self->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) { } 0 0 0
722 0 ▶ if ($type eq 'info') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'info') { } 0 0 0
elsif ($type eq 'warn') { } 0 0 0
elsif ($type eq 'error') { } 0 0 0
723 0 $self->logger->info(@args);
724 }
725 elsif ($type eq 'warn') {
726 0 $self->logger->warn(@args);
727 }
728 elsif ($type eq 'error') {
729 0 $self->logger->error(@args);
730 }
731 else {
732 0 warn $type;
733 0 $self->logger->event($type, @args);
734 }
735  
736 0 return 1;
737 }
738 else {
739 0 return 0;
740 }
741 }
742  
743 0 ▶sub pause {
Subroutine pause: not called CC=9 SCAR=45.0
Pod undocumented
744 0 my $self = shift;
745 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 0 0 0
746 0 ▶ if ($_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1]) { } 0 0 0
747 0 ▶ my @range = (($_[0] < $_[1] ? $_[0] : 0) .. $_[1]);
Branch: 1 branch
BranchTrueFalseTotal
$_[0] < $_[1] ? : 0 0 0
748 0 $self->worker->{pause_range} = [$_[0], $_[1]];
749 0 $self->worker->{pause} = $range[rand(@range)];
750 }
751 else {
752 0 $self->worker->{pause} = $_[0];
753 0 ▶ $self->worker->{pause_range} = [0, 0] unless $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 0 0 0
754 }
755 }
756 else {
757 0 ▶ my $interval = $self->worker->{pause} || 0;
Condition0%0/2
$self->worker->{'pause'} || 0
l!l
00
MC/DC0%0/1
$self->worker->{'pause'} || 0
$self->worker->{'pause'}
Truth table0%0/2
A
A= $self->worker->{'pause'}
Aresult
11
00
758  
759 # select the next random pause value from the range
760 0 ▶ if (defined $self->worker->{pause_range}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->worker->{"pause_range"}) 0 0 0
761 0 my @range = @{$self->worker->{pause_range}};
762 0 ▶ $self->pause(@range) if @range == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @range == 2 0 0 0
763 }
764  
765 0 ▶ $self->log("info", "processing was halted for $interval seconds")
Branch: 1 branch
BranchTrueFalseTotal
if $interval > 0 0 0 0
766 if $interval > 0;
767 0 return $interval;
768 }
769 }
770  
771 0 ▶sub response {
Subroutine response: not called CC=1 SCAR=6.9
Pod undocumented
772 0 return shift->worker->response;
773 }
774  
775 0 ▶sub stash {
Subroutine stash: not called CC=7 SCAR=40.3
Pod undocumented
776 0 my $self = shift;
777 0 ▶ $self->{stash} = {} unless defined $self->{stash};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"stash"} 0 0 0
778  
779 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
780 0 ▶ my $stash = @_ > 1 ? {@_} : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
781 0 ▶ if ($stash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stash) 0 0 0
782 0 ▶ if (ref $stash eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $stash eq 'HASH') { } 0 0 0
783 0 $self->{stash}->{$_} = $stash->{$_} for keys %{$stash};
784 }
785 else {
786 0 return $self->{stash}->{$stash};
787 }
788 }
789 }
790  
791 0 return $self->{stash};
792 }
793  
794 0 ▶sub store {
Subroutine store: not called CC=7 SCAR=40.3
Pod undocumented
795  
796 # return shift->worker->save_content(@_);
797 # oh no i didnt just rewrite www:mech save_content, oh yes i did
798 # ... in hope to avoid content encoding issues
799  
800 0 my $self = shift;
801 0 my $filename = shift;
802  
803 0 ▶ open(my $fh, '>', $filename)
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $filename or $self->worker->die("Unable to create ${filename}: $!") 0 0 0
804 or $self->worker->die("Unable to create $filename: $!");
805  
806 0 ▶ if ( $self->worker->content_type =~ m{^text/}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->worker->content_type =~ m[^text/] or $self->worker->content_type =~ m[^application/(atom|css|javascript|json|rss|xml)]) { } 0 0 0
Condition0%0/3
$self->worker->content_type =~ m[^text/] or $self->worker->content_type =~ m[^application/(atom|css|javascript|json|rss|xml)]
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->worker->content_type =~ m[^text/] or $self->worker->content_type =~ m[^application/(atom|css|javascript|json|rss|xml)]
$self->worker->content_type =~ m[^text/] $self->worker->content_type =~ m[^application/(atom|css|javascript|json|rss|xml)]
Truth table0%0/3
A or B
A= $self->worker->content_type =~ m[^text/]B= $self->worker->content_type =~ m[^application/(atom|css|javascript|json|rss|xml)]
ABresult
1X1
011
000
807 || $self->worker->content_type
808 =~ m{^application/(atom|css|javascript|json|rss|xml)})
809 {
810  
811 # text
812 0 $self->worker->response->decode;
813 0 ▶ print {$fh} $self->worker->response->content
Branch: 1 branch
BranchTrueFalseTotal
print {$fh;} $self->worker->response->content or $self->worker->die("Unable to write to ${filename}: $!") 0 0 0
814 or $self->worker->die("Unable to write to $filename: $!");
815 }
816 else {
817  
818 # binary
819 0 binmode $fh;
820 0 ▶ print {$fh} $self->worker->response->content
Branch: 1 branch
BranchTrueFalseTotal
print {$fh;} $self->worker->response->content or $self->worker->die("Unable to write to ${filename}: $!") 0 0 0
821 or $self->worker->die("Unable to write to $filename: $!");
822 }
823  
824 0 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or $self->worker->die("Unable to close ${filename}: $!") 0 0 0
825 or $self->worker->die("Unable to close $filename: $!");
826  
827 0 return $self;
828 }
829  
830 0 ▶sub url {
Subroutine url: not called CC=2 SCAR=17.9
Pod undocumented
831 0 ▶ return $_[0]->worker->uri
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->content 0 0 0
832 if $_[0]->content;
833 }
834  
835 10 1;
« Scrappy/Logger.pm ↑ Summary Scrappy/Scraper/Control.pm »