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/ |
||||||
65 | 0 | ($self->{'Url'} = $artist_in) =~ s#\s*\/\s*# and #; #ONLY USE 1ST ARTIST, IF MORE THAN ONE! | |||||
66 | 0 | my $artist = $artist_in; | |||||
67 | 0 | (my $song = $song_in) =~ s#\s*\/\s*#\-#g; #FIX SONGS WITH "/" IN THEM! | |||||
68 | 0 | $self->{'Url'} .= " ${song}-lyrics"; | |||||
69 | 0 | $self->{'Url'} =~ s/\&/and/g; | |||||
70 | 0 | $self->{'Url'} =~ s/ +/\-/g; | |||||
71 | 0 | $self->{'Url'} =~ s/[^a-zA-Z0-9\-]+//g; | |||||
72 | 0 | $self->{'Url'} = $Site . '/' . $self->{'Url'}; | |||||
73 | |||||||
74 | 0 | my $lyrics = $self->_web_fetch($artist_in, $song_in); | |||||
75 | 0 | 0 | if ($lyrics) { | ||||
76 | 0 | $lyrics =~ s/[\d ]*Embed Cancel How to Format Lyrics\:.+$/\n/s; #REMOVE TRAILING GENIUS SPAM! :^/ | |||||
77 | 0 | 0 | 0 | if ($haveLyricsCache && $self->{'-cache'} && $self->{'-cache'} !~ /^\) { | |||
0 | |||||||
78 | 0 | $self->_debug("=== WILL CACHE LYRICS! ==="); | |||||
79 | # cache the fetched lyrics, if we can: | ||||||
80 | 0 | my $cache = new LyricFinder::Cache(%{$self}); | |||||
0 | |||||||
81 | 0 | 0 | $cache->save($artist_in, $song_in, $lyrics) if ($cache); | ||||
82 | } | ||||||
83 | } | ||||||
84 | 0 | return $lyrics; | |||||
85 | } | ||||||
86 | |||||||
87 | sub _parse { | ||||||
88 | 0 | 0 | my $self = shift; | ||||
89 | 0 | my $html = shift; | |||||
90 | |||||||
91 | 0 | $self->_debug("Genius::_parse()!"); | |||||
92 | 0 | 0 | if (my ($goodbit) = $html =~ | ||||
93 | # m{\ (.+)\<\!\-\-\/sse\-\-\>}msi) |
||||||
94 | m{\ ]*\>(.+?)\ | ||||||
95 | { | ||||||
96 | 0 | my $hs = HTML::Strip->new(); | |||||
97 | 0 | $goodbit =~ s#\<\/?p\>#\r\n#gsi; | |||||
98 | 0 | $goodbit =~ s#\ #\r\n#gsi; |
|||||
99 | 0 | my $text = $hs->parse($goodbit); | |||||
100 | |||||||
101 | #WHILE WE'RE AT IT, SEE IF WE HAVE A COVER IMAGE?!: | ||||||
102 | 0 | 0 | if ($html =~ m#\ | ||||
103 | 0 | my $imgurl = $1; | |||||
104 | 0 | 0 | $imgurl = 'https:' . $imgurl if ($imgurl =~ m#^//#); | ||||
105 | 0 | $self->{'image_url'} = $imgurl; | |||||
106 | } | ||||||
107 | |||||||
108 | 0 | return $self->_normalize_lyric_text($self->_html2text($text)); | |||||
109 | } else { | ||||||
110 | 0 | carp($self->{'Error'} = "e:$Source - Failed to identify lyrics on result page."); | |||||
111 | 0 | return ''; | |||||
112 | } | ||||||
113 | } | ||||||
114 | |||||||
115 | 1 | ||||||
116 | |||||||
117 | __END__ |