File Coverage

blib/lib/LyricFinder/Genius.pm
Criterion Covered Total %
statement 20 79 25.3
branch 0 24 0.0
condition 0 21 0.0
subroutine 7 10 70.0
pod 2 2 100.0
total 29 136 21.3


line stmt bran cond sub pod time code
1             package LyricFinder::Genius;
2              
3 1     1   5 use strict;
  1         1  
  1         23  
4 1     1   4 use warnings;
  1         2  
  1         18  
5 1     1   4 use Carp;
  1         2  
  1         48  
6 1     1   6 use HTML::Strip;
  1         1  
  1         17  
7 1     1   3 use parent 'LyricFinder::_Class';
  1         2  
  1         3  
8              
9             our $haveLyricsCache;
10             BEGIN {
11 1     1   69 $haveLyricsCache = 0;
12 1     1   59 eval "use LyricFinder::Cache; \$haveLyricsCache = 1; 1";
  1         6  
  1         1  
  1         17  
13             }
14              
15             my $Source = 'Genius';
16             my $Site = 'https://genius.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("Genius::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           $artist_in = $self->_remove_accents($artist_in);
61 0           $song_in = $self->_remove_accents($song_in);
62              
63             # Their URLs look like e.g.:
64             # https://genius.com/--lyrics </td> </tr> <tr> <td class="h" > <a name="65">65</a> </td> <td class="c0" > <a href="#66"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> ($self->{'Url'} = $artist_in) =~ s#\s*\/\s*# and #; #ONLY USE 1ST ARTIST, IF MORE THAN ONE! </td> </tr> <tr> <td class="h" > <a name="66">66</a> </td> <td class="c0" > <a href="#67"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> my $artist = $artist_in; </td> </tr> <tr> <td class="h" > <a name="67">67</a> </td> <td class="c0" > <a href="#68"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> (my $song = $song_in) =~ s#\s*\/\s*#\-#g; #FIX SONGS WITH "/" IN THEM! </td> </tr> <tr> <td class="h" > <a name="68">68</a> </td> <td class="c0" > <a href="#69"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> $self->{'Url'} .= " ${song}-lyrics"; </td> </tr> <tr> <td class="h" > <a name="69">69</a> </td> <td class="c0" > <a href="#70"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> $self->{'Url'} =~ s/\&/and/g; </td> </tr> <tr> <td class="h" > <a name="70">70</a> </td> <td class="c0" > <a href="#71"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> $self->{'Url'} =~ s/ +/\-/g; </td> </tr> <tr> <td class="h" > <a name="71">71</a> </td> <td class="c0" > <a href="#72"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> $self->{'Url'} =~ s/[^a-zA-Z0-9\-]+//g; </td> </tr> <tr> <td class="h" > <a name="72">72</a> </td> <td class="c0" > <a href="#74"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> $self->{'Url'} = $Site . '/' . $self->{'Url'}; </td> </tr> <tr> <td class="h" > <a name="73">73</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="74">74</a> </td> <td class="c0" > <a href="#75"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> my $lyrics = $self->_web_fetch($artist_in, $song_in); </td> </tr> <tr> <td class="h" > <a name="75">75</a> </td> <td class="c0" > <a href="#76"> 0 </a> </td> <td class="c0" > <a href="blib-lib-LyricFinder-Genius-pm--branch.html#75-1"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> if ($lyrics) { </td> </tr> <tr> <td class="h" > <a name="76">76</a> </td> <td class="c0" > <a href="#77"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> $lyrics =~ s/[\d ]*Embed Cancel How to Format Lyrics\:.+$/\n/s; #REMOVE TRAILING GENIUS SPAM! :^/ </td> </tr> <tr> <td class="h" > <a name="77">77</a> </td> <td class="c0" > <a href="#78"> 0 </a> </td> <td class="c0" > <a href="blib-lib-LyricFinder-Genius-pm--branch.html#77-1"> 0 </a> </td> <td class="c0" > <a href="blib-lib-LyricFinder-Genius-pm--condition.html#77-1"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> if ($haveLyricsCache && $self->{'-cache'} && $self->{'-cache'} !~ /^\</) { </td> </tr> <tr> <td class="h" > <a > </a> </td> <td >   </td> <td >   </td> <td class="c0" > <a href="blib-lib-LyricFinder-Genius-pm--condition.html#-2"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="78">78</a> </td> <td class="c0" > <a href="#80"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> $self->_debug("=== WILL CACHE LYRICS! ==="); </td> </tr> <tr> <td class="h" > <a name="79">79</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> # cache the fetched lyrics, if we can: </td> </tr> <tr> <td class="h" > <a name="80">80</a> </td> <td class="c0" > <a href="# "> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> my $cache = new LyricFinder::Cache(%{$self}); </td> </tr> <tr> <td class="h" > <a > </a> </td> <td class="c0" > <a href="#81"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="81">81</a> </td> <td class="c0" > <a href="#84"> 0 </a> </td> <td class="c0" > <a href="blib-lib-LyricFinder-Genius-pm--branch.html#81-1"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> $cache->save($artist_in, $song_in, $lyrics) if ($cache); </td> </tr> <tr> <td class="h" > <a name="82">82</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> } </td> </tr> <tr> <td class="h" > <a name="83">83</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> } </td> </tr> <tr> <td class="h" > <a name="84">84</a> </td> <td class="c0" > <a href="#88"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> return $lyrics; </td> </tr> <tr> <td class="h" > <a name="85">85</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> } </td> </tr> <tr> <td class="h" > <a name="86">86</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="87">87</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> sub _parse { </td> </tr> <tr> <td class="h" > <a name="88">88</a> </td> <td class="c0" > <a href="#89"> 0 </a> </td> <td >   </td> <td >   </td> <td class="c0" > <a href="blib-lib-LyricFinder-Genius-pm--subroutine.html#88-1"> 0 </a> </td> <td >   </td> <td >   </td> <td class="s"> my $self = shift; </td> </tr> <tr> <td class="h" > <a name="89">89</a> </td> <td class="c0" > <a href="#91"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> my $html = shift; </td> </tr> <tr> <td class="h" > <a name="90">90</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="91">91</a> </td> <td class="c0" > <a href="#92"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> $self->_debug("Genius::_parse()!"); </td> </tr> <tr> <td class="h" > <a name="92">92</a> </td> <td class="c0" > <a href="#96"> 0 </a> </td> <td class="c0" > <a href="blib-lib-LyricFinder-Genius-pm--branch.html#92-1"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> if (my ($goodbit) = $html =~ </td> </tr> <tr> <td class="h" > <a name="93">93</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> # m{\<div\s+class\=\"lyrics\"\>(.+)\<\!\-\-\/sse\-\-\>}msi) </td> </tr> <tr> <td class="h" > <a name="94">94</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> m{\<div\s+id\=\"lyrics\"[^\>]*\>(.+?)\<h1}msi) </td> </tr> <tr> <td class="h" > <a name="95">95</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> { </td> </tr> <tr> <td class="h" > <a name="96">96</a> </td> <td class="c0" > <a href="#97"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> my $hs = HTML::Strip->new(); </td> </tr> <tr> <td class="h" > <a name="97">97</a> </td> <td class="c0" > <a href="#98"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> $goodbit =~ s#\<\/?p\>#\r\n#gsi; </td> </tr> <tr> <td class="h" > <a name="98">98</a> </td> <td class="c0" > <a href="#99"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> $goodbit =~ s#\<br\/?\>#\r\n#gsi; </td> </tr> <tr> <td class="h" > <a name="99">99</a> </td> <td class="c0" > <a href="#102"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> my $text = $hs->parse($goodbit); </td> </tr> <tr> <td class="h" > <a name="100">100</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="101">101</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> #WHILE WE'RE AT IT, SEE IF WE HAVE A COVER IMAGE?!: </td> </tr> <tr> <td class="h" > <a name="102">102</a> </td> <td class="c0" > <a href="#103"> 0 </a> </td> <td class="c0" > <a href="blib-lib-LyricFinder-Genius-pm--branch.html#102-1"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> if ($html =~ m#\<noscript\>\s*\<img\s+src\=\"([^\"]+)#s) { </td> </tr> <tr> <td class="h" > <a name="103">103</a> </td> <td class="c0" > <a href="#104"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> my $imgurl = $1; </td> </tr> <tr> <td class="h" > <a name="104">104</a> </td> <td class="c0" > <a href="#105"> 0 </a> </td> <td class="c0" > <a href="blib-lib-LyricFinder-Genius-pm--branch.html#104-1"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> $imgurl = 'https:' . $imgurl if ($imgurl =~ m#^//#); </td> </tr> <tr> <td class="h" > <a name="105">105</a> </td> <td class="c0" > <a href="#108"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> $self->{'image_url'} = $imgurl; </td> </tr> <tr> <td class="h" > <a name="106">106</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> } </td> </tr> <tr> <td class="h" > <a name="107">107</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="108">108</a> </td> <td class="c0" > <a href="#110"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> return $self->_normalize_lyric_text($self->_html2text($text)); </td> </tr> <tr> <td class="h" > <a name="109">109</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> } else { </td> </tr> <tr> <td class="h" > <a name="110">110</a> </td> <td class="c0" > <a href="#111"> 0 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> carp($self->{'Error'} = "e:$Source - Failed to identify lyrics on result page."); </td> </tr> <tr> <td class="h" > <a name="111">111</a> </td> <td class="c0" > 0 </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> return ''; </td> </tr> <tr> <td class="h" > <a name="112">112</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> } </td> </tr> <tr> <td class="h" > <a name="113">113</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> } </td> </tr> <tr> <td class="h" > <a name="114">114</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="115">115</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> 1 </td> </tr> <tr> <td class="h" > <a name="116">116</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="117">117</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> __END__ </td> </tr> </table> </body> </html>