line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Lyrics::Fetcher::AZLyrics; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# $Id$ |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
29497
|
use 5.008000; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
39
|
|
6
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
7
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
28
|
|
8
|
1
|
|
|
1
|
|
1163
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
1134640
|
|
|
1
|
|
|
|
|
38
|
|
9
|
1
|
|
|
1
|
|
1083
|
use HTML::Strip; |
|
1
|
|
|
|
|
11090
|
|
|
1
|
|
|
|
|
71
|
|
10
|
1
|
|
|
1
|
|
11
|
use Carp; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
1267
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# the HTTP User-Agent we'll send: |
15
|
|
|
|
|
|
|
our $AGENT = "Perl/Lyrics::Fetcher::AZLyrics $VERSION"; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub fetch { |
20
|
|
|
|
|
|
|
|
21
|
3
|
|
|
3
|
1
|
5150
|
my $self = shift; |
22
|
3
|
|
|
|
|
12
|
my ( $artist, $song ) = @_; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# reset the error var, change it if an error occurs. |
25
|
3
|
|
|
|
|
9
|
$Lyrics::Fetcher::Error = 'OK'; |
26
|
|
|
|
|
|
|
|
27
|
3
|
50
|
33
|
|
|
28
|
unless ($artist && $song) { |
28
|
0
|
|
|
|
|
0
|
carp($Lyrics::Fetcher::Error = |
29
|
|
|
|
|
|
|
'fetch() called without artist and song'); |
30
|
0
|
|
|
|
|
0
|
return; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
3
|
|
|
|
|
18
|
$artist =~ s/[^a-z0-9]//gi; |
34
|
3
|
|
|
|
|
19
|
$song =~ s/[^a-z0-9]//gi; |
35
|
|
|
|
|
|
|
|
36
|
3
|
|
|
|
|
9
|
my $url = 'http://www.azlyrics.com/lyrics/'; |
37
|
3
|
|
|
|
|
54
|
$url .= join('/', lc $artist, lc $song) . '.html'; |
38
|
|
|
|
|
|
|
|
39
|
3
|
|
|
|
|
31
|
my $ua = new LWP::UserAgent; |
40
|
3
|
|
|
|
|
5577
|
$ua->timeout(6); |
41
|
3
|
|
|
|
|
56
|
$ua->agent($AGENT); |
42
|
3
|
|
|
|
|
199
|
my $res = $ua->get($url); |
43
|
3
|
50
|
|
|
|
282106
|
if ( $res->is_success ) { |
44
|
0
|
|
|
|
|
0
|
my $lyrics = _parse($res->content); |
45
|
0
|
|
|
|
|
0
|
return $lyrics; |
46
|
|
|
|
|
|
|
} else { |
47
|
|
|
|
|
|
|
|
48
|
3
|
50
|
|
|
|
46
|
if ($res->status_line =~ /^404/) { |
49
|
0
|
|
|
|
|
0
|
$Lyrics::Fetcher::Error = |
50
|
|
|
|
|
|
|
'Lyrics not found'; |
51
|
0
|
|
|
|
|
0
|
return; |
52
|
|
|
|
|
|
|
} else { |
53
|
3
|
|
|
|
|
50
|
carp($Lyrics::Fetcher::Error = |
54
|
|
|
|
|
|
|
"Failed to retrieve $url (".$res->status_line.')'); |
55
|
3
|
|
|
|
|
6799
|
return; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub _parse { |
64
|
0
|
|
|
0
|
|
|
my $html = shift; |
65
|
0
|
|
|
|
|
|
my $hs = HTML::Strip->new(); |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# Nasty - look for everything in between the two ringtones links: |
68
|
0
|
0
|
|
|
|
|
if (my ($goodbit) = $html =~ |
69
|
|
|
|
|
|
|
m{<\!-- END OF RINGTONE 1 -->(.+)<\!-- RINGTONE 2 -->}msi) |
70
|
|
|
|
|
|
|
{ |
71
|
0
|
|
|
|
|
|
my $text = $hs->parse($goodbit); |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# Remove mentions of ringtones: |
74
|
0
|
|
|
|
|
|
$text =~ s/^ .+ ringtone .+ $//xmgi; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# Scoop out any credits for these lyrics: |
77
|
0
|
|
|
|
|
|
my @credits; |
78
|
0
|
|
|
|
|
|
@Lyrics::Fetcher::azcredits = (); |
79
|
0
|
|
|
|
|
|
while ($text =~ s{\[ Thanks \s to \s (.+) \]}{}xgi) { |
80
|
0
|
|
|
|
|
|
push @Lyrics::Fetcher::azcredits, $1; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
0
|
|
|
|
|
|
$text =~ s/\s*\[.+at \s www.AZLyrics.com\s+\]\s*//xmgi; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# finally, clear up excess blank lines: |
86
|
0
|
|
|
|
|
|
$text =~ s/(\r?\n){2,}/\n\n/gs; |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
return $text; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
} else { |
91
|
0
|
|
|
|
|
|
carp "Failed to identify lyrics on result page"; |
92
|
0
|
|
|
|
|
|
return; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
} # end of sub parse |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
1; |
100
|
|
|
|
|
|
|
__END__ |