/
LyricWiki.pm
146 lines (92 loc) · 3.32 KB
/
LyricWiki.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package Lyrics::Fetcher::LyricWiki;
# $Id$
use 5.005000;
use strict;
use warnings;
use LWP::UserAgent;
use HTML::TagParser;
use Carp;
our $VERSION = '0.06';
# the HTTP User-Agent we'll send:
our $AGENT = "Perl/Lyrics::Fetcher::LyricWiki $VERSION";
=head1 NAME
Lyrics::Fetcher::LyricWiki - Get song lyrics from www.LyricWiki.org
=head1 SYNOPSIS
use Lyrics::Fetcher;
print Lyrics::Fetcher->fetch("<artist>","<song>","LyricWiki");
# or, if you want to use this module directly without Lyrics::Fetcher's
# involvement:
use Lyrics::Fetcher::LyricWiki;
print Lyrics::Fetcher::LyricWiki->fetch('<artist>', '<song>');
=head1 DESCRIPTION
This module tries to get song lyrics from www.lyricwiki.org. It's designed to
be called by Lyrics::Fetcher, but can be used directly if you'd prefer.
=head1 FUNCTIONS
=over 4
=item I<fetch>($artist, $song)
Fetch lyrics for the requested song.
=cut
sub fetch {
my $self = shift;
my ( $artist, $song ) = @_;
# reset the error var, change it if an error occurs.
$Lyrics::Fetcher::Error = 'OK';
unless ($artist && $song) {
carp($Lyrics::Fetcher::Error =
'fetch() called without artist and song');
return;
}
my $ua = LWP::UserAgent->new();
$ua->agent($AGENT);
my $url = join ':', map { s/\s+/_/; $_ } ($artist, $song);
my $resp = $ua->get("http://lyrics.wikia.com/lyrics/$url");
if (!$resp->is_success) {
if ($resp->status_line =~ /404/) {
# Lyrics for this song not found
$Lyrics::Fetcher::Error = 'Lyrics not found';
return;
} else {
# Something else wrong, so return HTTP error description
$Lyrics::Fetcher::Error = "Failed to fetch - " . $resp->status_line;
return;
}
}
# OK, parse the HTML:
my $html = $resp->content;
$html =~ s{<br\s+/?>}{%newline%}gi;
my $parser = HTML::TagParser->new( $html );
if (my $lyricsdiv = $parser->getElementsByClassName('lyricbox')) {
$Lyrics::Fetcher::Error = 'OK';
my $lyrics = $lyricsdiv->innerText;
$lyrics =~ s/%newline%/\n/g;
return $lyrics;
} else {
$Lyrics::Fetcher::Error = 'No lyrics parsed from page';
return;
}
}
1;
__END__
=back
=head1 BUGS
Probably. Coded in about an hour whilst drinking cold lager :)
If you find any bugs, please let me know.
=head1 THANKS
Thanks to Sean Colombo for creating www.LyricWiki.org, and thanks to Wikia.com
for taking on the project and keeping it alive.
=head1 COPYRIGHT
This program is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
=head1 DISCLAIMER
Lyrics are copyright their original owners and/or record companies who purchased
the rights to them. Lyrics accessed programmatically via this module should be
used for educational purposes only, and should not be distributed/re-published
without authorisation. You are responsible for your usage of this module.
=head1 AUTHOR
David Precious E<lt>davidp@preshweb.co.ukE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2007-2009 by David Precious
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.7 or,
at your option, any later version of Perl 5 you may have available.
=cut