| 1 | package Lyrics::Fetcher::Genius; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.008000; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use HTML::Strip; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # the HTTP User-Agent we'll send: | ||||||||||||||||||||||||||||
| 14 | 2 | our $AGENT | |||||||||||||||||||||||||||
| 15 | = "Mozilla/5.0 (X11; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0"; | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 3 | ▶ | sub fetch { | ||||||||||||||||||||||||||
|
Subroutine
fetch: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 20 | 3 | my ($artist, $song) = @_; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # reset the error var, change it if an error occurs. | ||||||||||||||||||||||||||||
| 23 | 3 | $Lyrics::Fetcher::Error = 'OK'; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 3 | ▶ | unless ($artist && $song) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$artist and $song
MC/DC0%0/2
$artist and $song
$artist
$song
Truth table33%1/3
A and B
A= $artistB= $song
| |||||||||||||||||||||||||||||
| 26 | 0 | carp($Lyrics::Fetcher::Error | |||||||||||||||||||||||||||
| 27 | = 'fetch() called without artist and song'); | ||||||||||||||||||||||||||||
| 28 | 0 | return; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # Their URLs look like e.g.: | ||||||||||||||||||||||||||||
| 32 | # https://genius.com/Dire-straits-heavy-fuel-lyrics | ||||||||||||||||||||||||||||
| 33 | 3 | $artist = ucfirst $artist; | |||||||||||||||||||||||||||
| 34 | 3 | $song = lc $song; | |||||||||||||||||||||||||||
| 35 | 3 | (my $url = $artist) =~ s#\/.*$##; | |||||||||||||||||||||||||||
| 36 | 3 | $url .= "-${song}-lyrics"; | |||||||||||||||||||||||||||
| 37 | 3 | $url =~ s/ +/\-/g; | |||||||||||||||||||||||||||
| 38 | 3 | $url =~ s/\%20/\-/g; | |||||||||||||||||||||||||||
| 39 | 3 | $url =~ s/\%\d+//g; | |||||||||||||||||||||||||||
| 40 | 3 | $url =~ s/\.//g; | |||||||||||||||||||||||||||
| 41 | 3 | $url =~ s/[^a-zA-Z0-9\.\-]+//g; | |||||||||||||||||||||||||||
| 42 | 3 | $url = 'https://genius.com/' . $url; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 3 | my $ua = LWP::UserAgent->new( | |||||||||||||||||||||||||||
| 45 | timeout => 10, | ||||||||||||||||||||||||||||
| 46 | agent => $AGENT, | ||||||||||||||||||||||||||||
| 47 | protocols_allowed => ['https'], | ||||||||||||||||||||||||||||
| 48 | cookie_jar => {}, | ||||||||||||||||||||||||||||
| 49 | ssl_opts => { verify_hostname => 0, }, | ||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||
| 51 | 3 | push @{ $ua->requests_redirectable }, 'GET'; | |||||||||||||||||||||||||||
| 52 | 3 | (my $referer = $url) =~ s{^(\w+)\:\/\/}{}; | |||||||||||||||||||||||||||
| 53 | 3 | my $protocol = $1; | |||||||||||||||||||||||||||
| 54 | 3 | $referer =~ s{\/.+$}{\/}; | |||||||||||||||||||||||||||
| 55 | 3 | my $host = $referer; | |||||||||||||||||||||||||||
| 56 | 3 | $host =~ s{\/$}{}; | |||||||||||||||||||||||||||
| 57 | 3 | $referer = $protocol . '://' . $referer; | |||||||||||||||||||||||||||
| 58 | 3 | my $req = new HTTP::Request 'GET' => $url; | |||||||||||||||||||||||||||
| 59 | 3 | $req->header( | |||||||||||||||||||||||||||
| 60 | 'Accept' => | ||||||||||||||||||||||||||||
| 61 | 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', | ||||||||||||||||||||||||||||
| 62 | 'Accept-Language' => 'en-US,en;q=0.5', | ||||||||||||||||||||||||||||
| 63 | 'Accept-Encoding' => 'gzip, deflate', | ||||||||||||||||||||||||||||
| 64 | 'Connection' => 'keep-alive', | ||||||||||||||||||||||||||||
| 65 | 'Upgrade-insecure-requests' => 1, | ||||||||||||||||||||||||||||
| 66 | 'Host' => $host, | ||||||||||||||||||||||||||||
| 67 | ); | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 3 | my $res = $ua->request($req); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 3 | ▶ | if ($res->is_success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 2 | my $lyrics = _parse($res->decoded_content); | |||||||||||||||||||||||||||
| 73 | 2 | return $lyrics; | |||||||||||||||||||||||||||
| 74 | } else { | ||||||||||||||||||||||||||||
| 75 | 1 | ▶ | if ($res->status_line =~ /^404/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 1 | $Lyrics::Fetcher::Error = 'Lyrics not found'; | |||||||||||||||||||||||||||
| 77 | 1 | return; | |||||||||||||||||||||||||||
| 78 | } else { | ||||||||||||||||||||||||||||
| 79 | 0 | carp($Lyrics::Fetcher::Error | |||||||||||||||||||||||||||
| 80 | = "Failed to retrieve $url (" . $res->status_line . ')'); | ||||||||||||||||||||||||||||
| 81 | 0 | return; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 2 | ▶ | sub _parse { | ||||||||||||||||||||||||||
|
Subroutine
_parse: called CC=2 SCAR=9.7
| |||||||||||||||||||||||||||||
| 88 | 2 | my $html = shift; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 2 | ▶ | if (my ($goodbit) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | = $html =~ m{\<div\s+class\=\"lyrics\"\>(.+)\<\!\-\-\/sse\-\-\>}msi) | ||||||||||||||||||||||||||||
| 92 | { | ||||||||||||||||||||||||||||
| 93 | 0 | my $hs = HTML::Strip->new(); | |||||||||||||||||||||||||||
| 94 | 0 | my $text = $hs->parse($goodbit); | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | $text =~ s/\s+$//xmgi; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # finally, clear up excess blank lines: | ||||||||||||||||||||||||||||
| 99 | 0 | $text =~ s/(\r?\n){2,}/\n\n/gs; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | return $text; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | } else { | ||||||||||||||||||||||||||||
| 104 | 2 | carp "Failed to identify lyrics on result page"; | |||||||||||||||||||||||||||
| 105 | 2 | return; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | } # end of sub parse | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 2 | 1; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | __END__ | ||||||||||||||||||||||||||||