-
Notifications
You must be signed in to change notification settings - Fork 20
/
urban
executable file
·140 lines (114 loc) · 3.09 KB
/
urban
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env perl
# SPDX-FileCopyrightText: 2009-2023 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
use warnings;
use strict;
use utf8;
use Encode;
use WebService::UrbanDictionary;
use Getopt::Long qw(GetOptionsFromString);
Getopt::Long::Configure ("bundling");
my $getopt_error;
local $SIG{__WARN__} = sub {
$getopt_error = shift;
chomp $getopt_error;
};
my $usage = "Usage: udict [-a] [-m <show definition matching this regex>] [-n <entry number>] [-s <up/down (sort by thumbs up/down)>] <phrase>\n";
binmode(STDOUT, ":utf8");
binmode(STDERR, ":utf8");
@ARGV = map { decode('UTF-8', $_, 1) } @ARGV;
my ($entry, $sort, $match, $show_all);
my $arguments = join(' ', @ARGV);
$arguments =~ s/'/\\'/g;
my ($ret, $args) = GetOptionsFromString($arguments,
'a' => \$show_all,
'm=s' => \$match,
'n=i' => \$entry,
's=s' => \$sort);
print "$getopt_error -- $usage" and exit if defined $getopt_error;
print "Missing phrase -- $usage" and exit if @$args == 0 and not $match and not $entry;
if (@$args == 0) {
open my $fh, "<", "udict.last";
if ($fh) {
$args = <$fh>;
chomp $args;
close $fh;
} else {
$args = "wtf";
}
} else {
$args = join(' ', @$args);
open my $fh, ">", "udict.last";
print $fh "$args\n";
close $fh;
}
my $ud = WebService::UrbanDictionary->new;
my $results = $ud->request($args);
sub sort_entries {
if (defined $sort) {
if (lc $sort eq 'down' or lc $sort eq 'd') {
return $a->{'thumbs_up'} <=> $b->{'thumbs_up'};
} else {
return $b->{'thumbs_up'} <=> $a->{'thumbs_up'};
}
} else {
return $b->{'thumbs_up'} <=> $a->{'thumbs_up'};
}
}
my @entries = sort sort_entries @{ $results->definitions };
my $num_entries = @entries;
if ($num_entries == 0) {
print "$args: no definition found.\n";
exit;
}
if (defined $entry) {
if ($entry < 1 or $entry > $num_entries) {
if ($num_entries == 1) {
print "There is only one entry for $args.\n";
} else {
print "$args: no such entry. There are $num_entries entries.\n";
}
exit;
}
$entry--;
}
sub show_definition {
my $entry = shift;
my $num = shift;
$num = 1 if not defined $num;
if ($num_entries > 1) {
print "$num/$num_entries: ";
}
$entry->{'definition'} =~ s/\[(.*?)\]/$1/g;
$entry->{'example'} =~ s/\[(.*?)\]/$1/g if $entry->{'example'};
print "(+$entry->{'thumbs_up'}/-$entry->{'thumbs_down'}) $entry->{'definition'}\n";
print "«$entry->{'example'}»\n" if $entry->{'example'};
}
if (defined $show_all or defined $match) {
my $shown;
eval {
my $n = 0;
for my $each (@entries) {
$n++;
if (defined $match) {
my $def = $each->{'definition'} . "\n";
$def .= "«$each->{'example'}»" if $each->{'example'};
next if $def !~ m/$match/im;
}
show_definition($each, $n);
print "\n";
$shown++;
}
};
if ($@) {
my $err = $@;
$err =~ s/; marked by <-- HERE.*/: $match/;
print "Oops, $err\n";
exit;
}
if (not $shown) {
print "$args: no definition matching '$match' found.\n";
}
} else {
show_definition($entries[$entry], $entry + 1);
}