File Coverage

blib/lib/Lyrics/Fetcher/LyrDB.pm
Criterion Covered Total %
statement 12 43 27.9
branch 0 8 0.0
condition 0 3 0.0
subroutine 4 6 66.6
pod 2 2 100.0
total 18 62 29.0


line stmt bran cond sub pod time code
1             package Lyrics::Fetcher::LyrDB;
2              
3 1     1   22303 use warnings;
  1         2  
  1         33  
4 1     1   5 use strict;
  1         2  
  1         35  
5 1     1   5 use Carp;
  1         5  
  1         70  
6 1     1   741 use LWP::Simple;
  1         152900  
  1         8  
7              
8             =head1 NAME
9              
10             Lyrics::Fetcher::LyrDB - The great new Lyrics::Fetcher::LyrDB!
11              
12             =head1 VERSION
13              
14             Version 0.02
15              
16             =cut
17              
18             our $VERSION = '0.02';
19             our $AGENT = "Perl/Lyric::Fetcher::LyrDB $VERSION";
20              
21             =head1 NAME
22              
23             Lyrics::Fetcher::LyrDB - Get song lyrics from www.LyrDB.com
24              
25             =head1 SYNOPSIS
26              
27             use Lyrics::Fetcher;
28             print Lyrics::Fetcher->fetch("","","LyrDB");
29              
30             # or, if you want to use this module directly without Lyrics::Fetcher's
31             # involvement:
32             use Lyrics::Fetcher::LyrDB;
33             print Lyrics::Fetcher::LyricDB->fetch('', '');
34              
35              
36             =head1 DESCRIPTION
37              
38             This module uses LyrDB's web services to get song lyrics from
39             www.lyrdb.com. It's designed to be called by Lyrics::Fetcher, but can be
40             used directly if you'd prefer. This module makes use of the LWP::Simple
41             module, which you most likely already have.
42              
43             =head1 FUNCTIONS
44              
45             =over 4
46              
47             =item I($string)
48              
49             Helper function that trims starting and ending spaces from the string.
50              
51             =cut
52              
53             sub trim($)
54             {
55 0     0 1   my $string = shift;
56 0           $string =~ s/^\s+//;
57 0           $string =~s/^\s+$//;
58 0           return $string;
59             }
60              
61             =back
62              
63             =over 4
64              
65             =item I($artist, $song)
66              
67             Fetch lyrics for the requested song.
68              
69             =cut
70              
71             sub fetch
72             {
73            
74 0     0 1   my $self = shift;
75 0           my ( $artist, $song ) = @_;
76 0           my $result = undef;
77 0           my @number = ();
78 0           my $url = undef;
79              
80             # reset the error var, change it if an error occurs.
81 0           $Lyrics::Fetcher::Error = 'OK';
82            
83 0 0 0       unless ($artist && $song)
84             {
85 0           carp($Lyrics::Fetcher::Error =
86             'fetch() called without artist and song');
87              
88 0           return;
89             }
90            
91             # Get index of song.
92 0           $url = "http://webservices.lyrdb.com/lookup.php?" .
93             "q=$artist|$song&for=match&agent=iSing";
94              
95 0           $result = get $url;
96              
97 0 0         if(!defined $result)
98             {
99 0           carp($Lyrics::Fetcher::Error =
100             'fetch() could not query LyrDB.');
101 0           return;
102             }
103            
104             # Let's pick the first one and give LyrDB a query on it.
105 0           $result = trim $result;
106 0           @number = $result =~ /(\d+)\\/;
107              
108 0 0         if(!scalar(@number))
109             {
110 0           $Lyrics::Fetcher::Error =
111             "No id number found in query response.";
112 0           return;
113             }
114              
115 0           $url = "http://www.lyrdb.com/getlyr.php?q=$number[0]";
116              
117 0           $result = get $url;
118            
119 0 0         if($result =~ /error:\d\d\d/)
120             {
121 0           $result = $result =~ /\n+(.+)/;
122 0           $Lyrics::Fetcher::Error = $result;
123 0           return;
124             }
125              
126             # looks like it worked:
127 0           $Lyrics::Fetcher::Error = 'OK';
128              
129 0           return $result;
130              
131              
132             }
133              
134              
135              
136             1;
137             __END__