Extractor/Utils.pm

filter: Statementstmt 96.4% 110 / 114 Branchbran 81.2% 26 / 32 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 26 / 26 Podpod 100% 13 / 13 totaltotal 92.8% 181 / 195 CC 21 SCAR 10.2
CC
21
Cov
92%
load_addr_to_str
5.2
absolutize_links_array
5.0
looks_like_url
4.0
CRAP 2.8
« Extractor.pm ↑ Summary
1 #ABSTRACT: Set of functions that can be useful when building web crawlers
2  
3  
4 package Email::Extractor::Utils;
5 2 $Email::Extractor::Utils::VERSION = '0.01';
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use feature 'say';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use URI::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use File::Slurp qw(read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use File::Basename; # fileparse
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Regexp::Common qw /URI/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Mojo::DOM;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 require Exporter;
21  
22 2 our @ISA = qw(Exporter);
23 2 our @EXPORT_OK = qw(
24 looks_like_url
25 looks_like_rel_link
26 looks_like_file
27 get_file_uri
28 load_addr_to_str
29 absolutize_links_array
30 find_all_links
31 find_links_by_text
32 drop_asset_links
33 drop_anchor_links
34 remove_query_params
35 remove_external_links
36 );
37 2 our %EXPORT_TAGS = ( 'ALL' => [@EXPORT_OK] );
38  
39 2 ▶our $Verbose = 0 unless defined $Verbose;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Email::Extractor::Utils::Verbose 2 0 2
40  
41  
42 2 our $Assets = [
43 'css', 'less', 'js', 'jpg', 'JPG', 'jpeg', 'JPEG', 'png',
44 'PNG', 'svg', 'doc', 'docx', 'ppt', 'odt', 'rtf', 'ppt'
45 ];
46  
47  
48 sub load_addr_to_str {
49 4 ▶ my $addr = shift;
Subroutine load_addr_to_str: called CC=5 SCAR=16.5
Pod documented
50  
51 4 eval {
52  
53 4 ▶ if ( looks_like_url($addr) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (looks_like_url($addr)) { } 1 3 4
54  
55 1 ▶ say "$addr: is url" if ($Verbose);
Branch: 1 branch
BranchTrueFalseTotal
if $Email::Extractor::Utils::Verbose 0 1 1
56 1 my $ua = LWP::UserAgent->new;
57 1 my $resp = $ua->get($addr); # HTTP::Response
58 1 return $resp->content;
59  
60 }
61 else {
62  
63 3 ▶ say "$addr: is file" if ($Verbose);
Branch: 1 branch
BranchTrueFalseTotal
if $Email::Extractor::Utils::Verbose 0 3 3
64 3 my $file_uri = get_file_uri($addr);
65  
66 3 ▶ if ( looks_like_file($file_uri) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (looks_like_file($file_uri)) { } 3 0 3
67 3 return read_file( get_abs_path($addr) );
68  
69 }
70 else {
71 0 die "No such file: " . $addr . " or it is not file or http uri";
72 }
73  
74 }
75  
76 };
77 }
78  
79  
80 sub get_abs_path {
81 3 ▶ my $filename = shift;
Subroutine get_abs_path: called CC=1 SCAR=0.0
Pod documented
82 3 return File::Spec->catfile( getcwd(), $filename );
83 }
84  
85  
86 sub get_file_uri {
87 3 ▶ my $filename = shift;
Subroutine get_file_uri: called CC=1 SCAR=0.0
Pod documented
88 3 return 'file://' . File::Spec->catfile( getcwd(), $filename );
89 }
90  
91  
92 sub looks_like_url {
93 17 ▶ my $string = shift;
Subroutine looks_like_url: called CC=4 SCAR=13.9
Pod documented
94 17 my $regexp = qr($RE{URI}{HTTP}{-scheme=>qr/https?/}{-keep});
95 17 ▶ if ( $string =~ $regexp ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /$regexp/) { } 9 8 17
96  
97 # warn "$1 $2 $3 $4 $5 $6 $7 $8";
98 9 ▶ return $7 if defined $7;
Branch: 1 branch
BranchTrueFalseTotal
if defined $7 4 5 9
99 5 ▶ return $1 if defined $1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $1 5 0 5
100 }
101 else {
102 8 return 0;
103 }
104 }
105  
106  
107 sub looks_like_rel_link {
108 8 ▶ my $link = shift;
Subroutine looks_like_rel_link: called CC=2 SCAR=6.9
Pod documented
109 8 ▶ return 0 if ( looks_like_url($link) );
Branch: 1 branch
BranchTrueFalseTotal
if looks_like_url($link) 4 4 8
110 4 return 1;
111 }
112  
113  
114 sub looks_like_file {
115 13 ▶ my $string = shift;
Subroutine looks_like_file: called CC=2 SCAR=6.9
Pod documented
116 13 ▶ if ( $string =~ qr($RE{URI}{file}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ qr/$Email::Extractor::Utils::RE{'URI'}{'file'}/) { } 4 9 13
117 4 return 1;
118 }
119 else {
120 9 return 0;
121 }
122 }
123  
124  
125 sub absolutize_links_array {
126 3 ▶ my ( $links_arr, $dname ) = @_;
Subroutine absolutize_links_array: called CC=5 SCAR=16.1
Pod documented
127  
128 3 ▶ confess 'No valid dname in absolutize_links_array()'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dname and length $dname 1 2 3
Condition66%2/3
defined $dname and length $dname
!ll&&!rl&&r
102
MC/DC50%1/2
defined $dname and length $dname
defined $dname length $dname
Truth table66%2/3
A and B
A= defined $dnameB= length $dname
ABresult
0X0
100
111
129 unless defined $dname && length $dname;
130  
131 2 my @res;
132  
133 2 for my $l (@$links_arr) {
134 6 ▶ if ( looks_like_rel_link($l) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (looks_like_rel_link($l)) 3 3 6
135 3 $l = url( $l, $dname )->abs->as_string;
136 }
137 6 push @res, $l;
138 }
139  
140 2 return \@res;
141 }
142  
143  
144 sub remove_external_links {
145 3 ▶ my ( $links_arr, $only_dname ) = @_;
Subroutine remove_external_links: called CC=4 SCAR=13.9
Pod documented
146  
147 3 ▶ confess 'No valid dname in remove_external_links()'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $only_dname and length $only_dname 1 2 3
Condition66%2/3
defined $only_dname and length $only_dname
!ll&&!rl&&r
102
MC/DC50%1/2
defined $only_dname and length $only_dname
defined $only_dname length $only_dname
Truth table66%2/3
A and B
A= defined $only_dnameB= length $only_dname
ABresult
0X0
100
111
148 unless defined $only_dname && length $only_dname;
149  
150 my @res =
151 2 ▶ grep { ( $_ =~ /^$only_dname/ ) || looks_like_file( 'file://' . $_ ) }
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /^$only_dname/ or looks_like_file('file://' . $_) 6 4 10
152 @$links_arr;
153  
154 # looks_like_file('file://'.$_) = looks_like_relative_link
155 2 return \@res;
156 }
157  
158  
159 sub drop_asset_links {
160 7 ▶ my $links = shift;
Subroutine drop_asset_links: called CC=3 SCAR=11.0
Pod documented
161  
162 7 $links = remove_query_params($links);
163  
164 7 my @res;
165  
166 7 for my $link (@$links) {
167 11 my ( $filename, $dirs, $suffix ) = fileparse( $link, @$Assets );
168 11 ▶ push( @res, $link ) if ( $suffix eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if $suffix eq "" 8 3 11
169 }
170  
171 7 return \@res;
172 }
173  
174  
175 sub drop_anchor_links {
176 7 ▶ my $links_arr = shift;
Subroutine drop_anchor_links: called CC=1 SCAR=0.0
Pod documented
177 7 my @res = grep { $_ !~ /^#/ } @$links_arr;
178 7 return \@res;
179 }
180  
181  
182 sub remove_query_params {
183 14 ▶ my $links = shift;
Subroutine remove_query_params: called CC=3 SCAR=12.0
Pod documented
184  
185 14 my @res;
186  
187 14 for my $link (@$links) {
188 22 my $uri = URI->new($link);
189 22 ▶ if ( $uri->query ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->query) { } 0 22 22
190 0 my $l = length($link) - length( $uri->query ) - 1;
191 0 my $new_str = substr $link, 0, $l;
192 0 push( @res, $new_str );
193 }
194 else {
195 22 push( @res, $link );
196 }
197 }
198  
199 14 return \@res;
200 }
201  
202  
203 sub find_all_links {
204 7 ▶ my $html = shift;
Subroutine find_all_links: called CC=1 SCAR=0.0
Pod documented
205 7 my $dom = Mojo::DOM->new($html);
206 7 return $dom->find('a')->map( attr => 'href' )->to_array;
207 }
208  
209  
210 sub find_links_by_text {
211 10 ▶ my ( $html, $a_text ) = @_;
Subroutine find_links_by_text: called CC=1 SCAR=0.0
Pod documented
212 10 my $dom = Mojo::DOM->new($html);
213  
214 # Mojo::Collection of Mojo::DOM
215 # return $dom->find('a')->grep('text' => $a_text)->map(attr => 'href')->to_array;
216 15 ▶ return $dom->find('a')->grep( sub { $_->text eq $a_text } )
Subroutine __ANON__: called CC=1 SCAR=0.0
217 10 ->map( attr => 'href' )->to_array;
218 }
219  
220 2 1;
221  
222 __END__
« Extractor.pm ↑ Summary