PlaggerのFilterです。
MicrosoftTranslatorを使って、タイトルを翻訳しています。
Plagger::Plugin::Filter::Babelfishを参考に書きましたが、まだゼンゼン中途半端でコードが汚いです。。。
メモ的に載せておきます。
コスト的に考えて、本文の翻訳はやめておいたほうが良さそうかなと。
MicrosoftTranslatorを使って、タイトルを翻訳しています。
Plagger::Plugin::Filter::Babelfishを参考に書きましたが、まだゼンゼン中途半端でコードが汚いです。。。
メモ的に載せておきます。
コスト的に考えて、本文の翻訳はやめておいたほうが良さそうかなと。
package Plagger::Plugin::Filter::MicrosoftTranslator; use strict; use base qw( Plagger::Plugin ); use Plagger::UserAgent; use Digest::MD5 qw(md5_hex); use Encode qw(encode_utf8); use LWP::UserAgent; use XML::Atom::Feed; use XML::Atom::Entry; sub register { my($self, $context) = @_; $context->autoload_plugin({ module => 'Filter::GuessLanguage' }); $context->register_hook( $self, 'plugin.init' => \&connect_to_azure_api, 'update.entry.fixup' => \&update, ); } sub rule_hook { 'update.entry.fixup' } sub connect_to_azure_api { my($self, $context, $args) = @_; $context->log(debug => "hello"); } sub update { my($self, $context, $args) = @_; my $language = $self->conf->{source}; my $title = $args->{entry}->title->data; my $title_tr = $self->translate($title, $language); unless (defined $title_tr) { $context->log(error => "Translation failed"); return; } $args->{entry}->title($title_tr); sleep 1; =c my $body = $args->{entry}->body->data; my $body_tr = $self->translate($body, $language); unless (defined $body_tr) { $context->log(error => "Translation failed: "); return; } $args->{entry}->body($body_tr); =cut } sub translate { my ($self, $text, $language) = @_; my $destination = $self->conf->{destination} or do { Plagger->context->log(error => "set destination language"); return; }; # get content from azure api my $account_key = $self->conf->{account_key}; $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0; my $url = URI->new("https://api.datamarket.azure.com/Bing/MicrosoftTranslator/v1/Translate?Text=%27$text%27&To=%27$destination%27"); my $ua = LWP::UserAgent->new or die; my $req = HTTP::Request->new(GET => $url); $req->authorization_basic('', $account_key); my $res = $ua->request($req); die $res->status_line if !$res->is_success; my $content = $res->content; # get elem from Atom my $feed = XML::Atom::Feed->new(\$content); my @entries = $feed->entries; foreach my $entry(@entries){ return $entry->content->elem->findvalue('./m:properties/d:Text') } } 1; __END__ =head1 NAME Plagger::Plugin::Filter::MicrosoftTranslator - translate via MicrosoftTranslator =head1 SYNOPSIS - module: Filter::MicrosoftTranslator config: destination: ko account_key: xxxxxxxxxxxxxxxxxxxx =head1 DESCRIPTION This plugin translates each entry TITLE via MicrosoftTranslator. See "Microsoft Translator" http://msdn.microsoft.com/en-us/library/dd576287.aspx for details. =head1 CONFIG =over 4 =item destination Which language the feeds/entries should be translated to. See "Translator Language Codes" http://msdn.microsoft.com/en-us/library/hh456380.aspx for details. =back =head1 AUTHOR yako.takeshi@googlemail.com =head1 SEE ALSO L=cut