-
Notifications
You must be signed in to change notification settings - Fork 5
/
googletranslate.agi
executable file
·146 lines (125 loc) · 3.84 KB
/
googletranslate.agi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env perl
#
# AGI script that uses Google Translate API v2 for text translation.
#
# Copyright (C) 2012 - 2014, Lefteris Zafiris <zaf@fastmail.com>
#
# This program is free software, distributed under the terms of
# the GNU General Public License Version 2. See the COPYING file
# at the top of the source tree.
#
# -----
# Usage
# -----
# agi(googletranslate.agi,"text",[target language],[source language]):
# The text string will be send to google translate for translation to target language.
# The translated text will be stored as a channel variable named 'gtranslation'.
# The setting of source language is optional, if not set the tranlator engine
# will try to auto detect it.
# If both target language and source language are omitted the script performs language
# detection on the given text string and returns the two-character language code
# as the value of the channel variable 'glang'
#
# An API Key from google is required.
#
use warnings;
use strict;
use URI::Escape;
use JSON qw(decode_json);
use LWP::UserAgent;
binmode(STDOUT, ":encoding(UTF-8)");
$| = 1;
# ----------------------------- #
# User defined parameters: #
# ----------------------------- #
# App ID from google #
my $appid = "";
# Verbose debugging messages #
my $debug = 0;
# ----------------------------- #
my %AGI;
my $text;
my $in_lang;
my $out_lang;
my $url = "https://www.googleapis.com/language/translate/v2";
# Store AGI input #
($AGI{arg_1}, $AGI{arg_2}, $AGI{arg_3}) = @ARGV;
while (<STDIN>) {
chomp;
last if (!length);
$AGI{$1} = $2 if (/^agi_(\w+)\:\s+(.*)$/);
}
my $name = " -- $AGI{request}:";
# Reset channel variable. #
foreach ("gtranslation", "glang") {
print "SET VARIABLE \"$_\" \"-1\"\n";
checkresponse();
}
# Abort if there is not API key. #
die "$name No API key found. Aborting.\n" if (!$appid);
# Sanitising input #
for ($AGI{arg_1}) {
s/[\\|*~<>^\(\)\[\]\{\}[:cntrl:]]/ /g;
s/\s+/ /g;
s/^\s|\s$//g;
die "$name No text passed. Aborting\n" if (!length);
$text = $_;
}
# Setting source and target language #
if (length($AGI{arg_2})) {
if ($AGI{arg_2} =~ /[a-zA-Z\-]{2,}/) {
$out_lang = $AGI{arg_2};
} else {
die "$name Invalid output language setting.\n";
}
if (length($AGI{arg_3})) {
if ($AGI{arg_3} =~ /[a-zA-Z\-]{2,}/) {
$in_lang = $AGI{arg_3};
} else {
warn "$name Invalid input language setting. Using auto-detect.\n";
}
}
} else {
warn "$name Performing Language detection.\n";
}
warn "$name Text passed for translation: $text\ntarget lang: $out_lang\n" if ($debug && $out_lang);
warn "$name Text passed for language detection: $text\n" if ($debug && !$out_lang);
# Translate text using Google Translate API #
my $ua = LWP::UserAgent->new(ssl_opts => {verify_hostmane => 1});
$ua->agent("Asterisk Google Translate module");
$ua->env_proxy;
$ua->timeout(10);
$text = uri_escape($text);
if ($in_lang && $out_lang) {
$url .= "?key=$appid&q=$text&source=$in_lang&target=$out_lang";
} elsif (!$in_lang && $out_lang) {
$url .= "?key=$appid&q=$text&target=$out_lang";
} elsif (!$out_lang) {
$url .= "/detect?key=$appid&q=$text";
}
my $response = $ua->post("$url", "X-HTTP-Method-Override" => "GET");
die "$name Failed to fetch file.\n" if (!$response->is_success);
# export resulted text to channel variable #
my $jdata = decode_json $response->content;
if ($out_lang) {
print "SET VARIABLE \"gtranslation\" \"$$jdata{data}{translations}[0]{translatedText}\"\n";
checkresponse();
} else {
print "SET VARIABLE \"glang\" \"$$jdata{data}{detections}[0][0]{language}\"\n";
checkresponse();
}
exit;
sub checkresponse {
my $input = <STDIN>;
my @values;
chomp $input;
if ($input =~ /^200 result=(-?\d+)\s?(.*)$/) {
warn "$name Command returned: $input\n" if ($debug);
@values = ("$1", "$2");
} else {
$input .= <STDIN> if ($input =~ /^520-Invalid/);
warn "$name Unexpected result: $input\n";
@values = (-1, -1);
}
return @values;
}