-
Notifications
You must be signed in to change notification settings - Fork 0
/
dict
executable file
·74 lines (55 loc) · 1.25 KB
/
dict
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env perl
# martin, 2018-12-10, 2019-01-12, 2019-06-30, 2020-12-20, 2022-01-05
# ... ask dict.cc for a translation
use strict;
use warnings;
use v5;
use Encode;
use LWP::UserAgent;
use URI::Escape;
use open qw(:std :utf8);
sub fetch_list {
my ($htmlref, $id) = @_;
return
grep { $_ ne '' }
map { s/\\'/'/g; $_ }
map { s/(^"|"$)//g; $_ }
map { /\((.*?)\);/; split /","/, $1 }
grep { /var c${id}Arr/ }
split /\n/, $$htmlref;
}
sub lookup {
my $term = join(' ', @_);
my $url = 'https://www.dict.cc/?s=' . uri_escape($term);
my $ua = LWP::UserAgent->new(agent => 'junidict/1.0');
my $res = $ua->get($url);
if (!$res->is_success) {
die "dict.cc returned: " . $res->code . "\n";
}
my $html = $res->decoded_content;
my @xs1 = fetch_list(\$html, 1);
my @xs2 = fetch_list(\$html, 2);
for (0..$#xs1) {
printf "%s -- %s\n", $xs1[$_], $xs2[$_];
}
}
sub show_help {
print <<'EOF';
Usage: dict [-h | words...]
Look up a German/English translation in dict.cc
Example: dict talk to the hand
This file is part of Martin's scripts.
<https://github.com/nanont/scripts>
Licensed under MIT.
EOF
}
sub main {
for (@ARGV) {
if (/^--?h(elp)?$/) {
show_help;
exit;
}
}
lookup(@ARGV);
}
main;