File Coverage

blib/lib/LyricFinder/AZLyrics.pm
Criterion Covered Total %
statement 20 69 28.9
branch 0 18 0.0
condition 0 24 0.0
subroutine 7 10 70.0
pod 2 2 100.0
total 29 123 23.5


line stmt bran cond sub pod time code
1             package LyricFinder::AZLyrics;
2              
3 1     1   5 use strict;
  1         2  
  1         52  
4 1     1   5 use warnings;
  1         2  
  1         19  
5 1     1   4 use Carp;
  1         1  
  1         51  
6 1     1   5 use HTML::Strip;
  1         1  
  1         25  
7 1     1   4 use parent 'LyricFinder::_Class';
  1         2  
  1         9  
8              
9             our $haveLyricsCache;
10             BEGIN {
11 1     1   72 $haveLyricsCache = 0;
12 1     1   58 eval "use LyricFinder::Cache; \$haveLyricsCache = 1; 1";
  1         6  
  1         1  
  1         16  
13             }
14              
15             my $Source = 'AZLyrics';
16             my $Site = 'https://www.azlyrics.com';
17             my $DEBUG = 0;
18              
19             sub new
20             {
21 0     0 1   my $class = shift;
22              
23 0           my $self = $class->SUPER::new($Source, @_);
24 0           @{$self->{'_fetchers'}} = ($Source);
  0            
25 0           unshift(@{$self->{'_fetchers'}}, 'Cache') if ($haveLyricsCache
26 0 0 0       && $self->{'-cache'} && $self->{'-cache'} !~ /^\>/);
      0        
27              
28 0           bless $self, $class; #BLESS IT!
29              
30 0           return $self;
31             }
32              
33             sub fetch {
34 0     0 1   my ($self, $artist_in, $song_in) = @_;
35              
36 0           $self->_debug("AZLyrics::fetch($artist_in, $song_in)!");
37              
38 0 0         return '' unless ($self->_check_inputs($artist_in, $song_in));
39 0 0         return '' if ($self->{'Error'} ne 'Ok');
40              
41             # first, see if we've got it cached:
42 0           $self->_debug("i:haveCache=$haveLyricsCache= -cachedir=".$self->{'-cache'}."=");
43 0 0 0       if ($haveLyricsCache && $self->{'-cache'} && $self->{'-cache'} !~ /^\>/) {
      0        
44 0           my $cache = new LyricFinder::Cache(%{$self});
  0            
45 0 0         if ($cache) {
46 0           my $lyrics = $cache->fetch($artist_in, $song_in);
47 0 0 0       if (defined($lyrics) && $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 =~ s/[^a-z0-9]//gi;
62 0           (my $song = $song_in) =~ s/[^a-z0-9]//gi;
63              
64             # Their URLs look like e.g.:
65             # https://www.azlyrics.com/lyrics/direstraits/heavyfuel
66 0           $self->{'Url'} = $Site . '/lyrics/'
67             . join('/', lc $artist, lc $song) . '.html';
68              
69 0           my $lyrics = $self->_web_fetch($artist_in, $song_in);
70 0 0 0       if ($lyrics && $haveLyricsCache && $self->{'-cache'} && $self->{'-cache'} !~ /^\
      0        
      0        
71 0           $self->_debug("=== WILL CACHE LYRICS! ===");
72             # cache the fetched lyrics, if we can:
73 0           my $cache = new LyricFinder::Cache(%{$self});
  0            
74 0 0         $cache->save($artist_in, $song_in, $lyrics) if ($cache);
75             }
76 0           return $lyrics;
77             }
78              
79             # Internal use only functions:
80              
81             sub _parse {
82 0     0     my $self = shift;
83 0           my $html = shift;
84              
85 0           $self->_debug("AZLyrics::_parse()!");
86 0 0         if (my ($goodbit) = $html =~
87             m{our licensing agreement. Sorry about that. \-\-\>(.+?)\<\/div\>}msi)
88             {
89             # Scoop out any credits for these lyrics:
90 0           while ($html =~ s{Thanks\s+to\s+(.+)}{}xi) {
91 0           (my $creditlist = $1) =~ s/\s+for\b.+$//o;
92 0           push @{$self->{'Credits'}}, split(/\,\s*/, $creditlist);
  0            
93             }
94              
95 0           my $hs = HTML::Strip->new();
96 0           my $text = $hs->parse($goodbit);
97              
98 0           return $self->_normalize_lyric_text($self->_html2text($text));
99             } else {
100 0           carp($self->{'Error'} = "e:$Source - Failed to identify lyrics on result page.");
101 0           return '';
102             }
103             } # end of sub parse
104              
105             1
106              
107             __END__