blib/lib/Lyrics/Fetcher/AZLyrics.pm

filter: Statementstmt 90.9% 60 / 66 Branchbran 62.5% 5 / 8 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 85.3% 76 / 89 CC 6 SCAR 9.7
CC
6
Cov
86%
fetch
5.2
_parse
2.1
BEGIN
1.0
CRAP 2.6
↑ Summary
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
BranchTrueFalseTotal
unless ($artist and $song) 0 3 3
Condition33%1/3
$artist and $song
!ll&&!rl&&r
003
MC/DC0%0/2
$artist and $song
$artist $song
Truth table33%1/3
A and B
A= $artistB= $song
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($res->is_success) { } 2 1 3
65 2 my $lyrics = _parse($res->decoded_content);
66 2 return $lyrics;
67 } else {
68 1 ▶ if ($res->status_line =~ /^404/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->status_line =~ /^404/) { } 1 0 1
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
BranchTrueFalseTotal
if (my($goodbit) = $html =~ m[our licensing agreement. Sorry about that. \-\-\>(.+)\<\/div\>]ism) { } 2 0 2
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__
↑ Summary