#!/usr/bin/perl # # a perl5 program/filter using libwww-perl (LWP) to navigate to # Altavista's babelfish site to translate text. Input comes # from standard input, output goes to standard output # # Written on Feb. 1, 2001 by Frank Adelstein. # use LWP::UserAgent; use URI::Escape; use Getopt::Long; if (GetOptions(qw {-t=s -o=s --help!}) == 0) { # force a usage message $opt_help = 1 }; if (defined($opt_help)) { # print usage message and terminate print <) { $input .= $_; } close IN; # fix up the input a bit $input =~ s/%/%25/g; $input =~ s/\+/%2B/g; $input =~ s/ /\+/g; $input = uri_escape($input); # create the user agent object $ua = new LWP::UserAgent; # starting url $theurl = 'http://babelfish.altavista.com/tr'; # build the page for the FORM response my $req = new HTTP::Request 'POST', $theurl; $req->content_type('application/x-www-form-urlencoded'); $req->content("eng=utf8&doit=done&BabelFishFrontPage=yes&bblType=urltext&urltext=$input&url=http%3A%2F%2F&lp=$trans"); #print "request: " . $req->content . "\n"; # POST it $res = $ua->simple_request($req); #print "success: " . $res->is_success . "\n"; #print "status: " . $res->status_line . "\n"; #print "content: " . $res->content . "\n"; $buffer = ""; $inmatch = 0; # extract the translation out of the response # unfortunately, there are two DIFFERENT responses # depending on the length of the text (grrrr....) foreach $line (split('\n', $res->content)) { if ($line =~ m|| || $line =~ m|
//; $line =~ s/<\/tr>//; $line =~ s///; $line =~ s/