line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Lyrics::Fetcher::LyricsTranslate; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
70270
|
use 5.014000; |
|
2
|
|
|
|
|
5
|
|
4
|
2
|
|
|
2
|
|
6
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
35
|
|
5
|
2
|
|
|
2
|
|
6
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
52
|
|
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
2075
|
use HTML::TreeBuilder; |
|
2
|
|
|
|
|
44792
|
|
|
2
|
|
|
|
|
18
|
|
8
|
2
|
|
|
2
|
|
1472
|
use HTTP::Tiny; |
|
2
|
|
|
|
|
66102
|
|
|
2
|
|
|
|
|
589
|
|
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
|
1
|
|
|
1
|
0
|
141
|
my ($self, $artist, $song) = @_; |
19
|
1
|
|
|
|
|
2
|
$Lyrics::Fetcher::Error = 'OK'; |
20
|
1
|
|
|
|
|
5
|
my $url = sprintf $URL_FORMAT, $artist, $song; |
21
|
1
|
|
|
|
|
23
|
my $response = $ht->get($url); |
22
|
1
|
50
|
|
|
|
130606
|
unless ($response->{success}) { |
23
|
0
|
|
|
|
|
0
|
$Lyrics::Fetcher::Error = 'Search request failed: ' . $response->{reason}; |
24
|
|
|
|
|
|
|
return |
25
|
0
|
|
|
|
|
0
|
} |
26
|
1
|
|
|
|
|
13
|
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
|
1
|
|
|
|
|
93276
|
my (undef, $result) = $tree->look_down(class => 'ltsearch-translatenameoriginal'); |
29
|
1
|
50
|
|
|
|
2216
|
unless ($result) { |
30
|
0
|
|
|
|
|
0
|
$Lyrics::Fetcher::Error = 'Lyrics not found'; |
31
|
|
|
|
|
|
|
} |
32
|
1
|
|
|
|
|
4
|
$response = $ht->get($BASE_URL . $result->find('a')->attr('href')); |
33
|
1
|
50
|
|
|
|
51710
|
unless ($response->{success}) { |
34
|
0
|
|
|
|
|
0
|
$Lyrics::Fetcher::Error = 'Lyrics request failed: ' . $response->{reason}; |
35
|
|
|
|
|
|
|
return |
36
|
0
|
|
|
|
|
0
|
} |
37
|
1
|
|
|
|
|
8
|
$tree = HTML::TreeBuilder->new_from_content($response->{content}); |
38
|
1
|
|
|
|
|
118454
|
my $node = $tree->look_down(class => qr/\btranslate-node-text\b/); |
39
|
1
|
|
|
|
|
748
|
my $ltf = $node->look_down(class => qr/\bltf\b/); |
40
|
1
|
|
|
|
|
49
|
my @pars = $ltf->look_down(class => 'par'); |
41
|
|
|
|
|
|
|
join "\n", map { |
42
|
1
|
|
|
|
|
436
|
join '', map { $_->as_trimmed_text . "\n" } $_->content_list |
|
13
|
|
|
|
|
274
|
|
|
54
|
|
|
|
|
937
|
|
43
|
|
|
|
|
|
|
} @pars |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
1; |
47
|
|
|
|
|
|
|
__END__ |