LyricFinder/Letras.pm

filter: Statementstmt 25.7% 25 / 97 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 70.0% 7 / 10 Podpod 100% 2 / 2 totaltotal 18.1% 34 / 187 CC 27 SCAR 48.2
CC
27
Cov
16%
fetch
210.0
_parse
132.0
new
20.0
CRAP 124.4
« LyricFinder/Lrclib.pm ↑ Summary LyricFinder/ApiLyricsOvh.pm »
1 package LyricFinder::Letras;
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 Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use HTML::Strip;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use parent 'LyricFinder::_Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $haveLyricsCache;
10 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 $haveLyricsCache = 0;
12 1 ▶ eval "use LyricFinder::Cache; \$haveLyricsCache = 1; 1";
Subroutine BEGIN: called CC=1 SCAR=0.0
13 }
14  
15 1 my $Source = 'Letras';
16 1 my $Site = 'https://www.letras.mus.br';
17 1 my $DEBUG = 0;
18  
19 sub new
20 0 ▶{
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
21 0 my $class = shift;
22  
23 0 my $self = $class->SUPER::new($Source, @_);
24 0 @{$self->{'_fetchers'}} = ($Source);
25 0 unshift(@{$self->{'_fetchers'}}, 'Cache') if ($haveLyricsCache
26 0 ▶ && $self->{'-cache'} && $self->{'-cache'} !~ /^\>/);
Branch: 1 branch
BranchTrueFalseTotal
if $LyricFinder::Letras::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\>/ 0 0 0
Condition0%0/6
$LyricFinder::Letras::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\>/
!ll&&!rl&&r
000
$LyricFinder::Letras::haveLyricsCache and $self->{"-cache"}
!ll&&!rl&&r
000
MC/DC0%0/3
$LyricFinder::Letras::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\>/
$LyricFinder::Letras::haveLyricsCache $self->{"-cache"} not $self->{"-cache"} =~ /^\>/
Truth table0%0/4
A and B and not(C)
A= $LyricFinder::Letras::haveLyricsCacheB= $self->{"-cache"}C= not $self->{"-cache"} =~ /^\>/
ABCresult
0XX0
10X0
1100
1111
27  
28 0 bless $self, $class; #BLESS IT!
29  
30 0 return $self;
31 }
32  
33 0 ▶sub fetch {
Subroutine fetch: not called CC=14 SCAR=53.5
Pod documented
34 0 my ($self, $artist_in, $song_in) = @_;
35  
36 0 $self->_debug("Letras::fetch($artist_in, $song_in)!");
37  
38 0 ▶ return '' unless ($self->_check_inputs($artist_in, $song_in));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_inputs($artist_in, $song_in) 0 0 0
39 0 ▶ return '' if ($self->{'Error'} ne 'Ok');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Error"} ne "Ok" 0 0 0
40  
41 0 my $artist = $artist_in;
42 0 my $song = $song_in;
43 # first, see if we've got it cached:
44 0 $self->_debug("i:haveCache=$haveLyricsCache= -cachedir=".$self->{'-cache'}."=");
45 0 ▶ if ($haveLyricsCache && $self->{'-cache'} && $self->{'-cache'} !~ /^\>/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LyricFinder::Letras::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\>/) 0 0 0
Condition0%0/6
$LyricFinder::Letras::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\>/
!ll&&!rl&&r
000
$LyricFinder::Letras::haveLyricsCache and $self->{"-cache"}
!ll&&!rl&&r
000
MC/DC0%0/3
$LyricFinder::Letras::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\>/
$LyricFinder::Letras::haveLyricsCache $self->{"-cache"} not $self->{"-cache"} =~ /^\>/
Truth table0%0/4
A and B and not(C)
A= $LyricFinder::Letras::haveLyricsCacheB= $self->{"-cache"}C= not $self->{"-cache"} =~ /^\>/
ABCresult
0XX0
10X0
1100
1111
46 0 my $cache = new LyricFinder::Cache(%{$self});
47 0 ▶ if ($cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache) 0 0 0
48 0 my $lyrics = $cache->fetch($artist, $song);
49 0 ▶ if (defined($lyrics) && $lyrics =~ /\w/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lyrics and $lyrics =~ /\w/) 0 0 0
Condition0%0/3
defined $lyrics and $lyrics =~ /\w/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $lyrics and $lyrics =~ /\w/
defined $lyrics $lyrics =~ /\w/
Truth table0%0/3
A and B
A= defined $lyricsB= $lyrics =~ /\w/
ABresult
0X0
100
111
50 0 $self->_debug("..Got lyrics from cache.");
51 0 $self->{'Source'} = 'Cache';
52 0 $self->{'Site'} = $cache->site();
53 0 $self->{'Url'} = $cache->url();
54  
55 0 return $lyrics;
56 }
57 }
58 }
59  
60 0 $self->{'Site'} = $Site;
61  
62 0 $artist = $self->_remove_accents($artist);
63 0 $song = $self->_remove_accents($song);
64  
65 # Their URLs look like e.g.:
66 # https://www.letras.mus.br/<artist>/<title>
67 0 ($self->{'Url'} = $artist) =~ s#\s*\/\s*# and #; #CONVERT "artist1 / artist2" TO "artist1 and artist2"!
68 0 $song =~ s#\s*\/\s*#\-#g; #FIX SONGS WITH "/" IN THEM!
69 0 $self->{'Url'} .= "/${song}-lyrics";
70 0 $self->{'Url'} =~ s/\&/and/g;
71 0 $self->{'Url'} =~ s/ +/\-/g;
72 0 $self->{'Url'} = $Site . '/' . $self->{'Url'};
73 0 $self->{'Url'} =~ tr/A-Z/a-z/;
74 0 my $lyrics = $self->_web_fetch($artist, $song);
75 0 ▶ if ($lyrics && $haveLyricsCache && $self->{'-cache'} && $self->{'-cache'} !~ /^\</) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lyrics and $LyricFinder::Letras::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\</) 0 0 0
Condition0%0/9
$lyrics and $LyricFinder::Letras::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\</
!ll&&!rl&&r
000
$lyrics and $LyricFinder::Letras::haveLyricsCache and $self->{"-cache"}
!ll&&!rl&&r
000
$lyrics and $LyricFinder::Letras::haveLyricsCache
!ll&&!rl&&r
000
MC/DC0%0/4
$lyrics and $LyricFinder::Letras::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\</
$lyrics $LyricFinder::Letras::haveLyricsCache $self->{"-cache"} not $self->{"-cache"} =~ /^\</
Truth table0%0/5
A and B and C and not(D)
A= $lyricsB= $LyricFinder::Letras::haveLyricsCacheC= $self->{"-cache"}D= not $self->{"-cache"} =~ /^\</
ABCDresult
0XXX0
10XX0
110X0
11100
11111
76 0 $self->_debug("=== WILL CACHE LYRICS! ===");
77 # cache the fetched lyrics, if we can:
78 0 my $cache = new LyricFinder::Cache(%{$self});
79 0 ▶ $cache->save($artist, $song, $lyrics) if ($cache);
Branch: 1 branch
BranchTrueFalseTotal
if $cache 0 0 0
80 }
81 0 return $lyrics;
82 }
83  
84 0 ▶sub _parse {
Subroutine _parse: not called CC=11 SCAR=48.8
85 0 my $self = shift;
86 0 my $html = shift;
87 0 my ($artist_in, $song_in) = @_;
88  
89 0 $self->_debug("Letras::_parse()!");
90 0 ▶ if ($html =~
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ m[\<div\s+class\=\"(?:lyric-original|cnt\-letra\s+p\d+\_premium)\"\>(.+?)\<\/div\>]ism) { } 0 0 0
91 m{\<div\s+class\=\"(?:lyric-original|cnt\-letra\s+p\d+\_premium)\"\>(.+?)\<\/div\>}msi)
92 {
93 0 my $goodbit = $1;
94 0 my $hs = HTML::Strip->new();
95  
96 #LETRAS SOMETIMES RETURNS "BEST GUESS" (WRONG) SONG LYRICS IF NOT FOUND, AND WE
97 #DON'T WANT THIS, SO WE MUST CONFIRM THAT THE TITLE AND ARTIST MATCH WHAT WE
98 #REQUESTED, AND PUNT IF THEY DON'T!:
99 0 ▶ if ($html =~ m#\"artist\_name\"\:\"\Q$artist_in\E\"#i && $html =~ m#\"track\_name\"\:\"\Q$song_in\E\"#i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($html =~ /\"artist\_name\"\:\"\Q$artist_in\E\"/i and $html =~ /\"track\_name\"\:\"\Q$song_in\E\"/i) { } 0 0 0
elsif ($html =~ m[\<div\s+class\=\"cnt\-head\_title\"\>(.+?)\<\/div\>]ism) { } 0 0 0
Condition0%0/3
$html =~ /\"artist\_name\"\:\"\Q$artist_in\E\"/i and $html =~ /\"track\_name\"\:\"\Q$song_in\E\"/i
!ll&&!rl&&r
000
MC/DC0%0/2
$html =~ /\"artist\_name\"\:\"\Q$artist_in\E\"/i and $html =~ /\"track\_name\"\:\"\Q$song_in\E\"/i
$html =~ /\"artist\_name\"\:\"\Q$artist_in\E\"/i $html =~ /\"track\_name\"\:\"\Q$song_in\E\"/i
Truth table0%0/3
A and B
A= $html =~ /\"artist\_name\"\:\"\Q$artist_in\E\"/iB= $html =~ /\"track\_name\"\:\"\Q$song_in\E\"/i
ABresult
0X0
100
111
100 0 goto HAVEIT;
101 } elsif ($html =~ m#\<div\s+class\=\"cnt\-head\_title\"\>(.+?)\<\/div\>#msi) {
102 0 my $headers = $1;
103 0 ▶ my $title = ($headers =~ m#\<h1\>(.+?)\<\/h1\>#si) ? $hs->parse($1) : '';
Branch: 1 branch
BranchTrueFalseTotal
$headers =~ m[\<h1\>(.+?)\<\/h1\>]is ? : 0 0 0
104 0 $title =~ s/^\s+//;
105 0 $title =~ s/\s+$//;
106 0 ▶ if ($title) {
Branch: 1 branch
BranchTrueFalseTotal
if ($title) 0 0 0
107 0 ▶ if ($song_in !~ /^${title}$/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($song_in =~ /^$title$/i) 0 0 0
108 0 $self->{'Error'} = "e:$Source - Results did not match title ($title).";
109 0 return '';
110 }
111 0 ▶ my $artist = ($headers =~ m#\<h2\>(.+?)\<\/h2\>#si) ? $hs->parse($1) : '';
Branch: 1 branch
BranchTrueFalseTotal
$headers =~ m[\<h2\>(.+?)\<\/h2\>]is ? : 0 0 0
112 0 $artist =~ s/^\s+//;
113 0 $artist =~ s/\s+$//;
114 0 ▶ if ($artist && $artist_in !~ /^${artist}$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($artist and not $artist_in =~ /^$artist$/i) 0 0 0
Condition0%0/3
$artist and not $artist_in =~ /^$artist$/i
!ll&&!rl&&r
000
MC/DC0%0/2
$artist and not $artist_in =~ /^$artist$/i
$artist not $artist_in =~ /^$artist$/i
Truth table0%0/3
A and not(B)
A= $artistB= not $artist_in =~ /^$artist$/i
ABresult
0X0
100
111
115 0 $self->{'Error'} = "e:$Source - Results did not match artist ($artist).";
116 0 return '';
117 }
118 }
119 }
120 HAVEIT:
121 0 $goodbit =~ s#\<\/?p\>#\r\n#gsi;
122 0 $goodbit =~ s#\<br\/?\>#\r\n#gsi;
123 0 my $text = $hs->parse($goodbit);
124  
125 0 return $self->_normalize_lyric_text($self->_html2text($text));
126 } else {
127 0 carp($self->{'Error'} = "e:$Source - Failed to identify lyrics on result page.");
128 0 return '';
129 }
130 }
131  
132 1 1
133  
134 __END__
« LyricFinder/Lrclib.pm ↑ Summary LyricFinder/ApiLyricsOvh.pm »