| 1 | package LyricFinder::Cache; | ||||||||||||||||||||||||||||
| 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 parent 'LyricFinder::_Class'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | my $Source = 'Cache'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | sub new | ||||||||||||||||||||||||||||
| 11 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 12 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 0 | my $self = $class->SUPER::new($Source, @_); | |||||||||||||||||||||||||||
| 15 | 0 | @{$self->{'_fetchers'}} = ($Source); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 0 | bless $self, $class; #BLESS IT! | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 0 | return $self; | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub site { | ||||||||||||||||||||||||||
|
Subroutine
site: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | return 'none' unless ($self->{'-cache'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 0 | return 'file://'.$self->{'-cache'}; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # Allow user to specify a different user-agent: | ||||||||||||||||||||||||||||
| 30 | 0 | ▶ | sub fetch { | ||||||||||||||||||||||||||
|
Subroutine
fetch: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 32 | 0 | my ($artist, $song) = @_; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | $self->_debug("i:CACHE::fetch($artist, $song)!"); | |||||||||||||||||||||||||||
| 35 | # reset the error var, change it if an error occurs. | ||||||||||||||||||||||||||||
| 36 | 0 | $self->{'Error'} = 'Ok'; | |||||||||||||||||||||||||||
| 37 | 0 | $self->{'Url'} = ''; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | unless ($artist && $song) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$artist and $song
MC/DC0%0/2
$artist and $song
$artist
$song
Truth table0%0/3
A and B
A= $artistB= $song
| |||||||||||||||||||||||||||||
| 40 | 0 | carp($self->{'Error'} = 'e:Cache.fetch() called without artist and song!'); | |||||||||||||||||||||||||||
| 41 | 0 | return; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | unless ($self->{'-cache'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | carp($self->{'Error'} = 'e:Cache.fetch() called without a cache directory specified!'); | |||||||||||||||||||||||||||
| 46 | 0 | return; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if ($self->{'-cache'} =~ /^\>/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | carp($self->{'Error'} = 'e:Cache.fetch() called but cache directory writeonly (">" specified)!'); | |||||||||||||||||||||||||||
| 51 | 0 | return; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | $artist =~ s#\s*\/.*$##; #ONLY USE 1ST ARTIST, IF MORE THAN ONE! | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # Their URLs look like e.g.: | ||||||||||||||||||||||||||||
| 57 | #https://www.musixmatch.com/lyrics/Artist-name/Title | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | my $LOCALDIR = $self->{'-cache'}; | |||||||||||||||||||||||||||
| 60 | 0 | $LOCALDIR =~ s#^\<##; #STRIP OFF ANY LEADING DIRECTIONAL INDICATOR. | |||||||||||||||||||||||||||
| 61 | 0 | $LOCALDIR =~ s#\\#\/#g; #DE-DOSIFY WINDOWS FNS. | |||||||||||||||||||||||||||
| 62 | 0 | ▶ | $LOCALDIR .= '/' unless ($LOCALDIR =~ m#\/#); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | $LOCALDIR =~ s#^file\:\/\/##; #CONVERT TO FILE IF URI | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | unless (-d "$LOCALDIR") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | carp($self->{'Error'} = 'e:Cache.fetch() called but cache directory not a valid directory'); | |||||||||||||||||||||||||||
| 67 | 0 | return; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | $self->{'Url'} = $LOCALDIR . '/' . $artist . '/' . $song . '.lrc'; | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | if (-f $self->{'Url'} && open (IN, $self->{'Url'})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-f $self->{"Url"} and open IN, $self->{"Url"}
MC/DC0%0/2
-f $self->{"Url"} and open IN, $self->{"Url"}
-f $self->{"Url"}
open IN, $self->{"Url"}
Truth table0%0/3
A and B
A= -f $self->{"Url"}B= open IN, $self->{"Url"}
| |||||||||||||||||||||||||||||
| 72 | 0 | my $lyrics = ''; | |||||||||||||||||||||||||||
| 73 | 0 | ▶ | while (<IN>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | $lyrics .= $_; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 0 | close IN; | |||||||||||||||||||||||||||
| 77 | 0 | $lyrics = $self->_parse($lyrics); | |||||||||||||||||||||||||||
| 78 | 0 | return $lyrics; | |||||||||||||||||||||||||||
| 79 | } else { | ||||||||||||||||||||||||||||
| 80 | 0 | $self->{'Error'} = 'e:Cache - Lyrics not found'; | |||||||||||||||||||||||||||
| 81 | 0 | return; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | sub save { | ||||||||||||||||||||||||||
|
Subroutine
save: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | 0 | my ($self, $artist, $song, $lyrics) = @_; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | $self->_debug('i:CACHE::save('.$self->{'-cache'}.')!'); | |||||||||||||||||||||||||||
| 89 | 0 | ▶ | return 0 if ($self->{'-cache'} =~ /^\</); #NO SAVE IF CACHE IS INPUT ONLY. | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | my $LOCALDIR = $self->{'-cache'}; | |||||||||||||||||||||||||||
| 92 | 0 | $LOCALDIR =~ s#^\>##; | |||||||||||||||||||||||||||
| 93 | 0 | $LOCALDIR =~ s#\\#\/#g; #DE-DOSIFY WINDOWS FNS. | |||||||||||||||||||||||||||
| 94 | 0 | ▶ | $LOCALDIR .= '/' unless ($LOCALDIR =~ m#\/$#); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | $LOCALDIR =~ s#^file\:\/\/##; #CONVERT TO FILE IF URI | |||||||||||||||||||||||||||
| 96 | 0 | ▶ | if (-d "$LOCALDIR") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | (my $artistDir = $LOCALDIR . $artist) =~ s/([\&])/\\$1/g; | |||||||||||||||||||||||||||
| 98 | 0 | ▶ | mkdir "$artistDir" unless (-d $artistDir); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | if (-d "$artistDir") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | (my $songFid = $LOCALDIR . $artist . '/' . $song . '.lrc') =~ s/([\&])/\\$1/g; | |||||||||||||||||||||||||||
| 101 | 0 | $self->_debug("------songFid=$songFid="); | |||||||||||||||||||||||||||
| 102 | 0 | ▶ | if (open OUT, ">$songFid") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | { | ||||||||||||||||||||||||||||
| 104 | 0 | print OUT $lyrics; | |||||||||||||||||||||||||||
| 105 | 0 | $self->_debug("------WROTE IT!!!------\n"); | |||||||||||||||||||||||||||
| 106 | 0 | close OUT; | |||||||||||||||||||||||||||
| 107 | 0 | return 1; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | else | ||||||||||||||||||||||||||||
| 110 | { | ||||||||||||||||||||||||||||
| 111 | 0 | carp($self->{'Error'} = "e:$Source - Could not cache lyrics for ($artist, $song) to ($songFid) (could not open file: $!)!"); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | else | ||||||||||||||||||||||||||||
| 115 | { | ||||||||||||||||||||||||||||
| 116 | 0 | carp($self->{'Error'} = "e:$Source - Could not cache lyrics for ($artist, $song) dir ($artistDir) still does not exist?!"); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | } else { | ||||||||||||||||||||||||||||
| 119 | 0 | carp($self->{'Error'} = "e:$Source - Could not cache lyrics for ($artist, $song): $LOCALDIR is not a directory!"); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | 0 | return 0; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | sub _parse { | ||||||||||||||||||||||||||
|
Subroutine
_parse: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 125 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 126 | 0 | my $text = shift; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | $self->_debug("i:CACHE::_parse()!\n"); | |||||||||||||||||||||||||||
| 129 | 0 | ▶ | if ($text =~ /\w/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | return $self->_normalize_lyric_text($text); | |||||||||||||||||||||||||||
| 131 | } else { | ||||||||||||||||||||||||||||
| 132 | 0 | carp($self->{'Error'} = "e:$Source - Failed to identify lyrics in cache."); | |||||||||||||||||||||||||||
| 133 | 0 | return; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 1 | 1 | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | __END__ | ||||||||||||||||||||||||||||