Plagger/Plugin/Filter/Babelfish.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 121 CC 7 SCAR 23.7
CC
7
Cov
0%
update
30.0
connect_to_babelfish
6.0
translate
6.0
CRAP 10.7
1 package Plagger::Plugin::Filter::Babelfish;
2 use strict;
3 use base qw( Plagger::Plugin );
4  
5 use Plagger::UserAgent;
6 use WWW::Babelfish;
7 use Digest::MD5 qw(md5_hex);
8 use Encode qw(encode_utf8);
9  
10 use Locale::Language;
11  
12 sub register {
13 my($self, $context) = @_;
14  
15 $context->autoload_plugin({ module => 'Filter::GuessLanguage' });
16 $context->register_hook(
17 $self,
18 'plugin.init' => \&connect_to_babelfish,
19 'update.entry.fixup' => \&update,
20 );
21 }
22  
23 sub rule_hook { 'update.entry.fixup' }
24  
25 sub connect_to_babelfish {
26 my($self, $context, $args) = @_;
27  
28 my $service = $self->conf->{service} || 'Babelfish';
29  
30 $context->log(debug => "hello, $service");
31  
32 my $ua = Plagger::UserAgent->new;
33 $self->{babelfish}->{translator} = new WWW::Babelfish(
34 service => $service,
35 agent => $ua->agent
36 );
37 unless (defined $self->{babelfish}->{translator}) {
38 $context->log(error => "$service is not available");
39 return;
40 }
41 }
42  
43 sub update {
44 my($self, $context, $args) = @_;
45  
46 my $translator = $self->{babelfish}->{translator} or return;
47 my $language = $self->conf->{source} || code2language(
48 $args->{entry}->{language} || $args->{feed}->language
49 ) or do {
50 $context->log(warn => "can't identify language");
51 return;
52 };
53  
54 my $title = $args->{entry}->title->data;
55 my $title_tr = $self->translate($translator, $title, $language);
56 unless (defined $title_tr) {
57 $context->log(error => "Translation failed: " . $translator->error);
58 return;
59 }
60 $title_tr = $title . "\n\n" . $title_tr if $self->conf->{prepend_original};
61  
62 $args->{entry}->title($title_tr);
63  
64 sleep 1;
65  
66 my $body = $args->{entry}->body->data;
67 my $body_tr = $self->translate($translator, $body, $language);
68 unless (defined $body_tr) {
69 $context->log(error => "Translation failed: " . $translator->error);
70 return;
71 }
72 if ($self->conf->{prepend_original}) {
73 $body_tr = $body . "\n\n" . $body_tr;
74 $context->log(debug => "prepended original body");
75 }
76  
77 $args->{entry}->body($body_tr);
78 }
79  
80 sub translate {
81 my ($self, $translator, $text, $language) = @_;
82  
83 my $destination = $self->conf->{destination} or do {
84 Plagger->context->log(error => "set destination language");
85 return;
86 };
87  
88 unless ($translator->languagepairs->{$language}->{$destination}) {
89 Plagger->context->log(
90 error => "unsupported combination: $language to $destination"
91 );
92 return;
93 }
94  
95 my $key = md5_hex(encode_utf8($text));
96  
97 return $self->cache->get_callback(
98 "babelfish-$key-$destination",
99 sub {
100 $translator->translate(
101 source => $language,
102 destination => $destination,
103 text => $text,
104 delimiter => "\n\n",
105 );
106 },
107 '3 days'
108 );
109 }
110  
111 1;
112  
113 __END__