From 7685ad5fd2a26c1abcc7015485f4b8b98741505d Mon Sep 17 00:00:00 2001 From: Joelle Maslak Date: Sat, 6 Jul 2019 18:02:58 -0600 Subject: Solutions for 15.3 in P5 & P6 --- challenge-015/joelle-maslak/perl5/ch-3.pl | 68 +++++++++++++++++++++++++++++++ challenge-015/joelle-maslak/perl6/ch-3.p6 | 58 ++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100755 challenge-015/joelle-maslak/perl5/ch-3.pl create mode 100755 challenge-015/joelle-maslak/perl6/ch-3.p6 diff --git a/challenge-015/joelle-maslak/perl5/ch-3.pl b/challenge-015/joelle-maslak/perl5/ch-3.pl new file mode 100755 index 0000000000..87ccb7a483 --- /dev/null +++ b/challenge-015/joelle-maslak/perl5/ch-3.pl @@ -0,0 +1,68 @@ +#!/usr/bin/env perl +use v5.26; +use strict; +use warnings; + +# Turn on method signatures +use feature 'signatures'; +no warnings 'experimental::signatures'; + +use autodie; +use Getopt::Long; +use Mojo::UserAgent; +use Path::Tiny; +use Perl6::Slurp; + +MAIN: { + my $configfile = path( $ENV{HOME} // "", ".languageapi" )->stringify(); + my $url = 'https://ws.detectlanguage.com/0.2/detect'; + GetOptions( + "url=s" => \$url, + "config-file=s" => \$configfile, + ); + + if ( @ARGV != 0 ) { die("Unknown parameter"); } + + my $apikey = get_api_key($configfile); + + my $input = ''; + while () { $input .= $_ } + + my $info = get_language($input, $apikey, $url); + my $result = $info->{data}{detections}[0]; + say "Language: ", $result->{language}; + say "Is reliable: ", $result->{isReliable} ? "Yes" : "No"; + say "Confidence: ", $result->{confidence}; +} + +sub get_api_key($configfile) { + my (@lines) = grep { length($_) } slurp($configfile); + die "Config-file ($configfile) must consist of one line" if @lines != 1; + chomp $lines[0]; + return $lines[0]; +} + +# Usage: +# text = Text to look up +# apikey = Your API key +# url = URL +# +sub get_language($text, $apikey, $url) { + my $ua = Mojo::UserAgent->new(); + $ua->max_redirects(16); + $ua->once( + start => sub ( $ua, $tx ) { + $tx->req->headers->header( 'Authorization' => "Bearer " . $apikey ); + } + ); + + my $tx = $ua->post($url, json => { q => $text } ); + if ( ( $tx->result->code >= 200 ) && ( $tx->result->code <= 299 ) ) { + my $json = $tx->result->json; + return $json; + } + + my $body = $tx->result->json; + die "Error from API endpoint: " . $body->{error}{message}; +} + diff --git a/challenge-015/joelle-maslak/perl6/ch-3.p6 b/challenge-015/joelle-maslak/perl6/ch-3.p6 new file mode 100755 index 0000000000..fe3d0f7b45 --- /dev/null +++ b/challenge-015/joelle-maslak/perl6/ch-3.p6 @@ -0,0 +1,58 @@ +#!/usr/bin/env perl6 +use v6; + +use Cro::HTTP::Client; + +sub MAIN( + Str:D :$config-file? = $*HOME.add(".languageapi").Str, + Str:D :$url = 'https://ws.detectlanguage.com/0.2/detect', +) { + my $info = get-language(:text($*IN.slurp), :$url, :$config-file); + my $result = $info[0]; + say "Language: { $result }"; + say "Is reliable: { $result }"; + say "Confidence: { $result.fmt("%.2f") }"; +} + +sub get-api-key(Str:D $config-file -->Str:D) { + my @lines = $config-file.IO.lines().grep( *.chars > 0 ); + die "Config-file ($config-file) must consist of one line" if @lines.elems ≠ 1; + + return @lines.first; +} + +# Usage: +# :$text = Text to look up +# :$url = base URL +# :$config-file = Name of config file to use +# +sub get-language ( + Str:D :$text, + Str:D :$url, + Str:D :$config-file, +) { + my $api-key = get-api-key($config-file); + + my $client = Cro::HTTP::Client.new( + base-uri => $url, + headers => [ Authorization => "Bearer $api-key" ], + content-type => 'application/json', + ); + + my $resp = await $client.post('', body => %( q => $text ) ); + my $json = await $resp.body; + + if $json:exists { + return $json; + } else { + return; + } + + CATCH { + when X::Cro::HTTP::Error { + my $body = await .response.body; + die "Error from API endpoint: $body"; + } + } +} + -- cgit