| 1 | package LyricFinder::ApiLyricsOvh; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | our $haveLyricsCache; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | $haveLyricsCache = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | eval "use LyricFinder::Cache; \$haveLyricsCache = 1; 1"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | my $Source = 'ApiLyricsOvh'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | my $Site = 'https://api.lyrics.ovh'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | my $DEBUG = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | sub new | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | my $self = $class->SUPER::new($Source, @_); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | @{$self->{'_fetchers'}} = ($Source); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | unshift(@{$self->{'_fetchers'}}, 'Cache') if ($haveLyricsCache | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | && $self->{'-cache'} && $self->{'-cache'} !~ /^\>/); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$LyricFinder::ApiLyricsOvh::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\>/
$LyricFinder::ApiLyricsOvh::haveLyricsCache and $self->{"-cache"}
MC/DC0%0/3
$LyricFinder::ApiLyricsOvh::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\>/
$LyricFinder::ApiLyricsOvh::haveLyricsCache
$self->{"-cache"}
not $self->{"-cache"} =~ /^\>/
Truth table0%0/4
A and B and not(C)
A= $LyricFinder::ApiLyricsOvh::haveLyricsCacheB= $self->{"-cache"}C= not $self->{"-cache"} =~ /^\>/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | bless $self, $class; #BLESS IT! | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | sub fetch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fetch: not called CC=14 SCAR=53.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | my ($self, $artist_in, $song_in) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | $self->_debug("Letras::fetch($artist_in, $song_in)!"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | return '' unless ($self->_check_inputs($artist_in, $song_in)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | return '' if ($self->{'Error'} ne 'Ok'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # first, see if we've got it cached: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | $self->_debug("i:haveCache=$haveLyricsCache= -cachedir=".$self->{'-cache'}."="); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | if ($haveLyricsCache && $self->{'-cache'} && $self->{'-cache'} !~ /^\>/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$LyricFinder::ApiLyricsOvh::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\>/
$LyricFinder::ApiLyricsOvh::haveLyricsCache and $self->{"-cache"}
MC/DC0%0/3
$LyricFinder::ApiLyricsOvh::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\>/
$LyricFinder::ApiLyricsOvh::haveLyricsCache
$self->{"-cache"}
not $self->{"-cache"} =~ /^\>/
Truth table0%0/4
A and B and not(C)
A= $LyricFinder::ApiLyricsOvh::haveLyricsCacheB= $self->{"-cache"}C= not $self->{"-cache"} =~ /^\>/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | my $cache = new LyricFinder::Cache(%{$self}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | if ($cache) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | my $lyrics = $cache->fetch($artist_in, $song_in); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 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/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | $self->_debug("..Got lyrics from cache."); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | $self->{'Source'} = 'Cache'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | $self->{'Site'} = $cache->site(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | $self->{'Url'} = $cache->url(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | return $lyrics; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | $self->{'Site'} = $Site; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | (my $artist = $artist_in) =~ s#\s*\/.*$##; #ONLY USE 1ST ARTIST, IF MORE THAN ONE! | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | $artist =~ s/\s+/\%20/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | $artist =~ s/[^a-z0-9\%]//gi; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | (my $song = $song_in) =~ s/\s+/\%20/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | $song =~ s/[^a-z0-9\%]//gi; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # Their URLs look like e.g.: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # https://api.lyrics.ovh/v1/Dire%20straits/heavy%sfuel%s | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | $self->{'Url'} = "${Site}/v1/${artist}/$song"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | my $lyrics = $self->_web_fetch($artist_in, $song_in); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | if ($lyrics && $haveLyricsCache && $self->{'-cache'} && $self->{'-cache'} !~ /^\</) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/9
$lyrics and $LyricFinder::ApiLyricsOvh::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\</
$lyrics and $LyricFinder::ApiLyricsOvh::haveLyricsCache and $self->{"-cache"}
$lyrics and $LyricFinder::ApiLyricsOvh::haveLyricsCache
MC/DC0%0/4
$lyrics and $LyricFinder::ApiLyricsOvh::haveLyricsCache and $self->{"-cache"} and not $self->{"-cache"} =~ /^\</
$lyrics
$LyricFinder::ApiLyricsOvh::haveLyricsCache
$self->{"-cache"}
not $self->{"-cache"} =~ /^\</
Truth table0%0/5
A and B and C and not(D)
A= $lyricsB= $LyricFinder::ApiLyricsOvh::haveLyricsCacheC= $self->{"-cache"}D= not $self->{"-cache"} =~ /^\</
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | $self->_debug("=== WILL CACHE LYRICS! ==="); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | # cache the fetched lyrics, if we can: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | my $cache = new LyricFinder::Cache(%{$self}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | $cache->save($artist_in, $song_in, $lyrics) if ($cache); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | return $lyrics; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | sub _parse { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | my $html = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | $self->_debug("ApiLyricsOvh::_parse()!"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if (my ($goodbit) = $html =~ m{\{\"lyrics\"\:\"([^\"]+)\"}msi) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | my $text = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | # convert literal "\" followed by "r" or "n", etc. to "\r" or "\n" characters respectively: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | eval "\$text = \"$goodbit\";"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | # fix apparent site bug where they use "\n\n" where they appear to mean "\r\n" (excess double-lines): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | $text =~ s/\n\n/\n/gs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | return $self->_normalize_lyric_text($self->_html2text($text)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | carp($self->{'Error'} = "e:$Source - Failed to identify lyrics on result page."); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | return ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | 1 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||