LyricFinder/_Class.pm

filter: Statementstmt 11.8% 18 / 152 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 20.8% 5 / 24 Podpod 0.0% 0 / 13 totaltotal 9.0% 23 / 254 CC 26 SCAR 39.6
CC
26
Cov
9%
new
182.0
_check_inputs
12.0
_web_fetch
12.0
CRAP 52.6
« LyricFinder/Cache.pm ↑ Summary
1 package LyricFinder::_Class;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $AGENT = "Mozilla/5.0 (X11; Linux x86_64; rv:112.0) Gecko/20100101 Firefox/112.0";
10 1 our $DEBUG = 0; # If you want debug messages, set debug to a true value, and
11 # messages will be output with warn.
12  
13 sub new
14 0 ▶{
Subroutine new: not called CC=13 SCAR=52.0
Pod undocumented
15 0 my $class = shift;
16 0 my $source = shift;
17  
18 0 my $self = {};
19  
20 0 $self->{'-debug'} = $DEBUG;
21 0 $self->{'-agent'} = $AGENT;
22 0 $self->{'-cache'} = '';
23 0 $self->{'-synced'} = 'No'; #USER CHOICES ARE: YES(synced|plain), NO(plain), ONLY(synced), or OK|''(plain|synced)!
24 0 $self->{'Error'} = 'Ok';
25 0 $self->{'Source'} = $source;
26 0 $self->{'Site'} = '';
27 0 $self->{'Order'} = '';
28 0 $self->{'Tried'} = '';
29 0 $self->{'Url'} = '';
30 0 $self->{'image_url'} = '';
31 0 $self->{'Credits'} = [];
32  
33 #EXTRACT ANY ARGUMENTS:
34 0 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
35 0 ▶ if ($_[0] =~ /^\-/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\-/o) 0 0 0
36 0 my $key = shift;
37 0 ▶ $self->{$key} = (!defined($_[0]) || $_[0] =~/^\-/) ? 1 : shift;
Branch: 1 branch
BranchTrueFalseTotal
!defined($_[0]) || $_[0] =~ /^\-/ ? : 0 0 0
Condition0%0/3
not defined $_[0] or $_[0] =~ /^\-/
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $_[0] or $_[0] =~ /^\-/
not defined $_[0] $_[0] =~ /^\-/
Truth table0%0/3
not(A) or B
A= not defined $_[0]B= $_[0] =~ /^\-/
ABresult
1X1
011
000
38 0 next;
39 }
40 0 shift;
41 }
42  
43 #NOW EXTRACT ANY SUBMODULE-SPECIFIC HASH ARGUMENTS (ie. "-Submodule => {args}"):
44 0 ▶ if (defined($self->{"-$source"}) && ref($self->{"-$source"}) =~ /HASH/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$self{"-$source"} and ref($$self{"-$source"}) =~ /HASH/) 0 0 0
Condition0%0/3
defined $$self{"-$source"} and ref($$self{"-$source"}) =~ /HASH/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $$self{"-$source"} and ref($$self{"-$source"}) =~ /HASH/
defined $$self{"-$source"} ref($$self{"-$source"}) =~ /HASH/
Truth table0%0/3
A and B
A= defined $$self{"-$source"}B= ref($$self{"-$source"}) =~ /HASH/
ABresult
0X0
100
111
45 0 my @subarglist = %{$self->{"-$source"}};
46 0 ▶ while (@subarglist) {
Branch: 1 branch
BranchTrueFalseTotal
if (@subarglist) 0 0 0
47 0 ▶ if ($subarglist[0] =~ /^\-/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subarglist[0] =~ /^\-/o) 0 0 0
48 0 my $key = shift @subarglist;
49 0 ▶ $self->{$key} = (!defined($subarglist[0]) || $subarglist[0] =~/^\-/) ? 1 : shift(@subarglist);
Branch: 1 branch
BranchTrueFalseTotal
!defined($subarglist[0]) || $subarglist[0] =~ /^\-/ ? : 0 0 0
Condition0%0/3
not defined $subarglist[0] or $subarglist[0] =~ /^\-/
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $subarglist[0] or $subarglist[0] =~ /^\-/
not defined $subarglist[0] $subarglist[0] =~ /^\-/
Truth table0%0/3
not(A) or B
A= not defined $subarglist[0]B= $subarglist[0] =~ /^\-/
ABresult
1X1
011
000
50 0 next;
51 }
52 0 shift @subarglist;
53 }
54 }
55  
56 0 ▶ $self->{'-debug'} = $DEBUG unless (defined($self->{'-debug'}) && $self->{'-debug'} =~ /^\d$/);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"-debug"} and $self->{"-debug"} =~ /^\d$/ 0 0 0
Condition0%0/3
defined $self->{"-debug"} and $self->{"-debug"} =~ /^\d$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"-debug"} and $self->{"-debug"} =~ /^\d$/
defined $self->{"-debug"} $self->{"-debug"} =~ /^\d$/
Truth table0%0/3
A and B
A= defined $self->{"-debug"}B= $self->{"-debug"} =~ /^\d$/
ABresult
0X0
100
111
57 0 bless $self, $class; #BLESS IT!
58  
59 0 return $self;
60 }
61  
62 0 ▶sub _debug {
Subroutine _debug: not called CC=2 SCAR=17.9
63 0 my $self = shift;
64 0 my $msg = shift;
65
66 0 ▶ warn $msg if $self->{'-debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"-debug"} 0 0 0
67 }
68  
69 0 ▶sub sources {
Subroutine sources: not called CC=2 SCAR=17.9
Pod undocumented
70 0 my $self = shift;
71 0 ▶ return wantarray ? @{$self->{'_fetchers'}} : \@{$self->{'_fetchers'}};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
72 }
73  
74 0 ▶sub source {
Subroutine source: not called CC=1 SCAR=6.9
Pod undocumented
75 0 my $self = shift;
76 0 return $self->{'Source'};
77 }
78  
79 0 ▶sub url {
Subroutine url: not called CC=1 SCAR=6.9
Pod undocumented
80 0 my $self = shift;
81 0 return $self->{'Url'};
82 }
83  
84 0 ▶sub order {
Subroutine order: not called CC=2 SCAR=17.9
Pod undocumented
85 0 my $self = shift;
86 0 ▶ return wantarray ? ($self->{'Source'}) : $self->{'Source'};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
87 }
88  
89 0 ▶sub tried {
Subroutine tried: not called CC=1 SCAR=6.9
Pod undocumented
90 0 return order (@_);
91 }
92  
93 0 ▶sub credits {
Subroutine credits: not called CC=2 SCAR=17.9
Pod undocumented
94 0 my $self = shift;
95 0 ▶ return wantarray ? @{$self->{'Credits'}} : join(', ', @{$self->{'Credits'}});
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
96 }
97  
98 0 ▶sub message {
Subroutine message: not called CC=1 SCAR=6.9
Pod undocumented
99 0 my $self = shift;
100 0 return $self->{'Error'};
101 }
102  
103 0 ▶sub site {
Subroutine site: not called CC=1 SCAR=6.9
Pod undocumented
104 0 my $self = shift;
105 0 return $self->{'Site'};
106 }
107  
108 # Allow user to specify a different user-agent:
109 0 ▶sub agent {
Subroutine agent: not called CC=2 SCAR=17.9
Pod undocumented
110 0 my $self = shift;
111 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 0 0 0
112 0 $self->{'-agent'} = $_[0];
113 } else {
114 0 return $self->{'-agent'};
115 }
116 }
117  
118 0 ▶sub cache {
Subroutine cache: not called CC=2 SCAR=17.9
Pod undocumented
119 0 my $self = shift;
120 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 0 0 0
121 0 $self->{'-cache'} = $_[0];
122 } else {
123 0 return $self->{'-cache'};
124 }
125 }
126  
127 0 ▶sub image_url {
Subroutine image_url: not called CC=1 SCAR=6.9
Pod undocumented
128 0 return shift->{'image_url'};
129 }
130  
131 0 ▶sub fetch_synced_lyrics {
Subroutine fetch_synced_lyrics: not called CC=2 SCAR=17.9
Pod undocumented
132 0 my $self = shift;
133 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 0 0 0
134 0 $self->{'-synced'} = $_[0];
135 } else {
136 0 return $self->{'-synced'};
137 }
138 }
139  
140 0 ▶sub _check_inputs {
Subroutine _check_inputs: not called CC=3 SCAR=24.8
141 0 my $self = shift;
142  
143 0 my $Source = $self->{'Source'};
144 # reset the error var, change it if an error occurs.
145 0 $self->{'Error'} = 'Ok';
146 0 $self->{'Url'} = '';
147  
148 0 ▶ unless ($_[0] && $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[0] and $_[1]) 0 0 0
Condition0%0/3
$_[0] and $_[1]
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and $_[1]
$_[0] $_[1]
Truth table0%0/3
A and B
A= $_[0]B= $_[1]
ABresult
0X0
100
111
149 0 carp($self->{'Error'} = "e:$Source.fetch() called without artist and song!");
150 0 return 0;
151 }
152 0 return 1;
153 }
154  
155 0 ▶sub _web_fetch {
Subroutine _web_fetch: not called CC=3 SCAR=24.8
156 0 my $self = shift;
157  
158 0 $self->_debug($self->{'Source'}.":_web_fetch($_[0], $_[1]): URL=".$self->{'Url'}."=");
159 0 my $ua = LWP::UserAgent->new(
160 ssl_opts => { verify_hostname => 0, },
161 );
162 0 $ua->timeout(10);
163 0 $ua->agent($self->{'-agent'});
164 0 $ua->protocols_allowed(['https', 'http']);
165 0 $ua->cookie_jar( {} );
166 0 push @{ $ua->requests_redirectable }, 'GET';
167 0 (my $referer = $self->{'Url'}) =~ s{^(\w+)\:\/\/}{};
168 0 my $protocol = $1;
169 0 $referer =~ s{\/.+$}{\/};
170 0 my $host = $referer;
171 0 $host =~ s{\/$}{};
172 0 $referer = $protocol . '://' . $referer;
173 0 my $req = new HTTP::Request 'GET' => $self->{'Url'};
174 0 $req->header(
175 'Accept' =>
176 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
177 'Accept-Language' => 'en-US,en;q=0.5',
178 'Accept-Encoding' => 'gzip, deflate',
179 'Connection' => 'keep-alive',
180 'Upgrade-insecure-requests' => 1,
181 'Host' => $host,
182 );
183  
184 0 my $res = $ua->request($req);
185  
186 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
187 0 my $lyrics = $self->_parse($res->decoded_content, @_);
188 0 return $lyrics;
189 } else {
190 0 my $Source = $self->{'Source'};
191 0 ▶ if ($res->status_line =~ /^404/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->status_line =~ /^404/) { } 0 0 0
192 0 $self->{'Error'} = "..$Source - Lyrics not found.";
193 } else {
194 0 carp($self->{'Error'} = "e:$Source - Failed to retrieve ".$self->{'Url'}
195 .' ('.$res->status_line.').');
196 }
197 0 return '';
198 }
199 }
200  
201 0 ▶sub _remove_accents {
Subroutine _remove_accents: not called CC=1 SCAR=6.9
202 0 my $self = shift;
203 0 my $str = shift;
204  
205 0 $str =~ tr/\xc4\xc2\xc0\xc1\xc3\xe4\xe2\xe0\xe1\xe3/aaaaaaaaaa/;
206 0 $str =~ tr/\xcb\xca\xc8\xc9\xeb\xea\xe8\xe9/eeeeeeee/;
207 0 $str =~ tr/\xcf\xcc\xef\xec/iiii/;
208 0 $str =~ tr/\xd6\xd4\xd2\xd3\xd5\xf6\xf4\xf2\xf3\xf5/oooooooooo/;
209 0 $str =~ tr/\xdc\x{0016}\xd9\xda\xfc\x{0016}\xf9\xfa/uuuuuuuu/;
210 0 $str =~ tr/\x{0178}\xdd\xff\xfd/yyyy/;
211 0 $str =~ tr/\xd1\xf1/nn/;
212 0 $str =~ tr/\xc7\xe7/cc/;
213 0 $str =~ s/\xdf/ss/g;
214  
215 0 return $str;
216 }
217  
218 # nasty way to strip out HTML
219 0 ▶sub _html2text {
Subroutine _html2text: not called CC=1 SCAR=6.9
220 0 my $self = shift;
221 0 my $str = shift;
222  
223 0 $str =~ s#\<(?:br|\/?p).*?\>#\n#gio;
224 0 $str =~ s#\&gt\;#\>#go;
225 0 $str =~ s#\&lt\;#\<#go;
226 0 $str =~ s#\&amp\;#\&#go;
227 0 $str =~ s#\&quot\;#\"#go;
228 0 $str =~ s#\<.*?\>##go;
229  
230 0 return $str;
231 }
232  
233 0 ▶sub _normalize_lyric_text {
Subroutine _normalize_lyric_text: not called CC=3 SCAR=24.8
234 0 my $self = shift;
235 0 my $str = shift;
236  
237 # normalize Windowsey \r\n sequences:
238 0 $str =~ s/\r+//gs;
239 # strip off pre & post padding with spaces:
240 0 $str =~ s/^ +//mg;
241 0 $str =~ s/ +$//mg;
242 # clear up repeated blank lines:
243 0 $str =~ s/(\R){2,}/\n\n/gs;
244 # and remove any blank top and bottom lines:
245 0 $str =~ s/^\R+//s;
246 0 $str =~ s/\R\R+$/\n/s;
247 # add a linefeed to end of lyrics if ther's not one already:
248 0 ▶ $str .= "\n" unless ($str =~ /\n$/s);
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /\n$/s 0 0 0
249 # now fix up for either Windows or Linux/Unix:
250 0 ▶ $str =~ s/\R/\r\n/gs if ($^O =~ /Win/);
Branch: 1 branch
BranchTrueFalseTotal
if $^O =~ /Win/ 0 0 0
251  
252 0 return $str;
253 }
254  
255 1 1
256  
257 __END__
« LyricFinder/Cache.pm ↑ Summary