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