]>
iEval git - lyrics-fetcher-lyricstranslate.git/blob - lib/Lyrics/Fetcher/LyricsTranslate.pm
1 package Lyrics
::Fetcher
::LyricsTranslate
;
10 our $VERSION = '0.001';
11 our $BASE_URL = 'http://lyricstranslate.com';
12 # 0 means any language, 328 means English
13 our $URL_FORMAT = "$BASE_URL/en/translations/0/328/%s/%s/none";
15 my $ht = HTTP
::Tiny
->new(agent
=> "Lyrics-Fetcher-LyricsTranslate/$VERSION ");
18 my ($self, $artist, $song) = @_;
19 $Lyrics::Fetcher
::Error
= 'OK';
20 my $url = sprintf $URL_FORMAT, $artist, $song;
21 my $response = $ht->get($url);
22 unless ($response->{success
}) {
23 $Lyrics::Fetcher
::Error
= 'Search request failed: ' . $response->{reason
};
26 my $tree = HTML
::TreeBuilder
->new_from_content($response->{content
});
27 # First result would be the link to the artist, so we get the second one
28 my (undef, $result) = $tree->look_down(class => 'ltsearch-translatenameoriginal');
30 $Lyrics::Fetcher
::Error
= 'Lyrics not found';
32 $response = $ht->get($BASE_URL . $result->find('a')->attr('href'));
33 unless ($response->{success
}) {
34 $Lyrics::Fetcher
::Error
= 'Lyrics request failed: ' . $response->{reason
};
37 $tree = HTML
::TreeBuilder
->new_from_content($response->{content
});
38 my $node = $tree->look_down(class => qr/\btranslate-node-text\b/);
39 my $ltf = $node->look_down(class => qr/\bltf\b/);
40 my @pars = $ltf->look_down(class => 'par');
42 join '', map { $_->as_trimmed_text . "\n" } $_->content_list
53 Lyrics::Fetcher::LyricsTranslate - Get lyrics from lyricstranslate.com
57 # This module should be used directly
58 use Lyrics::Fetcher::LyricsTranslate;
59 print Lyrics::Fetcher::LyricsTranslate->fetch('Lyube', 'Kombat');
61 # Can also be used via Lyrics::Fetcher but produces ugly output
63 print Lyrics::Fetcher->fetch('Lyube', 'Kombat', 'LyricsTranslate');
67 This module tries to get translated lyrics from
68 L<http://lyricstranslate.com>. It does a search for a translation of
69 the given artist and song title from any language to English, and
70 returns the contents of the first result found.
72 This is a very basic implementation of the concept and it should be
73 improved in future versions (for example supporting multiple
74 destination languages).
76 It is recommended to use the module directly, as using it via
77 L<Lyrics::Fetcher> loses empty lines between parahraphs.
85 Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
87 =head1 COPYRIGHT AND LICENSE
89 Copyright (C) 2016 by Marius Gavrilescu
91 This library is free software; you can redistribute it and/or modify
92 it under the same terms as Perl itself, either Perl version 5.24.0 or,
93 at your option, any later version of Perl 5 you may have available.
This page took 0.05344 seconds and 4 git commands to generate.