File Coverage

blib/lib/LyricFinder/ApiLyricsOvh.pm
Criterion Covered Total %
statement 17 65 26.1
branch 0 18 0.0
condition 0 24 0.0
subroutine 6 9 66.6
pod 2 2 100.0
total 25 118 21.1


line stmt bran cond sub pod time code
1             package LyricFinder::ApiLyricsOvh;
2              
3 1     1   8 use strict;
  1         2  
  1         34  
4 1     1   5 use warnings;
  1         3  
  1         27  
5 1     1   5 use Carp;
  1         2  
  1         77  
6 1     1   7 use parent 'LyricFinder::_Class';
  1         2  
  1         7  
7              
8             our $haveLyricsCache;
9             BEGIN {
10 1     1   98 $haveLyricsCache = 0;
11 1     1   79 eval "use LyricFinder::Cache; \$haveLyricsCache = 1; 1";
  1         16  
  1         3  
  1         33  
12             }
13              
14             my $Source = 'ApiLyricsOvh';
15             my $Site = 'https://api.lyrics.ovh';
16             my $DEBUG = 0;
17              
18             sub new
19             {
20 0     0 1   my $class = shift;
21              
22 0           my $self = $class->SUPER::new($Source, @_);
23 0           @{$self->{'_fetchers'}} = ($Source);
  0            
24 0           unshift(@{$self->{'_fetchers'}}, 'Cache') if ($haveLyricsCache
25 0 0 0       && $self->{'-cache'} && $self->{'-cache'} !~ /^\>/);
      0        
26 0           bless $self, $class; #BLESS IT!
27              
28 0           return $self;
29             }
30              
31             sub fetch {
32 0     0 1   my ($self, $artist_in, $song_in) = @_;
33              
34 0           $self->_debug("Letras::fetch($artist_in, $song_in)!");
35              
36 0 0         return '' unless ($self->_check_inputs($artist_in, $song_in));
37 0 0         return '' if ($self->{'Error'} ne 'Ok');
38              
39             # first, see if we've got it cached:
40 0           $self->_debug("i:haveCache=$haveLyricsCache= -cachedir=".$self->{'-cache'}."=");
41 0 0 0       if ($haveLyricsCache && $self->{'-cache'} && $self->{'-cache'} !~ /^\>/) {
      0        
42 0           my $cache = new LyricFinder::Cache(%{$self});
  0            
43 0 0         if ($cache) {
44 0           my $lyrics = $cache->fetch($artist_in, $song_in);
45 0 0 0       if (defined($lyrics) && $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 0 0       if ($lyrics && $haveLyricsCache && $self->{'-cache'} && $self->{'-cache'} !~ /^\
      0        
      0        
70 0           $self->_debug("=== WILL CACHE LYRICS! ===");
71             # cache the fetched lyrics, if we can:
72 0           my $cache = new LyricFinder::Cache(%{$self});
  0            
73 0 0         $cache->save($artist_in, $song_in, $lyrics) if ($cache);
74             }
75 0           return $lyrics;
76             }
77              
78             sub _parse {
79 0     0     my $self = shift;
80 0           my $html = shift;
81              
82 0           $self->_debug("ApiLyricsOvh::_parse()!");
83 0 0         if (my ($goodbit) = $html =~ m{\{\"lyrics\"\:\"([^\"]+)\"}msi)
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
100              
101             __END__