scrape

filter: Statementstmt 72.5% 95 / 131 Branchbran 54.1% 26 / 48 Conditioncond 60.0% 9 / 15 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 66.6% 142 / 213 CC 14 SCAR 22.6
CC
14
Cov
67%
show_core
14.5
BEGIN
1.0
BEGIN
1.0
CRAP 9.6
« blib/lib/HTML/ListScraper/Interactive.pm ↑ Summary blib/lib/HTML/ListScraper/Sweep.pm »
1 #!/usr/bin/perl
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use HTML::ListScraper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use HTML::ListScraper::Interactive qw(format_tags canonicalize_tags);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Fatal qw(open close);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use vars qw(*EXPORT *IMPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub show_core;
14  
15 6 my $whole;
16 my $shapeless;
17 6 my $min_count = 1;
18 6 my @core_values = qw(item list all);
19 6 my $core;
20 6 my @detail_values = qw(none tags text attributes all);
21 6 my $detail = 'tags';
22 6 my $export;
23 my $import;
24 6 my $acquire;
25 6 my $ignore;
26 6 GetOptions('whole' => \$whole,
27 'shapeless' => \$shapeless,
28 'min-count=i' => \$min_count,
29 'core=s' => \$core,
30 'detail=s' => \$detail,
31 'export=s' => \$export,
32 'import=s' => \$import,
33 'acquire=s' => \$acquire,
34 'ignore=s' => \$ignore
35 );
36  
37 6 my $argc = scalar(@ARGV);
38 6 ▶if (($argc < 1) || ($argc > 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($argc < 1 or $argc > 2) 0 6 6
Condition33%1/3
$argc < 1 or $argc > 2
l!l&&r!l&&!r
006
MC/DC0%0/2
$argc < 1 or $argc > 2
$argc < 1 $argc > 2
Truth table33%1/3
A or B
A= $argc < 1B= $argc > 2
ABresult
1X1
011
000
39 0 die "usage: $0 [ options ] filename\n";
40 };
41  
42 # binmode(STDOUT, ":utf8");
43  
44 6 my $scraper = HTML::ListScraper->new( api_version => 3,
45 marked_sections => 1 );
46  
47 6 my @ignore;
48 6 ▶if (!$ignore) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ignore) { } 6 0 6
49 6 @ignore = qw(b i em strong);
50 } else {
51 0 @ignore = map { lc $_; } split /\s*,\s*/, $ignore;
52 }
53  
54 6 ▶if (@ignore) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ignore) 6 0 6
55 6 $scraper->ignore_tags(@ignore);
56 }
57  
58 6 ▶if ($shapeless) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shapeless) 0 6 6
59 0 $scraper->shapeless(1);
60 }
61  
62 6 ▶if ($min_count > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min_count > 1) 1 5 6
63 1 $scraper->min_count($min_count);
64 }
65  
66 6 my $name = $ARGV[0];
67 6 ▶if ($acquire || ($name =~ /^http:\/\//)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($acquire or $name =~ m[^http://]) { } 0 6 6
Condition33%1/3
$acquire or $name =~ m[^http://]
l!l&&r!l&&!r
006
MC/DC0%0/2
$acquire or $name =~ m[^http://]
$acquire $name =~ m[^http://]
Truth table33%1/3
A or B
A= $acquireB= $name =~ m[^http://]
ABresult
1X1
011
000
68 0 my $ua = LWP::UserAgent->new;
69 0 my $resp = $ua->get($name);
70 0 ▶ if (!$resp->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 0 0 0
71 0 die $resp->status_line;
72 }
73  
74 0 my $raw = $resp->content;
75 0 ▶ if ($acquire) {
Branch: 1 branch
BranchTrueFalseTotal
if ($acquire) 0 0 0
76 0 open(EXPORT, ">$acquire");
77 0 print EXPORT $raw;
78 0 close(EXPORT);
79 }
80  
81 0 my $html = $resp->decoded_content;
82 0 ▶ if (!$html) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($html) 0 0 0
83 0 warn "cannot decode incoming HTML: using raw bytes\n";
84 0 $html = $raw;
85 }
86  
87 0 $scraper->parse($html);
88 0 $scraper->eof;
89 } else {
90 6 $scraper->parse_file($name);
91 }
92  
93 6 ▶if ($whole) {
Branch: 1 branch
BranchTrueFalseTotal
if ($whole) 0 6 6
94 0 my $tags = $scraper->get_all_tags();
95 0 print "---|\n", format_tags($scraper, $tags, { index => 1 });
96 }
97  
98 6 ▶if ($import && !$core) {
Branch: 1 branch
BranchTrueFalseTotal
if ($import and not $core) 0 6 6
Condition66%2/3
$import and not $core
!ll&&!rl&&r
420
MC/DC0%0/2
$import and not $core
$import not $core
Truth table66%2/3
A and not(B)
A= $importB= not $core
ABresult
0X0
100
111
99 0 $core = 'l';
100 }
101  
102 6 ▶if ($core) {
Branch: 1 branch
BranchTrueFalseTotal
if ($core) 6 0 6
103 6 my %core_values;
104 6 foreach my $lv (@core_values) {
105 18 my $sv = substr($lv, 0, 1);
106 18 $core_values{$sv} = 1;
107 18 ▶ if ($core eq $lv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($core eq $lv) 3 15 18
108 3 $core = $sv;
109 }
110 }
111  
112 6 ▶ if (!exists($core_values{$core})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $core_values{$core}) 0 6 6
113 0 my $cv = join ', ', @core_values;
114 0 die "invalid --core value; use one of [ $cv ]\n";
115 }
116  
117 6 show_core();
118 }
119  
120 6 ▶if ($export) {
Branch: 1 branch
BranchTrueFalseTotal
if ($export) 0 6 6
121 0 my @seq = $scraper->get_sequences;
122 0 my $seq = shift @seq;
123 0 ▶ if ($seq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seq) 0 0 0
124 0 open(EXPORT, ">$export");
125 0 my @inst = $seq->instances;
126 0 my $inst = shift @inst;
127 0 my @tags = format_tags($scraper, [ $inst->tags ]);
128 0 print EXPORT @tags;
129 0 close(EXPORT);
130 }
131 }
132  
133 6 ▶sub show_core {
Subroutine show_core: called CC=14 SCAR=26.7
134 6 my %detail_values;
135 6 foreach my $lv (@detail_values) {
136 30 $detail_values{$lv} = 1;
137 }
138  
139 6 ▶ if (!exists($detail_values{$detail})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $detail_values{$detail}) 0 6 6
140 0 my $dv = join ', ', @detail_values;
141 0 die "invalid --detail value; use one of [ $dv ]\n";
142 }
143  
144 6 my @sequences;
145 6 ▶ if (!$import) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $import) { } 4 2 6
146 4 @sequences = $scraper->find_sequences;
147 } else {
148 2 open(IMPORT, $import);
149 2 my @import = <IMPORT>;
150 2 close(IMPORT);
151 2 my @tags = canonicalize_tags(@import);
152 2 my $s = $scraper->find_known_sequence(@tags);
153 2 ▶ if ($s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s) 2 0 2
154 2 push @sequences, $s;
155 }
156 }
157  
158 6 foreach my $seq (@sequences) {
159 6 print "---\n";
160 6 print "len: ", $seq->len, "\n";
161  
162 6 my $inst_count = scalar($seq->instances);
163 6 print "count: $inst_count\n";
164 6 ▶ if ($inst_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inst_count) 6 0 6
165 6 print "seq:\n";
166 6 foreach my $inst ($seq->instances) {
167 62 print " - ", $inst->match, ": ", $inst->start, "\n";
168  
169 62 ▶ if ($detail ne 'none') {
Branch: 1 branch
BranchTrueFalseTotal
if ($detail ne "none") 50 12 62
170 50 ▶ if (defined($inst->score)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $inst->score) 14 36 50
171 14 print " score: ", $inst->score, "\n";
172 }
173  
174 50 ▶ my $incl_text = ($detail eq 'text') || ($detail eq 'all');
Condition66%2/3
$detail eq 'text' || $detail eq 'all'
l!l&&r!l&&!r
01139
MC/DC50%1/2
$detail eq 'text' || $detail eq 'all'
$detail eq 'text' $detail eq 'all'
Truth table66%2/3
A || B
A= $detail eq 'text'B= $detail eq 'all'
ABresult
1X1
011
000
175 50 ▶ my $incl_attr = ($detail eq 'attributes') ||
Condition100%3/3
$detail eq 'attributes' || $detail eq 'all'
l!l&&r!l&&!r
33116
MC/DC100%2/2
$detail eq 'attributes' || $detail eq 'all'
$detail eq 'attributes' $detail eq 'all'
Truth table100%3/3
A || B
A= $detail eq 'attributes'B= $detail eq 'all'
ABresult
1X1
011
000
176 ($detail eq 'all');
177 50 my $indent = ' ' x 4;
178 my @scalar = map {
179 50 "$indent$_";
180 } format_tags($scraper, [ $inst->tags ],
181 { text => $incl_text, attr => $incl_attr });
182 50 print " inst: |\n", @scalar;
183 }
184  
185 62 ▶ if ($core eq 'i') {
Branch: 1 branch
BranchTrueFalseTotal
if ($core eq "i") 0 62 62
186 0 return;
187 }
188 }
189 }
190  
191 6 ▶ if ($core eq 'l') {
Branch: 1 branch
BranchTrueFalseTotal
if ($core eq "l") 1 5 6
192 1 return;
193 }
194 }
195 }
196  
197 __END__
« blib/lib/HTML/ListScraper/Interactive.pm ↑ Summary blib/lib/HTML/ListScraper/Sweep.pm »