line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package LyricFinder::Cache; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
4
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
22
|
|
5
|
1
|
|
|
1
|
|
4
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
54
|
|
6
|
1
|
|
|
1
|
|
5
|
use parent 'LyricFinder::_Class'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
6
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
my $Source = 'Cache'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new |
11
|
|
|
|
|
|
|
{ |
12
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
13
|
|
|
|
|
|
|
|
14
|
0
|
|
|
|
|
|
my $self = $class->SUPER::new($Source, @_); |
15
|
0
|
|
|
|
|
|
@{$self->{'_fetchers'}} = ($Source); |
|
0
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
0
|
|
|
|
|
|
bless $self, $class; #BLESS IT! |
18
|
|
|
|
|
|
|
|
19
|
0
|
|
|
|
|
|
return $self; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub site { |
23
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
24
|
|
|
|
|
|
|
|
25
|
0
|
0
|
|
|
|
|
return 'none' unless ($self->{'-cache'}); |
26
|
0
|
|
|
|
|
|
return 'file://'.$self->{'-cache'}; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# Allow user to specify a different user-agent: |
30
|
|
|
|
|
|
|
sub fetch { |
31
|
0
|
|
|
0
|
1
|
|
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
|
0
|
0
|
|
|
|
unless ($artist && $song) { |
40
|
0
|
|
|
|
|
|
carp($self->{'Error'} = 'e:Cache.fetch() called without artist and song!'); |
41
|
0
|
|
|
|
|
|
return; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
0
|
0
|
|
|
|
|
unless ($self->{'-cache'}) { |
45
|
0
|
|
|
|
|
|
carp($self->{'Error'} = 'e:Cache.fetch() called without a cache directory specified!'); |
46
|
0
|
|
|
|
|
|
return; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
0
|
0
|
|
|
|
|
if ($self->{'-cache'} =~ /^\>/) { |
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
|
0
|
|
|
|
|
$LOCALDIR .= '/' unless ($LOCALDIR =~ m#\/#); |
63
|
0
|
|
|
|
|
|
$LOCALDIR =~ s#^file\:\/\/##; #CONVERT TO FILE IF URI |
64
|
|
|
|
|
|
|
|
65
|
0
|
0
|
|
|
|
|
unless (-d "$LOCALDIR") { |
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
|
0
|
0
|
|
|
|
if (-f $self->{'Url'} && open (IN, $self->{'Url'})) { |
72
|
0
|
|
|
|
|
|
my $lyrics = ''; |
73
|
0
|
|
|
|
|
|
while () { |
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
|
|
|
|
|
|
|
sub save { |
86
|
0
|
|
|
0
|
1
|
|
my ($self, $artist, $song, $lyrics) = @_; |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
$self->_debug('i:CACHE::save('.$self->{'-cache'}.')!'); |
89
|
0
|
0
|
|
|
|
|
return 0 if ($self->{'-cache'} =~ /^\); #NO SAVE IF CACHE IS INPUT ONLY. |
90
|
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
my $LOCALDIR = $self->{'-cache'}; |
92
|
0
|
|
|
|
|
|
$LOCALDIR =~ s#^\>##; |
93
|
0
|
|
|
|
|
|
$LOCALDIR =~ s#\\#\/#g; #DE-DOSIFY WINDOWS FNS. |
94
|
0
|
0
|
|
|
|
|
$LOCALDIR .= '/' unless ($LOCALDIR =~ m#\/$#); |
95
|
0
|
|
|
|
|
|
$LOCALDIR =~ s#^file\:\/\/##; #CONVERT TO FILE IF URI |
96
|
0
|
0
|
|
|
|
|
if (-d "$LOCALDIR") { |
97
|
0
|
|
|
|
|
|
(my $artistDir = $LOCALDIR . $artist) =~ s/([\&])/\\$1/g; |
98
|
0
|
0
|
|
|
|
|
mkdir "$artistDir" unless (-d $artistDir); |
99
|
0
|
0
|
|
|
|
|
if (-d "$artistDir") { |
100
|
0
|
|
|
|
|
|
(my $songFid = $LOCALDIR . $artist . '/' . $song . '.lrc') =~ s/([\&])/\\$1/g; |
101
|
0
|
|
|
|
|
|
$self->_debug("------songFid=$songFid="); |
102
|
0
|
0
|
|
|
|
|
if (open OUT, ">$songFid") |
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
|
|
|
|
|
|
|
sub _parse { |
125
|
0
|
|
|
0
|
|
|
my $self = shift; |
126
|
0
|
|
|
|
|
|
my $text = shift; |
127
|
|
|
|
|
|
|
|
128
|
0
|
|
|
|
|
|
$self->_debug("i:CACHE::_parse()!\n"); |
129
|
0
|
0
|
|
|
|
|
if ($text =~ /\w/) { |
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 |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
__END__ |