From: Marius Gavrilescu Date: Wed, 31 Aug 2016 23:35:04 +0000 (+0100) Subject: Initial commit X-Git-Tag: 0.001 X-Git-Url: http://git.ieval.ro/?a=commitdiff_plain;h=c78c37e714308f2fcacea583334740ee0b832224;p=lyrics-fetcher-lyricstranslate.git Initial commit --- 0f95fc5facfee4030c3932b5658694ba2b0500e3 diff --git a/Changes b/Changes new file mode 100644 index 0000000..15b9362 --- /dev/null +++ b/Changes @@ -0,0 +1,4 @@ +Revision history for Perl extension Lyrics::Fetcher::LyricsTranslate. + +0.001 2016-09-01T00:35+01:00 + - Initial release diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..6684667 --- /dev/null +++ b/MANIFEST @@ -0,0 +1,7 @@ +Changes +Makefile.PL +MANIFEST +README +t/00-compile.t +t/01-network.t +lib/Lyrics/Fetcher/LyricsTranslate.pm diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..593f831 --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,25 @@ +use 5.014000; +use ExtUtils::MakeMaker; + +WriteMakefile( + NAME => 'Lyrics::Fetcher::LyricsTranslate', + VERSION_FROM => 'lib/Lyrics/Fetcher/LyricsTranslate.pm', + ABSTRACT_FROM => 'lib/Lyrics/Fetcher/LyricsTranslate.pm', + AUTHOR => 'Marius Gavrilescu ', + MIN_PERL_VERSION => '5.14.0', + LICENSE => 'perl', + SIGN => 1, + PREREQ_PM => { + qw/HTML::Tree 0 + Lyrics::Fetcher 0/, + }, + TEST_REQUIRES => { + qw/Test::RequiresInternet 0/, + }, + META_ADD => { + dynamic_config => 0, + resources => { + repository => 'https://git.ieval.ro/?p=lyrics-fetcher-lyricstranslate.git', + }, + } +); diff --git a/README b/README new file mode 100644 index 0000000..3b6ad56 --- /dev/null +++ b/README @@ -0,0 +1,34 @@ +Lyrics-Fetcher-LyricsTranslate version 0.001 +============================================ + +This module tries to get translated lyrics from +http://lyricstranslate.com It does a search for a translation of the +given artist and song title from any language to English, and returns +the contents of the first result found. + +INSTALLATION + +To install this module type the following: + + perl Makefile.PL + make + make test + make install + +DEPENDENCIES + +This module requires these other modules and libraries: + +* Lyrics::Fetcher +* HTML::Tree +* Test::RequiresInternet + +COPYRIGHT AND LICENCE + +Copyright (C) 2016 by Marius Gavrilescu + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself, either Perl version 5.24.0 or, +at your option, any later version of Perl 5 you may have available. + + diff --git a/lib/Lyrics/Fetcher/LyricsTranslate.pm b/lib/Lyrics/Fetcher/LyricsTranslate.pm new file mode 100644 index 0000000..6708c0c --- /dev/null +++ b/lib/Lyrics/Fetcher/LyricsTranslate.pm @@ -0,0 +1,96 @@ +package Lyrics::Fetcher::LyricsTranslate; + +use 5.014000; +use strict; +use warnings; + +use HTML::TreeBuilder; +use HTTP::Tiny; + +our $VERSION = '0.001'; +our $BASE_URL = 'http://lyricstranslate.com'; +# 0 means any language, 328 means English +our $URL_FORMAT = "$BASE_URL/en/translations/0/328/%s/%s/none"; + +my $ht = HTTP::Tiny->new(agent => "Lyrics-Fetcher-LyricsTranslate/$VERSION "); + +sub fetch { + my ($self, $artist, $song) = @_; + $Lyrics::Fetcher::Error = 'OK'; + my $url = sprintf $URL_FORMAT, $artist, $song; + my $response = $ht->get($url); + unless ($response->{success}) { + $Lyrics::Fetcher::Error = 'Search request failed: ' . $response->{reason}; + return + } + my $tree = HTML::TreeBuilder->new_from_content($response->{content}); + # First result would be the link to the artist, so we get the second one + my (undef, $result) = $tree->look_down(class => 'ltsearch-translatenameoriginal'); + unless ($result) { + $Lyrics::Fetcher::Error = 'Lyrics not found'; + } + $response = $ht->get($BASE_URL . $result->find('a')->attr('href')); + unless ($response->{success}) { + $Lyrics::Fetcher::Error = 'Lyrics request failed: ' . $response->{reason}; + return + } + $tree = HTML::TreeBuilder->new_from_content($response->{content}); + my $node = $tree->look_down(class => qr/\btranslate-node-text\b/); + my $ltf = $node->look_down(class => qr/\bltf\b/); + my @pars = $ltf->look_down(class => 'par'); + join "\n", map { + join '', map { $_->as_trimmed_text . "\n" } $_->content_list + } @pars +} + +1; +__END__ + +=encoding utf-8 + +=head1 NAME + +Lyrics::Fetcher::LyricsTranslate - Get lyrics from lyricstranslate.com + +=head1 SYNOPSIS + + # This module should be used directly + use Lyrics::Fetcher::LyricsTranslate; + print Lyrics::Fetcher::LyricsTranslate->fetch('Lyube', 'Kombat'); + + # Can also be used via Lyrics::Fetcher but produces ugly output + use Lyrics::Fetcher; + print Lyrics::Fetcher->fetch('Lyube', 'Kombat', 'LyricsTranslate'); + +=head1 DESCRIPTION + +This module tries to get translated lyrics from +L. It does a search for a translation of +the given artist and song title from any language to English, and +returns the contents of the first result found. + +This is a very basic implementation of the concept and it should be +improved in future versions (for example supporting multiple +destination languages). + +It is recommended to use the module directly, as using it via +L loses empty lines between parahraphs. + +=head1 SEE ALSO + +L + +=head1 AUTHOR + +Marius Gavrilescu, Emarius@ieval.roE + +=head1 COPYRIGHT AND LICENSE + +Copyright (C) 2016 by Marius Gavrilescu + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself, either Perl version 5.24.0 or, +at your option, any later version of Perl 5 you may have available. + + +=cut diff --git a/t/00-compile.t b/t/00-compile.t new file mode 100644 index 0000000..f24a803 --- /dev/null +++ b/t/00-compile.t @@ -0,0 +1,6 @@ +#!/usr/bin/perl +use strict; +use warnings; + +use Test::More tests => 1; +BEGIN { use_ok('Lyrics::Fetcher::LyricsTranslate') }; diff --git a/t/01-network.t b/t/01-network.t new file mode 100644 index 0000000..c924884 --- /dev/null +++ b/t/01-network.t @@ -0,0 +1,15 @@ +#!/usr/bin/perl +use strict; +use warnings; + +use Test::More; +use Test::RequiresInternet ('lyricstranslate.com' => 80); + +plan tests => 1; + +use Lyrics::Fetcher::LyricsTranslate; + +like ( + Lyrics::Fetcher::LyricsTranslate->fetch('Lyube', 'Kombat'), + qr/soldiers/i, + 'lyrics to Lyube - Kombat contain the word "soldiers"');