LyricFinder/Genius.pm

filter: Statementstmt 29.4% 25 / 85 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 70.0% 7 / 10 Podpod 100% 2 / 2 totaltotal 22.2% 34 / 153 CC 20 SCAR 46.7
CC
20
Cov
19%
fetch
210.0
new
20.0
_parse
20.0
CRAP 107.1
« LyricFinder/AZLyrics.pm ↑ Summary LyricFinder/Cache.pm »
1 package LyricFinder::Genius;
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 = 'Genius';
16 1 my $Site = 'https://genius.com';
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::Genius::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\>/ 0 0 0
Condition0%0/6
$LyricFinder::Genius::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\>/
!ll&&!rl&&r
000
$LyricFinder::Genius::haveLyricsCache and $self->{"-cache"}
!ll&&!rl&&r
000
MC/DC0%0/3
$LyricFinder::Genius::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\>/
$LyricFinder::Genius::haveLyricsCache $self->{"-cache"} not $self->{"-cache"} =~ /^\>/
Truth table0%0/4
A and B and not(C)
A= $LyricFinder::Genius::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("Genius::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 # first, see if we've got it cached:
42 0 $self->_debug("i:haveCache=$haveLyricsCache= -cachedir=".$self->{'-cache'}."=");
43 0 ▶ if ($haveLyricsCache && $self->{'-cache'} && $self->{'-cache'} !~ /^\>/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LyricFinder::Genius::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\>/) 0 0 0
Condition0%0/6
$LyricFinder::Genius::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\>/
!ll&&!rl&&r
000
$LyricFinder::Genius::haveLyricsCache and $self->{"-cache"}
!ll&&!rl&&r
000
MC/DC0%0/3
$LyricFinder::Genius::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\>/
$LyricFinder::Genius::haveLyricsCache $self->{"-cache"} not $self->{"-cache"} =~ /^\>/
Truth table0%0/4
A and B and not(C)
A= $LyricFinder::Genius::haveLyricsCacheB= $self->{"-cache"}C= not $self->{"-cache"} =~ /^\>/
ABCresult
0XX0
10X0
1100
1111
44 0 my $cache = new LyricFinder::Cache(%{$self});
45 0 ▶ if ($cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache) 0 0 0
46 0 my $lyrics = $cache->fetch($artist_in, $song_in);
47 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
48 0 $self->_debug("..Got lyrics from cache.");
49 0 $self->{'Source'} = 'Cache';
50 0 $self->{'Site'} = $cache->site();
51 0 $self->{'Url'} = $cache->url();
52  
53 0 return $lyrics;
54 }
55 }
56 }
57  
58 0 $self->{'Site'} = $Site;
59  
60 0 $artist_in = $self->_remove_accents($artist_in);
61 0 $song_in = $self->_remove_accents($song_in);
62  
63 # Their URLs look like e.g.:
64 # https://genius.com/<artist>-<title>-lyrics
65 0 ($self->{'Url'} = $artist_in) =~ s#\s*\/\s*# and #; #ONLY USE 1ST ARTIST, IF MORE THAN ONE!
66 0 my $artist = $artist_in;
67 0 (my $song = $song_in) =~ s#\s*\/\s*#\-#g; #FIX SONGS WITH "/" IN THEM!
68 0 $self->{'Url'} .= " ${song}-lyrics";
69 0 $self->{'Url'} =~ s/\&/and/g;
70 0 $self->{'Url'} =~ s/ +/\-/g;
71 0 $self->{'Url'} =~ s/[^a-zA-Z0-9\-]+//g;
72 0 $self->{'Url'} = $Site . '/' . $self->{'Url'};
73  
74 0 my $lyrics = $self->_web_fetch($artist_in, $song_in);
75 0 ▶ if ($lyrics) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lyrics) 0 0 0
76 0 $lyrics =~ s/[\d ]*Embed\s+Cancel\s+How to Format Lyrics\:.+$/\n/s; #REMOVE TRAILING GENIUS SPAM! :^/
77 0 $lyrics =~ s/\s*\([\w\s]*\)\s*$/\n/s; #REMOVE MORE OF IT!
78 0 ▶ if ($haveLyricsCache && $self->{'-cache'} && $self->{'-cache'} !~ /^\</) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LyricFinder::Genius::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\</) 0 0 0
Condition0%0/6
$LyricFinder::Genius::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\</
!ll&&!rl&&r
000
$LyricFinder::Genius::haveLyricsCache and $self->{"-cache"}
!ll&&!rl&&r
000
MC/DC0%0/3
$LyricFinder::Genius::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\</
$LyricFinder::Genius::haveLyricsCache $self->{"-cache"} not $self->{"-cache"} =~ /^\</
Truth table0%0/4
A and B and not(C)
A= $LyricFinder::Genius::haveLyricsCacheB= $self->{"-cache"}C= not $self->{"-cache"} =~ /^\</
ABCresult
0XX0
10X0
1100
1111
79 0 $self->_debug("=== WILL CACHE LYRICS! ===");
80 # cache the fetched lyrics, if we can:
81 0 my $cache = new LyricFinder::Cache(%{$self});
82 0 ▶ $cache->save($artist_in, $song_in, $lyrics) if ($cache);
Branch: 1 branch
BranchTrueFalseTotal
if $cache 0 0 0
83 }
84 }
85 0 return $lyrics;
86 }
87  
88 0 ▶sub _parse {
Subroutine _parse: not called CC=4 SCAR=30.0
89 0 my $self = shift;
90 0 my $html = shift;
91  
92 0 $self->_debug("Genius::_parse()!");
93 0 ▶ if (my ($goodbit) = $html =~
Branch: 1 branch
BranchTrueFalseTotal
if (my($goodbit) = $html =~ /\<div\s+id\=\"lyrics\"[^\>]*\>(.+?)\<h1/ism) { } 0 0 0
94 # m{\<div\s+class\=\"lyrics\"\>(.+)\<\!\-\-\/sse\-\-\>}msi)
95 m{\<div\s+id\=\"lyrics\"[^\>]*\>(.+?)\<h1}msi)
96 {
97 0 my $hs = HTML::Strip->new();
98 0 $goodbit =~ s#\<\/?p\>#\r\n#gsi;
99 0 $goodbit =~ s#\<br\/?\>#\r\n#gsi;
100 0 my $text = $hs->parse($goodbit);
101  
102 #WHILE WE'RE AT IT, SEE IF WE HAVE A COVER IMAGE?!:
103 0 ▶ if ($html =~ m#\<noscript\>\s*\<img\s+src\=\"([^\"]+)#s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ /\<noscript\>\s*\<img\s+src\=\"([^\"]+)/s) 0 0 0
104 0 my $imgurl = $1;
105 0 ▶ $imgurl = 'https:' . $imgurl if ($imgurl =~ m#^//#);
Branch: 1 branch
BranchTrueFalseTotal
if $imgurl =~ m[^//] 0 0 0
106 0 $self->{'image_url'} = $imgurl;
107 }
108  
109 0 return $self->_normalize_lyric_text($self->_html2text($text));
110 } else {
111 0 carp($self->{'Error'} = "e:$Source - Failed to identify lyrics on result page.");
112 0 return '';
113 }
114 }
115  
116 1 1
117  
118 __END__
« LyricFinder/AZLyrics.pm ↑ Summary LyricFinder/Cache.pm »