App-RoboBot
view release on metacpan or search on metacpan
lib/App/RoboBot/Plugin/API/Translate.pm view on Meta::CPAN
);
has 'ua' => (
is => 'rw',
isa => 'LWP::UserAgent',
default => sub {
my $ua = LWP::UserAgent->new;
$ua->agent('App::RoboBot');
$ua->timeout(3);
return $ua;
},
);
has 'valid_config' => (
is => 'rw',
isa => 'Bool',
traits => [qw( SetOnce )],
);
has 'cache' => (
is => 'rw',
isa => 'HashRef',
default => sub { {} },
);
sub init {
my ($self, $bot) = @_;
if (exists $self->bot->config->plugins->{'translate'}{'client'}
&& exists $self->bot->config->plugins->{'translate'}{'secret'}) {
if ($self->update_token) {
$self->valid_config(1);
} else {
$self->valid_config(0);
}
} else {
$self->valid_config(0);
}
}
sub update_token {
my ($self) = @_;
# last_authed only changes when it was successful, so if we have a current
# one, skip all the work ahead
return 1 if $self->last_authed > time() - 590;
my $client = $self->bot->config->plugins->{'translate'}{'client'};
my $secret = $self->bot->config->plugins->{'translate'}{'secret'};
my $response = $self->ua->post('https://datamarket.accesscontrol.windows.net/v2/OAuth2-13',
{ grant_type => 'client_credentials',
client_id => $client,
client_secret => $secret,
scope => 'http://api.microsofttranslator.com' });
return 0 unless $response->is_success;
my $json;
eval {
$json = decode_json($response->decoded_content);
};
return 0 if $@;
return 0 unless ref($json) eq 'HASH' && exists $json->{'access_token'} && $json->{'access_token'} =~ m{HMACSHA256};
$self->token($json->{'access_token'});
$self->last_authed(time() +0);
return 1;
}
sub translate_text {
my ($self, $message, $command, $rpl, $from, $to, @args) = @_;
unless ($self->valid_config) {
$message->response->raise('This bot instance does not have a valid Microsoft Translate API configuration. No translations will be possible.');
return;
}
unless (defined $from && defined $to && length($from) > 1 && length($to) > 1) {
$message->response->raise('Must provide a source and destination language for translation.');
return;
}
if (lc($from) eq lc($to)) {
$message->response->raise('Translate to the same language? What is the point in that?');
return;
}
my $text = join(' ', @args);
unless (defined $text && length($text) > 1) {
$message->response->raise('Must provide text to translate.');
return;
}
my $translation = $self->_do_translate($from, $to, $text);
return $translation if defined $translation;
$message->response->raise('Could not translate your phrase. Please check your source and destination languages.');
$message->response->raise('Valid language codes are listed at https://msdn.microsoft.com/en-us/library/hh456380.aspx');
return;
}
sub translate_party {
my ($self, $message, $command, $rpl, $from, $to, @args) = @_;
unless ($self->valid_config) {
$message->response->raise('This bot instance does not have a valid Microsoft Translate API configuration. No translations will be possible.');
return;
}
unless (defined $from && defined $to && length($from) > 1 && length($to) > 1) {
$message->response->raise('Must provide a source and destination language for translation.');
return;
}
if (lc($from) eq lc($to)) {
$message->response->raise('Translate to the same language? What is the point in that?');
return;
lib/App/RoboBot/Plugin/API/Translate.pm view on Meta::CPAN
my $equilibrium = $text;
my %seen = ( $from => { $text => 1 }, $to => {} );
# Cap attempts to find equilibrium at 6 round-trips between $from->$to->$from
for my $attempt (1..6) {
my $translation = $self->_do_translate($from, $to, $equilibrium);
last unless defined $translation;
last if exists $seen{$to}{$translation};
$seen{$to}{$translation} = 1;
$translation = $self->_do_translate($to, $from, $translation);
last unless defined $translation;
last if exists $seen{$from}{$translation};
$seen{$from}{$translation} = 1;
$equilibrium = $translation;
}
if (!defined $equilibrium || length($equilibrium) < 1 || $equilibrium eq $text) {
$message->response->raise('Could not get a party going with that phrase. Try again.');
return;
}
return $equilibrium;
}
sub _do_translate {
my ($self, $from, $to, $text) = @_;
$from = lc($from);
$to = lc($to);
my $key = lc($text);
return $self->cache->{$from}{$to}{$key}
if exists $self->cache->{$from}{$to}{$key};
return unless $self->update_token;
my $uri = URI->new;
$uri->scheme('http');
$uri->host('api.microsofttranslator.com');
$uri->path('/v2/Http.svc/Translate');
$uri->query_form({
from => $from,
to => $to,
text => $text,
});
my $req = HTTP::Request->new( GET => $uri->as_string );
$req->header('Authorization' => sprintf('Bearer %s', $self->token));
my $res = $self->ua->request($req);
return unless $res->is_success;
my $translation;
eval {
$translation = XML::LibXML->load_xml(
string => $res->decoded_content
)->getElementsByTagName("string") . "";
};
return if $@ || !defined $translation || length($translation) < 1;
# TODO: This should eventually have some sort of garbage collection to keep
# the translation cache from growing out of control on long running
# bot processes.
$self->cache->{$from}{$to}{$key} = $translation;
return $translation;
}
__PACKAGE__->meta->make_immutable;
1;
( run in 1.094 second using v1.01-cache-2.11-cpan-ceb78f64989 )