diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-07-07 19:15:57 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-07-07 19:15:57 +0100 |
| commit | 4587eba6316ea240baaf1b464a205ee34a05a0f6 (patch) | |
| tree | fa6dc97a1d315ceea430d43c60fb807e841a113b | |
| parent | 3c2e164e9771e96e943684a449968c050de0b9f6 (diff) | |
| parent | 7685ad5fd2a26c1abcc7015485f4b8b98741505d (diff) | |
| download | perlweeklychallenge-club-4587eba6316ea240baaf1b464a205ee34a05a0f6.tar.gz perlweeklychallenge-club-4587eba6316ea240baaf1b464a205ee34a05a0f6.tar.bz2 perlweeklychallenge-club-4587eba6316ea240baaf1b464a205ee34a05a0f6.zip | |
Merge pull request #343 from jmaslak/joelle-15-3-1
Solutions for 15.3 in P5 & P6
| -rwxr-xr-x | challenge-015/joelle-maslak/perl5/ch-3.pl | 68 | ||||
| -rwxr-xr-x | challenge-015/joelle-maslak/perl6/ch-3.p6 | 58 |
2 files changed, 126 insertions, 0 deletions
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 (<stdin>) { $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<detections>[0]; + say "Language: { $result<language> }"; + say "Is reliable: { $result<isReliable> }"; + say "Confidence: { $result<confidence>.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<data>:exists { + return $json<data>; + } else { + return; + } + + CATCH { + when X::Cro::HTTP::Error { + my $body = await .response.body; + die "Error from API endpoint: $body"; + } + } +} + |
