lib/WWW/Crawler/Mojo/ScraperUtil.pm

filter: Statementstmt 94.2% 114 / 121 Branchbran 88.7% 55 / 62 Conditioncond 86.6% 26 / 30 MC/DCmcdc 77.7% 14 / 18 Subroutinesub 86.4% 32 / 37 Podpod 100% 7 / 7 totaltotal 90.1% 248 / 275 CC 47 SCAR 18.9
CC
47
Cov
92%
__ANON__
19.1
guess_encoding
5.1
resolve_href
5.0
CRAP 6.6
↑ Summary blib/lib/WWW/Crawler/Mojo/Queue/MySQL.pm »
1 package WWW::Crawler::Mojo::ScraperUtil;
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 -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Encode qw(find_encoding);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 our @EXPORT_OK = qw(collect_urls_css html_handler_presets reduce_html_handlers
9 guess_encoding encoder decoded_body resolve_href);
10  
11 10 my $charset_re = qr{\bcharset\s*=\s*['"]?([a-zA-Z0-9_\-]+)['"]?}i;
12  
13 6 ▶sub collect_urls_css {
Subroutine collect_urls_css: called CC=3 SCAR=11.1
Pod documented
14 6 ▶ map { s/^(['"])// && s/$1$//; $_ } (shift || '') =~ m{url\((.+?)\)}ig;
Branch: 1 branch
BranchTrueFalseTotal
s/^(['"])//u and s/$1$//u 4 8 12
Condition50%1/2
shift() || ''
l!l
60
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
15 }
16  
17 24 ▶sub decoded_body {
Subroutine decoded_body: called CC=1 SCAR=0.0
Pod documented
18 24 my $res = shift;
19 24 return encoder(guess_encoding($res))->decode($res->body);
20 }
21  
22 29 ▶sub encoder {
Subroutine encoder: called CC=4 SCAR=14.0
Pod documented
23 29 ▶ for (shift || 'utf-8', 'utf-8') {
Condition100%2/2
shift() || 'utf-8'
l!l
1118
MC/DC100%1/1
shift() || 'utf-8'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
24 29 ▶ if (my $enc = find_encoding($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $enc = &find_encoding($_)) 29 0 29
25 29 return $enc;
26 }
27 }
28 }
29  
30 28 ▶sub guess_encoding {
Subroutine guess_encoding: called CC=5 SCAR=16.2
Pod documented
31 28 my $res = shift;
32 28 my $type = $res->headers->content_type;
33 28 ▶ return unless ($type);
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 28 28
34 28 my $charset = ($type =~ $charset_re)[0];
35 28 ▶ return $charset if ($charset);
Branch: 1 branch
BranchTrueFalseTotal
if $charset 9 19 28
36 19 ▶ return _guess_encoding_html($res->body) if ($type =~ qr{text/(html|xml)});
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ qr"text/(html|xml)"u 18 1 19
37 1 ▶ return _guess_encoding_css($res->body) if ($type =~ qr{text/css});
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ qr"text/css"u 1 0 1
38 }
39  
40 17 ▶sub html_handler_presets {
Subroutine html_handler_presets: called CC=1 SCAR=0.0
Pod documented
41 return {
42 12 ▶ 'script[src]' => sub { $_[0]->{src} },
Subroutine __ANON__: called CC=1 SCAR=0.0
43 14 ▶ 'link[href]' => sub { $_[0]->{href} },
Subroutine __ANON__: called CC=1 SCAR=0.0
44 38 ▶ 'a[href]' => sub { $_[0]->{href} },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 1 ▶ 'img[src]' => sub { $_[0]->{src} },
Subroutine __ANON__: called CC=1 SCAR=0.0
46 4 ▶ 'area' => sub { $_[0]->{href}, $_[0]->{ping} },
Subroutine __ANON__: called CC=1 SCAR=0.0
47 0 ▶ 'embed[src]' => sub { $_[0]->{src} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 0 ▶ 'frame[src]' => sub { $_[0]->{src} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 0 ▶ 'iframe[src]' => sub { $_[0]->{src} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 0 ▶ 'input[src]' => sub { $_[0]->{src} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 0 ▶ 'object[data]' => sub { $_[0]->{data} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 31 ▶ 'form' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
53 31 my $dom = shift;
54 31 my (%seed, $submit);
55  
56 $dom->find("[name],[type='submit'],[type='image']")->each(
57 81 ▶ sub {
Subroutine __ANON__: called CC=19 SCAR=29.5
58 81 my $e = shift;
59 81 ▶ $seed{my $name = $e->{name}} ||= [] if $e->{name};
Branch: 1 branch
BranchTrueFalseTotal
if $e->{"name"} 57 24 81
Condition100%2/2
$seed{my $name = $e->{"name"}} ||= []
l!l
1542
MC/DC100%1/1
$seed{my $name = $e->{"name"}} ||= []
$seed{my $name = $e->{"name"}}
Truth table100%2/2
A
A= $seed{my $name = $e->{"name"}}
Aresult
11
00
60  
61 81 ▶ if ($e->tag eq 'select' && $name) {
Branch: 2 branches
BranchTrueFalseTotal
if ($e->tag eq 'select' and $name) { } 5 76 81
elsif ($e->tag eq 'textarea') { } 5 71 76
Condition100%3/3
$e->tag eq "select" and $name
!ll&&!rl&&r
7425
MC/DC100%2/2
$e->tag eq "select" and $name
$e->tag eq "select" $name
Truth table100%3/3
A and B
A= $e->tag eq "select"B= $name
ABresult
0X0
100
111
62 5 my $found = 0;
63 5 ▶ if (exists $e->{multiple}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $e->{'multiple'}) { } 1 4 5
elsif (my $opts = $e->at('option[selected]')) { } 2 2 4
64 $e->find('option[selected]')->each(
65 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
66 2 push(@{$seed{$name}}, shift->{value});
67 2 $found++;
68 }
69 1 );
70 }
71 elsif (my $opts = $e->at('option[selected]')) {
72 2 push(@{$seed{$name}}, $opts->{value});
73 2 $found++;
74 }
75 5 ▶ if (!$found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 2 3 5
76 $e->find('option:nth-child(1)')->each(
77 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
78 2 push(@{$seed{$name}}, shift->{value});
79 }
80 2 );
81 }
82 }
83 elsif ($e->tag eq 'textarea') {
84 5 push(@{$seed{$name}}, $e->text);
85 }
86  
87 81 ▶ return unless (my $type = $e->{type});
Branch: 1 branch
BranchTrueFalseTotal
unless my $type = $e->{"type"} 12 69 81
88  
89 69 ▶ if (!$submit && grep { $_ eq $type } qw{submit image}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $submit and grep {$_ eq $type;} "submit", "image") 27 42 69
Condition100%3/3
not $submit and grep {$_ eq $type;} "submit", "image"
!ll&&!rl&&r
83427
MC/DC100%2/2
not $submit and grep {$_ eq $type;} "submit", "image"
not $submit grep {$_ eq $type;} "submit", "image"
Truth table100%3/3
not(A) and B
A= not $submitB= grep {$_ eq $type;} "submit", "image"
ABresult
0X0
100
111
90 27 $submit = 1;
91 27 ▶ push(@{$seed{$name}}, $e->{value}) if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 6 21 27
92 }
93 69 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) 47 22 69
94 47 ▶ if (grep { $_ eq $type } qw{text hidden number password date}) {
Branch: 3 branches
BranchTrueFalseTotal
if (grep {$_ eq $type;} 'text', 'hidden', 'number', 'password', 'date') { } 23 24 47
elsif (grep {$_ eq $type;} 'checkbox') { } 0 24 24
elsif (grep {$_ eq $type;} 'radio') { } 9 15 24
95 23 push(@{$seed{$name}}, $e->{value});
96 }
97 24 elsif (grep { $_ eq $type } qw{checkbox}) {
98 0 ▶ push(@{$seed{$name}}, $e->{value}) if (exists $e->{checked});
Branch: 1 branch
BranchTrueFalseTotal
if exists $e->{"checked"} 0 0 0
99 }
100 24 elsif (grep { $_ eq $type } qw{radio}) {
101 9 ▶ push(@{$seed{$name}}, $e->{value}) if (exists $e->{checked});
Branch: 1 branch
BranchTrueFalseTotal
if exists $e->{"checked"} 3 6 9
102 }
103 }
104 }
105 31 );
106  
107 return [
108 $dom->{action} || '',
109 31 ▶ uc($dom->{method} || 'GET'),
Condition100%4/4
$dom->{'action'} || ''
l!l
301
$dom->{'method'} || 'GET'
l!l
292
MC/DC100%1/1
$dom->{'action'} || ''
$dom->{'action'}
MC/DC100%1/1
$dom->{'method'} || 'GET'
$dom->{'method'}
Truth table100%2/2
A
A= $dom->{'action'}
Aresult
11
00
Truth table100%2/2
A
A= $dom->{'method'}
Aresult
11
00
110 Mojo::Parameters->new(%seed)
111 ];
112 },
113 2 ▶ 'meta[content]' => sub {
Subroutine __ANON__: called CC=4 SCAR=14.3
114 return $1
115 if ($_[0] =~ qr{http\-equiv="?Refresh"?}i
116 2 ▶ && (($_[0]->{content} || '') =~ qr{URL=(.+)}i)[0]);
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ qr/http\-equiv="?Refresh"?/iu and (($_[0]{"content"} || "") =~ qr/URL=(.+)/iu)[0] 1 1 2
Condition60%3/5
$_[0] =~ qr/http\-equiv="?Refresh"?/iu and (($_[0]{"content"} || "") =~ qr/URL=(.+)/iu)[0]
!ll&&!rl&&r
101
$_[0]{'content'} || ''
l!l
10
MC/DC50%1/2
$_[0] =~ qr/http\-equiv="?Refresh"?/iu and (($_[0]{"content"} || "") =~ qr/URL=(.+)/iu)[0]
$_[0] =~ qr/http\-equiv="?Refresh"?/iu (($_[0]{"content"} || "") =~ qr/URL=(.+)/iu)[0]
MC/DC0%0/1
$_[0]{'content'} || ''
$_[0]{'content'}
Truth table66%2/3
A and B
A= $_[0] =~ qr/http\-equiv="?Refresh"?/iuB= (($_[0]{"content"} || "") =~ qr/URL=(.+)/iu)[0]
ABresult
0X0
100
111
Truth table50%1/2
A
A= $_[0]{'content'}
Aresult
11
00
117 1 return;
118 },
119 1 ▶ 'style' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
120 1 collect_urls_css(shift->content);
121 },
122 3 ▶ '[style]' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
123 3 collect_urls_css(shift->{style});
124 },
125 1 ▶ 'urlset[xmlns^=http://www.sitemaps.org/schemas/sitemap/]' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
126 1 ▶ @{$_->find('url loc')->map(sub { $_->content })->to_array};
Subroutine __ANON__: called CC=1 SCAR=0.0
127 }
128 17 };
129 }
130  
131 23 ▶sub reduce_html_handlers {
Subroutine reduce_html_handlers: called CC=5 SCAR=16.1
Pod documented
132 23 my $handlers = $_[0];
133 23 ▶ my $contexts = ref $_[1] ? $_[1] : [$_[1]];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 1 22 23
134 23 my $ret;
135 23 for my $sel (keys %$handlers) {
136 345 my $cb = $handlers->{$sel};
137 345 for my $cont (@$contexts) {
138 83 ▶ $ret->{($cont ? $cont . ' ' : '') . $sel} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
139 83 ▶ return if ($_[0]->xml && _wrong_dom_detection($_[0]));
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->xml and _wrong_dom_detection($_[0]) 1 82 83
Condition100%3/3
$_[0]->xml and _wrong_dom_detection($_[0])
!ll&&!rl&&r
8111
MC/DC100%2/2
$_[0]->xml and _wrong_dom_detection($_[0])
$_[0]->xml _wrong_dom_detection($_[0])
Truth table100%3/3
A and B
A= $_[0]->xmlB= _wrong_dom_detection($_[0])
ABresult
0X0
100
111
140 82 return $cb->($_[0]);
141 }
142 360 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
$cont ? : 45 315 360
143 }
144 23 return $ret;
145 }
146  
147 174 ▶sub resolve_href {
Subroutine resolve_href: called CC=5 SCAR=16.1
Pod documented
148 174 my ($base, $href) = @_;
149 174 $href =~ s{^\s|\s$|\n}{}g;
150 174 ▶ $href = ref $href ? $href : Mojo::URL->new($href);
Branch: 1 branch
BranchTrueFalseTotal
ref $href ? : 0 174 174
151 174 ▶ $base = ref $base ? $base : Mojo::URL->new($base);
Branch: 1 branch
BranchTrueFalseTotal
ref $base ? : 168 6 174
152 174 my $abs = $href->fragment(undef)->to_abs($base);
153 174 my $path_parts = $abs->path->parts;
154 174 ▶ shift @{$path_parts} while (@$path_parts && $path_parts->[0] eq '..');
Branch: 1 branch
BranchTrueFalseTotal
@$path_parts and $path_parts->[0] eq ".." and do { shift @{$path_parts;} } 6 174 180
Condition100%3/3
@$path_parts and $path_parts->[0] eq ".."
!ll&&!rl&&r
191556
MC/DC100%2/2
@$path_parts and $path_parts->[0] eq ".."
@$path_parts $path_parts->[0] eq ".."
Truth table100%3/3
A and B
A= @$path_partsB= $path_parts->[0] eq ".."
ABresult
0X0
100
111
155 174 return $abs;
156 }
157  
158 1 ▶sub _guess_encoding_css {
Subroutine _guess_encoding_css: called CC=1 SCAR=0.0
159 1 return (shift =~ qr{^\s*\@charset ['"](.+?)['"];}is)[0];
160 }
161  
162 18 ▶sub _guess_encoding_html {
Subroutine _guess_encoding_html: called CC=2 SCAR=6.9
163 18 ▶ my $head = (shift =~ qr{<head>(.+)</head>}is)[0] or return;
Branch: 1 branch
BranchTrueFalseTotal
my $head = ((shift()) =~ qr"<head>(.+)</head>"isu)[0] or return 13 5 18
164 13 my $charset;
165 Mojo::DOM->new($head)->find('meta[http\-equiv=Content-Type]')->each(
166 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
167 1 $charset = (shift->{content} =~ $charset_re)[0];
168 }
169 13 );
170 13 return $charset;
171 }
172  
173 2 ▶sub _wrong_dom_detection {
Subroutine _wrong_dom_detection: called CC=4 SCAR=13.9
174 2 my $dom = shift;
175 2 ▶ while ($dom = $dom->parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dom = $dom->parent) 2 1 3
176 2 ▶ return 1 if ($dom->tag && $dom->tag eq 'script');
Branch: 1 branch
BranchTrueFalseTotal
if $dom->tag and $dom->tag eq "script" 1 1 2
Condition66%2/3
$dom->tag and $dom->tag eq "script"
!ll&&!rl&&r
101
MC/DC50%1/2
$dom->tag and $dom->tag eq "script"
$dom->tag $dom->tag eq "script"
Truth table66%2/3
A and B
A= $dom->tagB= $dom->tag eq "script"
ABresult
0X0
100
111
177 }
178 1 return;
179 }
180  
181 10 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
182  
183 10 1;
184  
185 =head1 NAME
186  
187 WWW::Crawler::Mojo::ScraperUtil - Scraper utitlities
188  
189 =head1 SYNOPSIS
190  
191 =head1 DESCRIPTION
192  
193 This class inherits L<Mojo::UserAgent> and override start method for storing
194 user info
195  
196 =head1 ATTRIBUTES
197  
198 WWW::Crawler::Mojo::ScraperUtil implements following attributes.
199  
200 =head1 METHODS
201  
202 WWW::Crawler::Mojo::ScraperUtil implements following methods.
203  
204 =head2 collect_urls_css
205  
206 Collects URLs out of CSS.
207  
208 @urls = collect_urls_css($dom);
209  
210 =head2 decoded_body
211  
212 Returns decoded response body for given L<Mojo::Message::Request> using
213 guess_encoding and encoder.
214  
215 =head2 encoder
216  
217 Generates L<Encode> instance for given name. Defaults to L<Encode::utf8>.
218  
219 =head2 html_handler_presets
220  
221 Returns common html handler in hash reference.
222  
223 my $handlers = html_handlers();
224  
225 =head2 reduce_html_handlers
226  
227 Narrows html handler selectors by prefixing container CSS snippets.
228  
229 my $handlers = html_handlers($handlers, ['#header', '#footer li']);
230
231 $handlers->{img} = sub {
232 my $dom = shift;
233 return $dom->{src};
234 };
235
236 my @urls;
237 for my $selector (sort keys %{$handlers}) {
238 $dom->find($selector)->each(sub {
239 push(@urls, $handlers->{$selector}->(shift));
240 })->to_array;
241 }
242  
243 =head2 resolve_href
244  
245 Resolves URLs with a base URL.
246  
247 WWW::Crawler::Mojo::resolve_href($base, $uri);
248  
249 =head2 guess_encoding
250  
251 Guesses encoding of HTML or CSS with given L<Mojo::Message::Response> instance.
252  
253 $encode = WWW::Crawler::Mojo::guess_encoding($res) || 'utf-8'
254  
255 =head1 AUTHOR
256  
257 Keita Sugama, E<lt>sugama@jamadam.comE<gt>
258  
259 =head1 COPYRIGHT AND LICENSE
260  
261 Copyright (C) Keita Sugama.
262  
263 This program is free software; you can redistribute it and/or
264 modify it under the same terms as Perl itself.
265  
266 =cut
↑ Summary blib/lib/WWW/Crawler/Mojo/Queue/MySQL.pm »