]> iEval git - lyrics-fetcher-lyricstranslate.git/blob - lib/Lyrics/Fetcher/LyricsTranslate.pm
Initial commit
[lyrics-fetcher-lyricstranslate.git] / lib / Lyrics / Fetcher / LyricsTranslate.pm
1 package Lyrics::Fetcher::LyricsTranslate;
2
3 use 5.014000;
4 use strict;
5 use warnings;
6
7 use HTML::TreeBuilder;
8 use HTTP::Tiny;
9
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";
14
15 my $ht = HTTP::Tiny->new(agent => "Lyrics-Fetcher-LyricsTranslate/$VERSION ");
16
17 sub 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
46 1;
47 __END__
48
49 =encoding utf-8
50
51 =head1 NAME
52
53 Lyrics::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
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.
71
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).
75
76 It is recommended to use the module directly, as using it via
77 L<Lyrics::Fetcher> loses empty lines between parahraphs.
78
79 =head1 SEE ALSO
80
81 L<Lyrics::Fetcher>
82
83 =head1 AUTHOR
84
85 Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
86
87 =head1 COPYRIGHT AND LICENSE
88
89 Copyright (C) 2016 by Marius Gavrilescu
90
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.
94
95
96 =cut
This page took 0.05344 seconds and 4 git commands to generate.