line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Yandex::Translate; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
404
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
23
|
|
4
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
21
|
|
5
|
1
|
|
|
1
|
|
512
|
use HTTP::Tiny; |
|
1
|
|
|
|
|
33797
|
|
|
1
|
|
|
|
|
39
|
|
6
|
1
|
|
|
1
|
|
7
|
use JSON; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
8
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
$Yandex::Translate::VERSION = '0.03'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new |
11
|
|
|
|
|
|
|
{ |
12
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
13
|
0
|
|
|
|
|
|
my $self = { |
14
|
|
|
|
|
|
|
_key_ => shift, |
15
|
|
|
|
|
|
|
_text_ => shift, |
16
|
|
|
|
|
|
|
_from_lang_ => shift, |
17
|
|
|
|
|
|
|
_to_lang_ => shift, |
18
|
|
|
|
|
|
|
_base_ => "https://translate.yandex.net/api/v1.5/tr.json", |
19
|
|
|
|
|
|
|
_post_ => undef |
20
|
|
|
|
|
|
|
}; |
21
|
|
|
|
|
|
|
|
22
|
0
|
|
|
|
|
|
bless $self, $class; |
23
|
0
|
|
|
|
|
|
return $self |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub set_key |
27
|
|
|
|
|
|
|
{ |
28
|
0
|
|
|
0
|
1
|
|
my ( $self, $key ) = @_; |
29
|
0
|
0
|
|
|
|
|
$self->{_key_} = $key if defined($key); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub get_langs_list |
33
|
|
|
|
|
|
|
{ |
34
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
35
|
0
|
|
|
|
|
|
my $query = '/getLangs?'; |
36
|
0
|
|
|
|
|
|
$self->{_post_} = 'ui=en&key='.$self->{_key_}; |
37
|
0
|
|
|
|
|
|
my $response = HTTP::Tiny->new->get($self->{_base_} . $query . $self->{_post_}); |
38
|
0
|
0
|
|
|
|
|
die "Invalid Api\n" if $response->{status} eq '403'; |
39
|
0
|
0
|
|
|
|
|
die "Blocked Api\n" if $response->{status} eq '402'; |
40
|
0
|
0
|
|
|
|
|
die "Failed!\n" unless $response->{success}; |
41
|
0
|
|
|
|
|
|
my $result = undef; |
42
|
0
|
0
|
|
|
|
|
if (length $response->{content}) { |
43
|
0
|
|
|
|
|
|
my $json_respond = JSON->new->utf8->decode($response->{content}); |
44
|
0
|
|
|
|
|
|
my $index = 0; |
45
|
0
|
|
|
|
|
|
while ($index < 10) { |
46
|
0
|
|
|
|
|
|
$result .= ' | '.$json_respond->{dirs}[$index]; |
47
|
0
|
|
|
|
|
|
$index++; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
0
|
0
|
|
|
|
|
return $result.'... for more info visit https://translate.yandex.com\n' if defined($result); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub set_text |
55
|
|
|
|
|
|
|
{ |
56
|
0
|
|
|
0
|
1
|
|
my ( $self, $text ) = @_; |
57
|
0
|
0
|
|
|
|
|
$self->{_text_} = $text if defined($text); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub set_from_lang |
61
|
|
|
|
|
|
|
{ |
62
|
0
|
|
|
0
|
1
|
|
my ($self, $from_lang) = @_; |
63
|
0
|
0
|
|
|
|
|
$self->{_from_lang_} = $from_lang if defined($from_lang); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub set_to_lang |
67
|
|
|
|
|
|
|
{ |
68
|
0
|
|
|
0
|
1
|
|
my ($self, $to_lang) = @_; |
69
|
0
|
0
|
|
|
|
|
$self->{_to_lang_} = $to_lang if defined($to_lang); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub detect_lang |
73
|
|
|
|
|
|
|
{ |
74
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
75
|
0
|
|
|
|
|
|
my $query = '/detect?'; |
76
|
0
|
|
|
|
|
|
$self->{_post_} = 'text='.$self->{_text_}.'&key='.$self->{_key_}; |
77
|
0
|
|
|
|
|
|
my $response = HTTP::Tiny->new->get($self->{_base_} . $query . $self->{_post_}); |
78
|
|
|
|
|
|
|
|
79
|
0
|
0
|
|
|
|
|
die "Failed!\n" unless $response->{success}; |
80
|
|
|
|
|
|
|
|
81
|
0
|
0
|
|
|
|
|
if (length $response->{content}) { |
82
|
0
|
|
|
|
|
|
my $json_respond = JSON->new->utf8->decode($response->{content}); |
83
|
0
|
|
|
|
|
|
return $json_respond->{lang}."\n"; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub translate |
88
|
|
|
|
|
|
|
{ |
89
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
90
|
0
|
|
|
|
|
|
my $query = '/translate?'; |
91
|
0
|
|
|
|
|
|
$self->{_text_} =~ s/ /+/ig; |
92
|
0
|
|
|
|
|
|
$self->{_post_} = 'lang='.$self->{_from_lang_}.'-'.$self->{_to_lang_}.'&key='.$self->{_key_}.'&text='.$self->{_text_}; |
93
|
0
|
|
|
|
|
|
my $response = HTTP::Tiny->new->get($self->{_base_} . $query . $self->{_post_}); |
94
|
0
|
0
|
|
|
|
|
die "Invalid Api\n" if $response->{status} eq '401'; |
95
|
0
|
0
|
|
|
|
|
die "Invalid Api\n" if $response->{status} eq '403'; |
96
|
0
|
0
|
|
|
|
|
die "Exceeded the daily limit on the amount of translated text\n" if $response->{status} eq '403'; |
97
|
0
|
0
|
|
|
|
|
die "Exceeded the maximum text size\n" if $response->{status} eq '413'; |
98
|
0
|
0
|
|
|
|
|
die "The text cannot be translated\n" if $response->{status} eq '422'; |
99
|
0
|
0
|
|
|
|
|
die "The specified translation direction is not supported\n" if $response->{status} eq '501'; |
100
|
0
|
0
|
|
|
|
|
die "Failed!\n" unless $response->{success}; |
101
|
|
|
|
|
|
|
|
102
|
0
|
0
|
|
|
|
|
if (length $response->{content}) { |
103
|
0
|
|
|
|
|
|
my $json_respond = JSON->new->utf8->decode($response->{content}); |
104
|
0
|
|
|
|
|
|
return $json_respond->{text}[0]."\n"; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
1; |
109
|
|
|
|
|
|
|
__END__ |