LyricFinder/Musixmatch.pm

filter: Statementstmt 26.6% 28 / 105 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 72.7% 8 / 11 Podpod 100% 2 / 2 totaltotal 18.9% 38 / 201 CC 30 SCAR 50.1
CC
30
Cov
16%
fetch
210.0
_parse
210.0
new
20.0
CRAP 149.2
« LyricFinder.pm ↑ Summary LyricFinder/Lrclib.pm »
1 package LyricFinder::Musixmatch;
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 URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use parent 'LyricFinder::_Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $haveLyricsCache;
11 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 $haveLyricsCache = 0;
13 1 ▶ eval "use LyricFinder::Cache; \$haveLyricsCache = 1; 1";
Subroutine BEGIN: called CC=1 SCAR=0.0
14 }
15  
16 1 my $Source = 'Musixmatch';
17 1 my $Site = 'https://www.musixmatch.com';
18 1 my $DEBUG = 0;
19  
20 sub new
21 0 ▶{
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
22 0 my $class = shift;
23  
24 0 my $self = $class->SUPER::new($Source, @_);
25 0 @{$self->{'_fetchers'}} = ($Source);
26 0 unshift(@{$self->{'_fetchers'}}, 'Cache') if ($haveLyricsCache
27 0 ▶ && $self->{'-cache'} && $self->{'-cache'} !~ /^\>/);
Branch: 1 branch
BranchTrueFalseTotal
if $LyricFinder::Musixmatch::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\>/ 0 0 0
Condition0%0/6
$LyricFinder::Musixmatch::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\>/
!ll&&!rl&&r
000
$LyricFinder::Musixmatch::haveLyricsCache and $self->{"-cache"}
!ll&&!rl&&r
000
MC/DC0%0/3
$LyricFinder::Musixmatch::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\>/
$LyricFinder::Musixmatch::haveLyricsCache $self->{"-cache"} not $self->{"-cache"} =~ /^\>/
Truth table0%0/4
A and B and not(C)
A= $LyricFinder::Musixmatch::haveLyricsCacheB= $self->{"-cache"}C= not $self->{"-cache"} =~ /^\>/
ABCresult
0XX0
10X0
1100
1111
28  
29 0 bless $self, $class; #BLESS IT!
30  
31 0 return $self;
32 }
33  
34 0 ▶sub fetch {
Subroutine fetch: not called CC=14 SCAR=53.5
Pod documented
35 0 my ($self, $artist_in, $song_in) = @_;
36  
37 0 $self->_debug("Musixmatch::fetch($artist_in, $song_in)!");
38  
39 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
40 0 ▶ return '' if ($self->{'Error'} ne 'Ok');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Error"} ne "Ok" 0 0 0
41  
42 # first, see if we've got it cached:
43 0 $self->_debug("i:haveCache=$haveLyricsCache= -cachedir=".$self->{'-cache'}."=");
44 0 ▶ if ($haveLyricsCache && $self->{'-cache'} && $self->{'-cache'} !~ /^\>/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LyricFinder::Musixmatch::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\>/) 0 0 0
Condition0%0/6
$LyricFinder::Musixmatch::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\>/
!ll&&!rl&&r
000
$LyricFinder::Musixmatch::haveLyricsCache and $self->{"-cache"}
!ll&&!rl&&r
000
MC/DC0%0/3
$LyricFinder::Musixmatch::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\>/
$LyricFinder::Musixmatch::haveLyricsCache $self->{"-cache"} not $self->{"-cache"} =~ /^\>/
Truth table0%0/4
A and B and not(C)
A= $LyricFinder::Musixmatch::haveLyricsCacheB= $self->{"-cache"}C= not $self->{"-cache"} =~ /^\>/
ABCresult
0XX0
10X0
1100
1111
45 0 my $cache = new LyricFinder::Cache(%{$self});
46 0 ▶ if ($cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache) 0 0 0
47 0 my $lyrics = $cache->fetch($artist_in, $song_in);
48 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
49 0 $self->_debug("..Got lyrics from cache.");
50 0 $self->{'Source'} = 'Cache';
51 0 $self->{'Site'} = $cache->site();
52 0 $self->{'Url'} = $cache->url();
53  
54 0 return $lyrics;
55 }
56 }
57 }
58  
59 0 $self->{'Site'} = $Site;
60  
61 0 (my $artist = $artist_in) =~ s#\s*\/.*$##; #ONLY USE 1ST ARTIST, IF MORE THAN ONE!
62 0 (my $song = $song_in) =~ s#\s*\/\s*#\-#; #FIX SONGS WITH "/" IN THEM!
63 0 $artist =~ s/\&/feat/; #DON'T ASK ME WHY "&" => "feat"? BUT IT DOES!
64 0 $song =~ s/\&/feat/;
65 0 $artist =~ s/ +/\-/g;
66 0 $song =~ s/ +/\-/g;
67 0 $artist =~ tr/A-Z/a-z/;
68 0 $song =~ tr/A-Z/a-z/;
69 0 $artist = uri_escape_utf8($artist);
70 0 $song = uri_escape_utf8($song);
71 0 $artist =~ s/[^a-z0-9\-\%]//gi;
72 0 $song =~ s/[^a-z0-9\-\%]//gi;
73  
74 # Their URLs look like e.g.:
75 #https://www.musixmatch.com/lyrics/Artist-name/Title
76 0 $self->{'Url'} = $Site . '/lyrics/'
77 . join('/', $artist, $song);
78  
79 0 my $lyrics = $self->_web_fetch($artist_in, $song_in);
80 0 ▶ if ($lyrics && $haveLyricsCache && $self->{'-cache'} && $self->{'-cache'} !~ /^\</) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lyrics and $LyricFinder::Musixmatch::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\</) 0 0 0
Condition0%0/9
$lyrics and $LyricFinder::Musixmatch::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\</
!ll&&!rl&&r
000
$lyrics and $LyricFinder::Musixmatch::haveLyricsCache and $self->{"-cache"}
!ll&&!rl&&r
000
$lyrics and $LyricFinder::Musixmatch::haveLyricsCache
!ll&&!rl&&r
000
MC/DC0%0/4
$lyrics and $LyricFinder::Musixmatch::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\</
$lyrics $LyricFinder::Musixmatch::haveLyricsCache $self->{"-cache"} not $self->{"-cache"} =~ /^\</
Truth table0%0/5
A and B and C and not(D)
A= $lyricsB= $LyricFinder::Musixmatch::haveLyricsCacheC= $self->{"-cache"}D= not $self->{"-cache"} =~ /^\</
ABCDresult
0XXX0
10XX0
110X0
11100
11111
81 0 $self->_debug("=== WILL CACHE LYRICS! ===");
82 # cache the fetched lyrics, if we can:
83 0 my $cache = new LyricFinder::Cache(%{$self});
84 0 ▶ $cache->save($artist_in, $song_in, $lyrics) if ($cache);
Branch: 1 branch
BranchTrueFalseTotal
if $cache 0 0 0
85 }
86 0 return $lyrics;
87 }
88  
89 0 ▶sub _parse {
Subroutine _parse: not called CC=14 SCAR=53.5
90 0 my $self = shift;
91 0 my $html = shift;
92  
93 0 $self->_debug("Musixmatch::_parse()!");
94 0 my $goodbit = '';
95 0 my $text = '';
96 0 my $mm_status = '';
97 0 my $hs = HTML::Strip->new();
98  
99 #METHOD 1: MERGE LYRICS FROM BOTH (OR MORE?) LYRIC "SPANS" MM LIKES TO BREAK LYRICS UP INTO
100 #2 SEPARATE SPAN TAGS: A SHORT (ABBREVIATED) PART, FOLLOWED BY THE REST OF THE LYRICS:
101 0 ▶ while ($html =~ s#\<span\s+class\=\"lyrics\_\_content\_\_(ok|warning|error)\"\>(.+?)\<\/span\>##s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ s[\<span\s+class\=\"lyrics\_\_content\_\_(ok|warning|error)\"\>(.+?)\<\/span\>][]s) 0 0 0
102 0 $mm_status = $1;
103 0 $goodbit = $2;
104 0 $text .= $hs->parse($goodbit) . "\r\n";
105 }
106 0 ▶ unless ($text =~ /[a-z]/i) { #IF METHOD 1 FAILS, THE FULL LYRICS ARE OFTEN FOUND IN THE FIRST "body":
Branch: 1 branch
BranchTrueFalseTotal
unless ($text =~ /[a-z]/i) 0 0 0
107 0 $html =~ s#\\\"#\x02#gs; #PROTECT ESCAPED QUOTES
108 0 ▶ if ($html =~ m#\"body\"\:\"([^\"]+)#s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ /\"body\"\:\"([^\"]+)/s) 0 0 0
109 0 $mm_status = 'error!';
110 0 ($goodbit = $1) =~ s#\x02#\"#gs;
111 0 $text .= $hs->parse($goodbit);
112 0 $text =~ s#\\n#\n#gs; #line-feeds are encoded literally in the JSON block.
113 }
114 }
115 0 ▶ if ($text =~ /[a-z]/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /[a-z]/i) { } 0 0 0
116 $text .= "\r\n(Musixmatch status: $mm_status)"
117 0 ▶ unless ((defined($self->{'-noextra'}) && $self->{'-noextra'}) || $mm_status !~ /\S/);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"-noextra"} and $self->{"-noextra"} or not $mm_status =~ /\S/ 0 0 0
Condition0%0/6
defined $self->{"-noextra"} and $self->{"-noextra"} or not $mm_status =~ /\S/
l!l&&r!l&&!r
000
defined $self->{"-noextra"} and $self->{"-noextra"}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $self->{"-noextra"} and $self->{"-noextra"} or not $mm_status =~ /\S/
defined $self->{"-noextra"} $self->{"-noextra"} not $mm_status =~ /\S/
Truth table0%0/5
A and B or not(C)
A= defined $self->{"-noextra"}B= $self->{"-noextra"}C= not $mm_status =~ /\S/
ABCresult
11X1
0X11
1011
0X00
1000
118  
119 #WHILE WE'RE AT IT, SEE IF WE HAVE A COVER IMAGE?!:
120 0 my $imgurl = '';
121 0 ▶ if ($html =~ m#\"coverImageHD\"\:\"([^\"]+)\"#) {
Branch: 3 branches
BranchTrueFalseTotal
if ($html =~ /\"coverImageHD\"\:\"([^\"]+)\"/) { } 0 0 0
elsif ($html =~ /\"coverImage\"\:\"([^\"]+)\"/) { } 0 0 0
elsif ($html =~ m[\<div\s+class\=\"banner\-album\-image\"\>(.+?)\<\/div\>]s) { } 0 0 0
122 0 $imgurl = $1;
123 } elsif ($html =~ m#\"coverImage\"\:\"([^\"]+)\"#) {
124 0 $imgurl = $1;
125 #DEPRECIATED?!:
126 } elsif ($html =~ m#\<div\s+class\=\"banner\-album\-image\"\>(.+?)\<\/div\>#s) {
127 0 my $imgdiv = $1;
128 0 ▶ $imgurl = $1 if ($imgdiv =~ m#\<img\s+src\=\"([^\"]+)#s);
Branch: 1 branch
BranchTrueFalseTotal
if $imgdiv =~ /\<img\s+src\=\"([^\"]+)/s 0 0 0
129 }
130  
131 0 ▶ if ($imgurl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($imgurl) 0 0 0
132 0 ▶ $imgurl = 'https:' . $imgurl if ($imgurl =~ m#^//#);
Branch: 1 branch
BranchTrueFalseTotal
if $imgurl =~ m[^//] 0 0 0
133 0 $self->{'image_url'} = $imgurl;
134 }
135  
136 0 return $self->_normalize_lyric_text($self->_html2text($text));
137 } else {
138 0 carp($self->{'Error'} = "e:$Source - Failed to identify lyrics on result page.");
139 0 return '';
140 }
141 }
142  
143 1 1
144  
145 __END__
« LyricFinder.pm ↑ Summary LyricFinder/Lrclib.pm »