#!perl
use strict;
use warnings;
use HTTP::Tiny;
my $ua = HTTP::Tiny->new( 'verify_SSL' => '1' );
my $res = $ua->request(
'POST' => 'https://api-free.deepl.com/v2/translate',
{
headers => {
'Authorization' =>
'DeepL-Auth-Key redacted',
'Content-Length' => '37',
'Accept' => '*/*',
'Content-Type' => 'application/x-www-form-urlencoded',
'User-Agent' => 'curl/7.55.1'
},
content => "text=Hello\x252C\x2520world!&target_lang=DE"
},
);
__END__
Created from curl command line
curl -X POST 'https://api-free.deepl.com/v2/translate' -H 'Authorization: DeepL-Auth-Key redacted' -d 'text=Hello%2C%20world!' -d 'target_lang=DE'
####
fritz@laptop:~/Documents$ ./3.trans.pl
Hello neighbor on Watercress lane,
{"translations":[{"detected_source_language":"EN","text":"Hola vecino de Watercress lane,"}]}content is {"translations":[{"detected_source_language":"EN","text":"Hola vecino de Watercress lane,"}]}
data is HASH(0x55fbf7cf4140)
...
Anyways, I start getting letters saying that I have not complied with this declaration, which had the bizarre predicate that we had to come to their residence to prove that we had complied. One thing I can promise you: I will never cross their threshold, because I don't want to know them at all based on what they stuffed into my mailbox.
{"translations":[{"detected_source_language":"EN","text":"De todos modos, empiezo a recibir cartas diciendo que no he cumplido con esta declaración, que tenía el extraño predicado de que teníamos que ir a su residencia para demostrar que habíamos cumplido. Una cosa puedo prometer: Nunca cruzaré su umbral, porque no quiero conocerlos en absoluto basándome en lo que me metieron en el buzón."}]}content is {"translations":[{"detected_source_language":"EN","text":"De todos modos, empiezo a recibir cartas diciendo que no he cumplido con esta declaración, que tenía el extraño predicado de que teníamos que ir a su residencia para demostrar que habíamos cumplido. Una cosa puedo prometer: Nunca cruzaré su umbral, porque no quiero conocerlos en absoluto basándome en lo que me metieron en el buzón."}]}
data is HASH(0x55fbf8768858)
fritz@laptop:~/Documents$ ^C
####
#!/usr/bin/perl
use v5.030; # strictness implied
use warnings;
use Path::Tiny;
use HTTP::Tiny;
use JSON::MaybeXS;
my $file_in = path("/home/fritz/Desktop/1.enchanto.txt");
my $file_out = path('/home/fritz/Desktop/1.enc_trans.txt');
my $lang = 'es';
my $guts = $file_in->slurp_utf8;
my @spl = split( '\n', $guts );
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' => 'https://api-free.deepl.com/v2/translate',
{
headers => {
'Authorization' =>
'DeepL-Auth-Key redacted',
'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";
$file_out->spew_utf8( $para, $data );
}
__END__
####
use LWP::UserAgent;
use HTTP::Request;
use Data::Roundtrip;
...
my $req = HTTP::Request->new(
...
$response = $ua->request($req);
die "Error fetching: " . $response->status_line
unless $response->is_success;
my $content = $response->decoded_content;
my $data = Data::Roundtrip::json2perl($content);
die "failed to parse received data:\n$content\n"
unless exists $data->{'elevation'};
return $data->{'elevation'};
####
my $content = $response->decoded_content;
my $data = Data::Roundtrip::json2perl($content);