Scrappy/Scraper/Parser.pm

filter: Statementstmt 24.3% 18 / 74 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 36.8% 7 / 19 Podpod 100% 10 / 10 totaltotal 23.9% 35 / 146 CC 21 SCAR 30.7
CC
21
Cov
16%
filter
72.0
select
20.0
focus
12.0
CRAP 21.5
« Scrappy/Plugin.pm ↑ Summary Scrappy/Action/Help.pm »
1 # ABSTRACT: Scrappy Scraper Data Extrator
2 # Dist::Zilla: +PodWeaver
3  
4 package Scrappy::Scraper::Parser;
5  
6 10 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 $Scrappy::Scraper::Parser::VERSION = '0.94112090';
8 }
9  
10 # load OO System
11 10 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # load other libraries
14 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use Web::Scraper;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # web-scraper object
18 has 'worker' => (
19 is => 'ro',
20 isa => 'Web::Scraper',
21 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
22 2 ▶ scraper(sub { });
Subroutine __ANON__: not called CC=1 SCAR=0.0
23 }
24 10 );
25  
26 # html attribute
27 10 has html => (is => 'rw', isa => 'Any');
28  
29 # data attribute
30 2 ▶has data => (is => 'rw', isa => 'ArrayRef', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
31  
32 # html-tags attribute
33 has 'html_tags' => (
34 is => 'rw',
35 isa => 'HashRef',
36 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
37 return {
38 2 'abbr' => '@abbr',
39 'accept-charset' => '@accept',
40 'accept' => '@accept',
41 'accesskey' => '@accesskey',
42 'action' => '@action',
43 'align' => '@align',
44 'alink' => '@alink',
45 'alt' => '@alt',
46 'archive' => '@archive',
47 'axis' => '@axis',
48 'background' => '@background',
49 'bgcolor' => '@bgcolor',
50 'border' => '@border',
51 'cellpadding' => '@cellpadding',
52 'cellspacing' => '@cellspacing',
53 'char' => '@char',
54 'charoff' => '@charoff',
55 'charset' => '@charset',
56 'checked' => '@checked',
57 'cite' => '@cite',
58 'class' => '@class',
59 'classid' => '@classid',
60 'clear' => '@clear',
61 'code' => '@code',
62 'codebase' => '@codebase',
63 'codetype' => '@codetype',
64 'color' => '@color',
65 'cols' => '@cols',
66 'colspan' => '@colspan',
67 'compact' => '@compact',
68 'content' => '@content',
69 'coords' => '@coords',
70 'data' => '@data',
71 'datetime' => '@datetime',
72 'declare' => '@declare',
73 'defer' => '@defer',
74 'dir' => '@dir',
75 'disabled' => '@disabled',
76 'enctype' => '@enctype',
77 'face' => '@face',
78 'for' => '@for',
79 'frame' => '@frame',
80 'frameborder' => '@frameborder',
81 'headers' => '@headers',
82 'height' => '@height',
83 'href' => '@href',
84 'hreflang' => '@hreflang',
85 'hspace' => '@hspace',
86 'http' => '@http-equiv',
87 'id' => '@id',
88 'ismap' => '@ismap',
89 'label' => '@label',
90 'lang' => '@lang',
91 'language' => '@language',
92 'link' => '@link',
93 'longdesc' => '@longdesc',
94 'marginheight' => '@marginheight',
95 'marginwidth' => '@marginwidth',
96 'maxlength' => '@maxlength',
97 'media' => '@media',
98 'method' => '@method',
99 'multiple' => '@multiple',
100 'name' => '@name',
101 'nohref' => '@nohref',
102 'noresize' => '@noresize',
103 'noshade' => '@noshade',
104 'nowrap' => '@nowrap',
105 'object' => '@object',
106 'onblur' => '@onblur',
107 'onchange' => '@onchange',
108 'onclick' => '@onclick',
109 'ondblclick' => '@ondblclick',
110 'onfocus' => '@onfocus',
111 'onkeydown' => '@onkeydown',
112 'onkeypress' => '@onkeypress',
113 'onkeyup' => '@onkeyup',
114 'onload' => '@onload',
115 'onmousedown' => '@onmousedown',
116 'onmousemove' => '@onmousemove',
117 'onmouseout' => '@onmouseout',
118 'onmouseover' => '@onmouseover',
119 'onmouseup' => '@onmouseup',
120 'onreset' => '@onreset',
121 'onselect' => '@onselect',
122 'onsubmit' => '@onsubmit',
123 'onunload' => '@onunload',
124 'profile' => '@profile',
125 'prompt' => '@prompt',
126 'readonly' => '@readonly',
127 'rel' => '@rel',
128 'rev' => '@rev',
129 'rows' => '@rows',
130 'rowspan' => '@rowspan',
131 'rules' => '@rules',
132 'scheme' => '@scheme',
133 'scope' => '@scope',
134 'scrolling' => '@scrolling',
135 'selected' => '@selected',
136 'shape' => '@shape',
137 'size' => '@size',
138 'span' => '@span',
139 'src' => '@src',
140 'standby' => '@standby',
141 'start' => '@start',
142 'style' => '@style',
143 'summary' => '@summary',
144 'tabindex' => '@tabindex',
145 'target' => '@target',
146 'text' => '@text',
147 'title' => '@title',
148 'type' => '@type',
149 'usemap' => '@usemap',
150 'valign' => '@valign',
151 'value' => '@value',
152 'valuetype' => '@valuetype',
153 'version' => '@version',
154 'vlink' => '@vlink',
155 'vspace' => '@vspace',
156 'width' => '@width',
157 'text' => 'TEXT',
158 'html' => 'HTML',
159 };
160 }
161 10 );
162  
163  
164 0 ▶sub filter {
Subroutine filter: not called CC=8 SCAR=42.8
Pod documented
165 0 my ($self, @filters) = @_;
166  
167 # remove filter list
168 0 ▶ if (@filters) {
Branch: 1 branch
BranchTrueFalseTotal
if (@filters) 0 0 0
169  
170 # remove all except for specified attributes
171 $self->data(
172 [ map {
173 0 my $record = $_;
174 0 my $changes = {};
175 0 foreach my $filter (@filters) {
176  
177 #if ('HASH' eq ref $filter) {
178 # my ($tag, $value) = each(%{$filter});
179 # $changes->{$filter} = $record->{$filter}
180 # if $record->{$filter}
181 # && $record->{$filter} eq $value
182 # && $filter eq $tag;
183 #}
184 #else {
185 $changes->{$filter} = $record->{$filter}
186 0 ▶ if $record->{$filter};
Branch: 1 branch
BranchTrueFalseTotal
if $record->{$filter} 0 0 0
187  
188 #}
189 }
190 0 $changes;
191 0 } @{$self->data}
192 ]
193 );
194 }
195  
196 # remove all empty attributes
197 $self->data(
198 [ map {
199 0 my $record = $_;
200 0 foreach my $tag (keys %{$record}) {
201 delete $record->{$tag}
202 0 ▶ if !$record->{$tag}
Branch: 1 branch
BranchTrueFalseTotal
if not $record->{$tag} and $tag ne "html" and $tag ne "text" 0 0 0
Condition0%0/6
not $record->{$tag} and $tag ne "html" and $tag ne "text"
!ll&&!rl&&r
000
not $record->{$tag} and $tag ne "html"
!ll&&!rl&&r
000
MC/DC0%0/3
not $record->{$tag} and $tag ne "html" and $tag ne "text"
not $record->{$tag} $tag ne "html" $tag ne "text"
Truth table0%0/4
not(A) and B and C
A= not $record->{$tag}B= $tag ne "html"C= $tag ne "text"
ABCresult
0XX0
10X0
1100
1111
203 && $tag ne 'html'
204 && $tag ne 'text';
205 }
206 0 $record;
207 0 } @{$self->data}
208 ]
209 );
210  
211 0 return $self;
212 }
213  
214  
215 0 ▶sub focus {
Subroutine focus: not called CC=3 SCAR=24.8
Pod documented
216 0 my $self = shift;
217 0 ▶ my $index = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
218  
219 0 ▶ return $self unless $self->has_html;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_html 0 0 0
220  
221 0 $self->html($self->data->[$index]->{html});
222 0 return $self;
223 }
224  
225  
226 0 ▶sub scrape {
Subroutine scrape: not called CC=3 SCAR=24.8
Pod documented
227 0 my ($self, $selector, $html) = @_;
228  
229 0 ▶ $self->html($html) if $html;
Branch: 1 branch
BranchTrueFalseTotal
if $html 0 0 0
230  
231 0 ▶ return [] unless $self->has_html;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_html 0 0 0
232  
233 0 $self->select($selector);
234 0 return $self->data;
235 }
236  
237  
238 0 ▶sub select {
Subroutine select: not called CC=4 SCAR=30.0
Pod documented
239 0 my ($self, $selector, $html) = @_;
240  
241 0 ▶ $self->html($html) if $html;
Branch: 1 branch
BranchTrueFalseTotal
if $html 0 0 0
242  
243 0 ▶ return $self unless $self->has_html;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_html 0 0 0
244  
245 0 ▶ $self->worker->{code} = scraper {
Subroutine __ANON__: not called CC=1 SCAR=6.9
246 0 process($selector, "data[]", $self->html_tags);
247 0 };
248  
249 0 my $scraper = $self->worker->{code};
250  
251 0 ▶ $self->data($scraper->scrape($self->html)->{data} || []);
Condition0%0/2
$scraper->scrape($self->html)->{'data'} || []
l!l
00
MC/DC0%0/1
$scraper->scrape($self->html)->{'data'} || []
$scraper->scrape($self->html)->{'data'}
Truth table0%0/2
A
A= $scraper->scrape($self->html)->{'data'}
Aresult
11
00
252 0 $self->filter;
253 0 return $self;
254 }
255  
256  
257 0 ▶sub first {
Subroutine first: not called CC=1 SCAR=6.9
Pod documented
258 0 return shift->data->[0];
259 }
260  
261  
262 0 ▶sub last {
Subroutine last: not called CC=1 SCAR=6.9
Pod documented
263 0 my $self = shift;
264 0 my $index = @{$self->data} - 1;
265 0 return $self->data->[$index];
266 }
267  
268  
269 0 ▶sub select_first {
Subroutine select_first: not called CC=3 SCAR=24.8
Pod documented
270 0 my ($self, $selector, $attribute) = @_;
271 0 $self->select($selector);
272 0 ▶ return $self->data->[0] ? $self->data->[0]->{$attribute || 'text'} : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->data->[0] ? : 0 0 0
Condition0%0/2
$attribute || 'text'
l!l
00
MC/DC0%0/1
$attribute || 'text'
$attribute
Truth table0%0/2
A
A= $attribute
Aresult
11
00
273 }
274  
275  
276 0 ▶sub select_last {
Subroutine select_last: not called CC=3 SCAR=24.8
Pod documented
277 0 my ($self, $selector, $attribute) = @_;
278 0 $self->select($selector);
279 0 my $index = @{$self->data} - 1;
280 return $self->data->[$index]
281 0 ▶ ? $self->data->[$index]->{$attribute || 'text'}
Branch: 1 branch
BranchTrueFalseTotal
$self->data->[$index] ? : 0 0 0
Condition0%0/2
$attribute || 'text'
l!l
00
MC/DC0%0/1
$attribute || 'text'
$attribute
Truth table0%0/2
A
A= $attribute
Aresult
11
00
282 : '';
283 }
284  
285  
286 0 ▶sub each {
Subroutine each: not called CC=2 SCAR=17.9
Pod documented
287 0 my ($self, $code) = @_;
288 0 foreach my $item (@{$self->data}) {
289 0 $code->($item);
290 }
291 0 return $self;
292 }
293  
294  
295 0 ▶sub has_html {
Subroutine has_html: not called CC=2 SCAR=17.9
Pod documented
296 0 my $self = shift;
297 0 ▶ return $self->html ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->html ? : 0 0 0
298 }
299  
300 10 1;
301  
302 __END__
« Scrappy/Plugin.pm ↑ Summary Scrappy/Action/Help.pm »