| 1 | package LyricFinder::Lrclib; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 URI::Escape; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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 = 'Lrclib'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | my $Site = 'https://lrclib.net'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/6
$LyricFinder::Lrclib::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\>/
$LyricFinder::Lrclib::haveLyricsCache and $self->{"-cache"}
MC/DC0%0/3
$LyricFinder::Lrclib::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\>/
$LyricFinder::Lrclib::haveLyricsCache
$self->{"-cache"}
not $self->{"-cache"} =~ /^\>/
Truth table0%0/4
A and B and not(C)
A= $LyricFinder::Lrclib::haveLyricsCacheB= $self->{"-cache"}C= not $self->{"-cache"} =~ /^\>/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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("Lrclib::fetch($artist_in, $song_in)!"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | return '' unless ($self->_check_inputs($artist_in, $song_in)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | return '' if ($self->{'Error'} ne 'Ok'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/6
$LyricFinder::Lrclib::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\>/
$LyricFinder::Lrclib::haveLyricsCache and $self->{"-cache"}
MC/DC0%0/3
$LyricFinder::Lrclib::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\>/
$LyricFinder::Lrclib::haveLyricsCache
$self->{"-cache"}
not $self->{"-cache"} =~ /^\>/
Truth table0%0/4
A and B and not(C)
A= $LyricFinder::Lrclib::haveLyricsCacheB= $self->{"-cache"}C= not $self->{"-cache"} =~ /^\>/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | my $cache = new LyricFinder::Cache(%{$self}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | if ($cache) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | my $lyrics = $cache->fetch($artist_in, $song_in); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | if (defined($lyrics) && $lyrics =~ /\w/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $lyrics and $lyrics =~ /\w/
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/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | (my $artist = $artist_in) =~ s#\s*\/.*$##; #ONLY USE 1ST ARTIST, IF MORE THAN ONE! | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | $artist = uri_escape_utf8($artist); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | $artist =~ s/\%20/\+/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | $artist =~ s/\%26/\&/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | $artist =~ s/\%28/\(/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | $artist =~ s/\%29/\)/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | $artist =~ s/\%3D/\=/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | my $song = $song_in; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | $song = uri_escape_utf8($song); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | $song =~ s/\%20/\+/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | $song =~ s/\%26/\&/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | $song =~ s/\%28/\(/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | $song =~ s/\%29/\)/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | $song =~ s/\%3D/\=/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | # Their URLs look like e.g.: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | # https://lrclib.net/api/get?artist_name=Borislav+Slavov&track_name=I+Want+to+Live | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | $self->{'Url'} = $Site . "/api/get?artist_name=${artist}&track_name=$song"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | my $lyrics = $self->_web_fetch($artist_in, $song_in); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if ($lyrics && $haveLyricsCache && $self->{'-cache'} && $self->{'-cache'} !~ /^\</) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/9
$lyrics and $LyricFinder::Lrclib::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\</
$lyrics and $LyricFinder::Lrclib::haveLyricsCache and $self->{"-cache"}
$lyrics and $LyricFinder::Lrclib::haveLyricsCache
MC/DC0%0/4
$lyrics and $LyricFinder::Lrclib::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\</
$lyrics
$LyricFinder::Lrclib::haveLyricsCache
$self->{"-cache"}
not $self->{"-cache"} =~ /^\</
Truth table0%0/5
A and B and C and not(D)
A= $lyricsB= $LyricFinder::Lrclib::haveLyricsCacheC= $self->{"-cache"}D= not $self->{"-cache"} =~ /^\</
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | $self->_debug("=== WILL CACHE LYRICS! ==="); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | # cache the fetched lyrics, if we can: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | my $cache = new LyricFinder::Cache(%{$self}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | $cache->save($artist_in, $song_in, $lyrics) if ($cache); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | return $lyrics; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | # Internal use only functions: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | sub _parse { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse: not called CC=12 SCAR=50.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | my $html = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | #NOTE: -synced: 'YES'(synced|plain), 'NO'(plain|bust), 'ONLY'(synced|bust), or 'OK'|''(plain|synced)! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | my $sync = (defined($self->{'-synced'}) && $self->{'-synced'}) ? $self->{'-synced'} : 'No'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->{"-synced"} and $self->{"-synced"}
MC/DC0%0/2
defined $self->{"-synced"} and $self->{"-synced"}
defined $self->{"-synced"}
$self->{"-synced"}
Truth table0%0/3
A and B
A= defined $self->{"-synced"}B= $self->{"-synced"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | $sync = 'yES' unless ($sync =~ /^(?:y|n|only|ok)/i); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | my $whichLyrics = ($sync !~ /^(?:NO|OK)/i) ? 'syncedLyrics' : 'plainLyrics'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | $self->_debug("Lrclib::_parse()!"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | my $lyrics; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | $html =~ s/\\\"/\x02QUOTE\x02/gs; #PROTECT ESCAPED QUOTES FROM NEXT REGEX!: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | for my $i (0,1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | $lyrics = ($html =~ m/\,\"$whichLyrics\"\:\"([^\"]+)\"(?:\,\"|\})/s) ? $1 : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | if (defined($lyrics) && length($lyrics) > 10) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
defined $lyrics and length $lyrics > 10
MC/DC0%0/2
defined $lyrics and length $lyrics > 10
defined $lyrics
length $lyrics > 10
Truth table0%0/3
A and B
A= defined $lyricsB= length $lyrics > 10
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | my $text = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | # convert literal "\" followed by "r" or "n", etc. to "\r" or "\n" characters respectively: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | eval "\$text = \"$lyrics\";"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | $text =~ s/\x02QUOTE\x02/\"/gs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | return $self->_normalize_lyric_text($self->_html2text($text)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | ▶ | } elsif ($i || $sync =~ /^(?:NO|ONLY)/i) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$i or $sync =~ /^(?:NO|ONLY)/i
MC/DC0%0/2
$i or $sync =~ /^(?:NO|ONLY)/i
$i
$sync =~ /^(?:NO|ONLY)/i
Truth table0%0/3
A or B
A= $iB= $sync =~ /^(?:NO|ONLY)/i
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | carp($self->{'Error'} = "e:$Source - Failed to identify lyrics on result page."); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | return ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | $whichLyrics = ($sync =~ /^YES/i) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | ? 'plainLyrics' : 'syncedLyrics'; #NO SYNCED LYRICS, TRY PLAIN-TEXT ONES: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | } # end of sub parse | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 1 | 1 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||