package Lyrics::Fetcher::Lyrics007;
use 5.008000;
use strict;
use WWW::Mechanize;
use Carp;
our $VERSION = '0.05';
our $AGENT = "Perl/Lyrics::Fetcher::Lyrics007 $VERSION";
sub fetch {
my $self = shift;
my ($band, $track) = @_;
# make sure that the artist and teack names are all Capitalised as the
# site pages are all uppercase firsts.
for ($band, $track) {
$_ = _title_case_all($_);
$_ = URI::Escape::uri_escape($_);
}
# preset the error var with ok, and change it if it went wrond.
$Lyrics::Fetcher::Error = 'OK';
# make sure we were given summit to search for...
unless ($band && $track) {
carp($Lyrics::Fetcher::Error =
"fetch() called without required parameters. artist & title )");
return;
}
# set up the url to call.
my $url = "http://www.lyrics007.com/$band Lyrics/$track Lyrics.html";
# right, its page time. so we'll sort out our useragent instance.
my $mech = WWW::Mechanize->new(agent => $AGENT);
$mech->get($url);
# if we weren't successful in our request then report the error.
unless ($mech->success()) {
carp($Lyrics::Fetcher::Error =
"Unable to retreive url from www.lyrics007.com");
return;
}
$mech->follow_link(text_regex => qr/print(?:able)?/i);
# if we followed the link successfuly then we'll clean up
# the lyrics and return them.
if ($mech->status() =~ m/200/) {
# grab the page contents.
my $lyrics = $mech->response()->content();
# swap out the html breaks for newlines
$lyrics =~ s/
/\n/gi;
# get shot of the lyrics007 tagline.
$lyrics =~ s/This is lyrics from www.lyrics007.com(?:\n)?//gi;
# and get shot of the title.
$lyrics =~ s/Tit?le\s?:.+\n//gi;
# if we have some form of lyrics at this point then
# return them, else error0z
if ($lyrics ne "") {
return $lyrics;
} else {
$Lyrics::Fetcher::Error = "Lyrics not found!";
return;
}
} else {
carp ($Lyrics::Fetcher::Error =
"URL Request Failed");# $response->status_line");
return;
}
}
# Uppercase all the words passed in a string.
sub _title_case_all {
return join ' ', map { ucfirst $_ } split /\s/, lc $_;
}
1;
__END__ # End of Lyrics::Fetcher::Lyrics007
=head1 NAME
Lyrics::Fetcher::Lyrics007
- Fetcher module for David Precious' (BIGPRESH) Lyrics::Fetcher
=head1 SYNOPSIS
use Lyrics::Fetcher::Lyrics007
print Lyrics::Fetcher::Lyrics007->fetch('', '