/
ch-extra.pl
executable file
·51 lines (37 loc) · 958 Bytes
/
ch-extra.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#! /usr/bin/perl
use v5.22;
use Mojo::UserAgent;
use Mojo::URL;
use Mojo::File qw/path/;
use Mojo::Util qw/encode/;
use utf8;
#task: use the language detection api
sub api_path {
Mojo::URL->new('https://ws.detectlanguage.com/0.2/')->path(shift)
}
say api_path('detect');
my $auth_key = path('.langdetectkey')->slurp;
chomp $auth_key;
say $auth_key;
my $headers = { Authorization => "Bearer $auth_key" };
my $ua = Mojo::UserAgent->new;
sub detect {
$ua->post( api_path('detect'),
$headers, json => { q => shift } )->res->json
}
sub user_status {
$ua->get( api_path('user/status') , $headers)->res->json
}
sub languages {
$ua->get( api_path('languages'))->res->json
}
use Data::Printer;
my $res;
$res = detect('how are you gentlemen?');
p $res;
$res = detect([ 'All your base are belong to us', '戦いが始まりました', 'שלום עליכם, רבותי' ]);
p $res;
$res = user_status();
p $res;
$res = languages();
p $res;