#!/usr/bin/perl # tiny is homophonic to тайный, meaning secret in russian use v5.030; # strictness implied use warnings; use Path::Tiny; use HTTP::Tiny; use JSON::MaybeXS; use utf8; my $file_in = path("/home/fritz/Desktop/1.enchanto.txt"); my $file_out = path('/home/fritz/Desktop/1.enc_trans.txt'); my $lang = 'es'; # slurp file my $guts = $file_in->slurp_utf8; my @spl = split( '\n', $guts ); # get credentials my ( $url, $key ) = get_тайный(); #say "$url, $key"; my $ua = HTTP::Tiny->new( 'verify_SSL' => '1' ); for my $para (@spl) { say $para; my $payload = "text=$para&target_lang=$lang"; my $payloadlen = length($payload); my $response = $ua->request( 'POST' => $url, { headers => { 'Authorization' => "DeepL-Auth-Key $key", 'Content-Length' => $payloadlen, 'Accept' => '*/*', 'Content-Type' => 'application/x-www-form-urlencoded', 'User-Agent' => 'curl/7.55.1' }, content => $payload, }, ); die "Failed!\n" unless $response->{success}; print $response->{content} if length $response->{content}; my $content = $response->{content}; say "content is $content"; my $data = decode_json($content); say "data is $data"; my $text1 = $data->{'translations'}->[0]->{'text'}; $file_out->append_utf8( $para, $text1 ); sub get_тайный { use Config::Tiny; my $ini_path = qw( 1.тайный.txt ); my $sub_hash = "deepl"; my $Config = Config::Tiny->new; $Config = Config::Tiny->read( $ini_path, 'utf8' ); # -> is optional between brackets my $url = $Config->{$sub_hash}{'url'}; my $key = $Config->{$sub_hash}{'key'}; #dial up the server $DB::single = 1; return ( $url, $key ); } } __END__ #### I have noticed that a sign went up in yard indicating that you were looking to leave Eagle Meadows. ...Me he dado cuenta de que se ha puesto un cartel en el jardín indicando que querías dejar Eagle Meadows. #### fritz@laptop:~/Documents$ trans "puesto un cartel" puesto un cartel put up a poster Translations of puesto un cartel [ Español -> English ] puesto un cartel put up a poster, put a sign fritz@laptop:~/Documents$ #### my $file_in = path("/home/fritz/Desktop/1.enchanto.txt"); my $file_out = path('/home/fritz/Desktop/1.enc_trans.txt'); my $lang = 'es'; #### #!/usr/bin/perl # tiny is homophonic to тайный, meaning secret in russian use v5.030; # strictness implied use warnings; use utf8; our $debug = 1; my $ref_events = init_event(); my $ref_from_http = http_tiny($ref_events, $debug); sub http_tiny { my ($ref_events, $debug) = (@_); use Path::Tiny; use HTTP::Tiny; use JSON::MaybeXS; $debug //= 0; my %h = %$ref_events; my $file_in = $h{infile}; my $file_out = $h{outfile}; my $lang = $h{lang}; # slurp file my $guts = $file_in->slurp_utf8; my @spl = split( '\n', $guts ); # get credentials my ( $url, $key ) = get_тайный(); #say "$url, $key"; my $ua = HTTP::Tiny->new( 'verify_SSL' => '1' ); for my $para (@spl) { say $para; my $payload = "text=$para&target_lang=$lang"; my $payloadlen = length($payload); my $response = $ua->request( 'POST' => $url, { headers => { 'Authorization' => "DeepL-Auth-Key $key", 'Content-Length' => $payloadlen, 'Accept' => '*/*', 'Content-Type' => 'application/x-www-form-urlencoded', 'User-Agent' => 'curl/7.55.1' }, content => $payload, }, ); die "Failed!\n" unless $response->{success}; if ( $debug > 0 ) { print "$0 : $payload\n$0 : sending above payload, of $payloadlen bytes..."; } #print $response->{content} if length $response->{content}; my $content = $response->{content}; #say "content is $content"; my $data = decode_json($content); #say "data is $data"; my $text1 = $data->{'translations'}->[0]->{'text'}; $file_out->append_utf8( $para, $text1 ); } sub get_тайный { use Config::Tiny; my $ini_path = qw( 1.тайный.txt ); my $sub_hash = "deepl"; my $Config = Config::Tiny->new; $Config = Config::Tiny->read( $ini_path, 'utf8' ); # -> is optional between brackets my $url = $Config->{$sub_hash}{'url'}; my $key = $Config->{$sub_hash}{'key'}; #dial up the server $DB::single = 1; return ( $url, $key ); } } sub init_event { my $hr = { infile => '/home/fritz/Desktop/1.enchanto.txt', outfile => '/home/fritz/Desktop/1.enc_trans.txt', lang => 'es', }; return $hr; } __END__ #### fritz@laptop:~/Documents$ ./5.trans.pl Name "DB::single" used only once: possible typo at ./5.trans.pl line 96. Can't locate object method "slurp_utf8" via package "/home/fritz/Desktop/1.enchanto.txt" (perhaps you forgot to load "/home/fritz/Desktop/1.enchanto.txt"?) at ./5.trans.pl line 35. fritz@laptop:~/Documents$