Initial commit
[lyrics-fetcher-lyricstranslate.git] / lib / Lyrics / Fetcher / LyricsTranslate.pm
CommitLineData
0f95fc5f
MG
1package Lyrics::Fetcher::LyricsTranslate;
2
3use 5.014000;
4use strict;
5use warnings;
6
7use HTML::TreeBuilder;
8use HTTP::Tiny;
9
10our $VERSION = '0.001';
11our $BASE_URL = 'http://lyricstranslate.com';
12# 0 means any language, 328 means English
13our $URL_FORMAT = "$BASE_URL/en/translations/0/328/%s/%s/none";
14
15my $ht = HTTP::Tiny->new(agent => "Lyrics-Fetcher-LyricsTranslate/$VERSION ");
16
17sub fetch {
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};
24 return
25 }
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');
29 unless ($result) {
30 $Lyrics::Fetcher::Error = 'Lyrics not found';
31 }
32 $response = $ht->get($BASE_URL . $result->find('a')->attr('href'));
33 unless ($response->{success}) {
34 $Lyrics::Fetcher::Error = 'Lyrics request failed: ' . $response->{reason};
35 return
36 }
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');
41 join "\n", map {
42 join '', map { $_->as_trimmed_text . "\n" } $_->content_list
43 } @pars
44}
45
461;
47__END__
48
49=encoding utf-8
50
51=head1 NAME
52
53Lyrics::Fetcher::LyricsTranslate - Get lyrics from lyricstranslate.com
54
55=head1 SYNOPSIS
56
57 # This module should be used directly
58 use Lyrics::Fetcher::LyricsTranslate;
59 print Lyrics::Fetcher::LyricsTranslate->fetch('Lyube', 'Kombat');
60
61 # Can also be used via Lyrics::Fetcher but produces ugly output
62 use Lyrics::Fetcher;
63 print Lyrics::Fetcher->fetch('Lyube', 'Kombat', 'LyricsTranslate');
64
65=head1 DESCRIPTION
66
67This module tries to get translated lyrics from
68L<http://lyricstranslate.com>. It does a search for a translation of
69the given artist and song title from any language to English, and
70returns the contents of the first result found.
71
72This is a very basic implementation of the concept and it should be
73improved in future versions (for example supporting multiple
74destination languages).
75
76It is recommended to use the module directly, as using it via
77L<Lyrics::Fetcher> loses empty lines between parahraphs.
78
79=head1 SEE ALSO
80
81L<Lyrics::Fetcher>
82
83=head1 AUTHOR
84
85Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
86
87=head1 COPYRIGHT AND LICENSE
88
89Copyright (C) 2016 by Marius Gavrilescu
90
91This library is free software; you can redistribute it and/or modify
92it under the same terms as Perl itself, either Perl version 5.24.0 or,
93at your option, any later version of Perl 5 you may have available.
94
95
96=cut
This page took 0.016414 seconds and 4 git commands to generate.