artagnon (owner)

Revisions

gist: 219819 Download_button fork
public
Public Clone URL: git://gist.github.com/219819.git
Embed All Files: show embed
LeoLyrics.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
147
148
149
150
151
152
153
#!/usr/bin/perl
package Lyrics::Fetcher::LeosLyrics;
our $VERSION = 0.02;
 
# Copyright (c) 2007 Edward Allen III. Some rights reserved.
#
## This program is free software; you can redistribute it and/or
## modify it under the terms of the Artistic License, distributed
## with Perl.
#
# Lyrics::Fetcher is maintained by BIGPRESH (David Precious <davidp@preshweb.co.uk>)
 
=pod
 
=head1 NAME
 
Lyrics::Fetcher::LeosLyrics - Get song lyrics from leoslyrics.com
 
=head1 SYNOPSIS
 
use Lyrics::Fetcher;
print Lyrics::Fetcher->fetch("<artist>","<song>","LeosLyrics");
 
# or, if you want to use this module directly without Lyrics::Fetcher's
# involvement:
use Lyrics::Fetcher::LeosLyrics;
print Lyrics::Fetcher::LeosLyrics->fetch('<artist>', '<song>');
 
=head1 DESCRIPTION
 
This module tries to get song lyrics from leoslyrics.com using the unpublished XML interface. It's designed
to be called by Lyrics::Fetcher, but can be used directly if you'd prefer.
 
=cut
 
use 5.008000;
use strict;
use Carp;
use URI::Escape qw(uri_escape uri_escape_utf8);
use Encode;
use XML::Simple;
use LWP::Simple;
 
print fetch($ARGV[0], $ARGV[1]);
print "\n";
 
sub _url_escape {
    my $in = shift;
    $in =~ s/ /+/g;
    return uri_escape_utf8( encode( "utf8", $in ), "^A-Za-z0-9\-_.!\/?=\+" );
}
 
=head1 FUNCTIONS
 
=over 4
 
=item I<fetch>($artist, $song)
 
Fetch lyrics for the requested song.
 
=cut
 
sub fetch {
    my $artist = uri_escape(shift);
    my $song = uri_escape(shift);
    
    $Lyrics::Fetcher::Error = 'OK';
    
    unless ($artist && $song) {
        carp($Lyrics::Fetcher::Error =
            'fetch() called without artist and song');
        return;
    }
 
    my $res = get( "http://api.leoslyrics.com/api_search.php?auth=LeosLyrics5&artist=${artist}&songtitle=${song}");
unless ($res) {
        $Lyrics::Fetcher::Error = 'request failed';
        return;
}
 
my $xml= XML::Simple::XMLin($res);
 
if ((ref $xml) && (exists $xml->{searchResults}) && (ref $xml->{searchResults}) && (exists $xml->{searchResults}->{result}) && (ref $xml->{searchResults}->{result}) && (exists $xml->{searchResults}->{result}->{exactMatch})) {
if ($xml->{searchResults}->{result}->{exactMatch} eq "true") {
my $hid = $xml->{searchResults}->{result}->{hid};
my $lres = get("http://api.leoslyrics.com/api_lyrics.php?auth=LeosLyrics5&hid=$hid");
unless ($lres) {
$Lyrics::Fetcher::Error = 'Error retrieving document';
return;
}
my $lxml= XML::Simple::XMLin($lres);
if ((ref $lxml) && (exists $lxml->{lyric}) && (ref $lxml->{lyric}) && (exists $lxml->{lyric}->{text})) {
my $ret = $lxml->{lyric}->{text};
if ($ret) {
$Lyrics::Fetcher::Error = 'OK';
return $ret;
}
else {
$Lyrics::Fetcher::Error = 'Blank Lyrics Returned';
return;
}
}
else {
$Lyrics::Fetcher::Error = 'Error processing XML';
return;
}
}
else {
$Lyrics::Fetcher::Error = 'HID not listed in result.';
return;
}
}
    else {
$Lyrics::Fetcher::Error = 'Lyrics not found';
        return undef;
    }
 
}
 
 
=pod
 
=back
 
=head1 BUGS
 
Most likely there is something. LeosLyrics is poor in its unicode support.
 
=head1 SEE ALSO
 
L<Lyrics::Fetcher>
 
=head1 AUTHOR
 
Edward Allen III <ealleniii _at_ cpan _dot_ org>
 
=head1 COPYRIGHT
 
Copyright (c) 2007 Edward Allen III. Some rights reserved.
 
This program is free software; you can redistribute it and/or
modify it under the terms of the Artistic License, distributed
with Perl.
 
Legal disclaimer: I have no connection with the owners of leoslyrics.com.
Lyrics fetched by this script may be copyrighted by the authors, it's up to
you to determine whether this is the case, and if so, whether you are entitled
to request/use those lyrics. You will almost certainly not be allowed to use
the lyrics obtained for any commercial purposes.
 
=cut
 
1;