2017-05-14 08:40:56 +02:00
|
|
|
#!/usr/bin/env perl
|
2017-03-05 22:33:31 +01:00
|
|
|
|
2017-05-14 08:42:43 +02:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2017-05-14 08:40:56 +02:00
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
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 [-m <show definition matching this regex>] [-n <entry number>] [-s <up/down (sort by thumbs up/down)>] <phrase>\n";
|
|
|
|
|
|
|
|
my ($entry, $sort, $match, $show_all);
|
|
|
|
my $arguments = join(' ', @ARGV);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
$args = join(' ', @$args);
|
|
|
|
|
|
|
|
my $ud = WebService::UrbanDictionary->new;
|
2017-05-14 08:42:43 +02:00
|
|
|
my $results = $ud->request($args);
|
2017-05-14 08:40:56 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
} else {
|
|
|
|
=cut
|
|
|
|
if ($num_entries > 1 and not defined $entry) {
|
|
|
|
print "$num_entries entries:\n";
|
|
|
|
}
|
|
|
|
=cut
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defined $entry) {
|
|
|
|
if ($entry < 1 or $entry > $num_entries) {
|
|
|
|
print "No such entry. There are $num_entries for $args.\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
$entry--;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub show_definition {
|
|
|
|
my $entry = shift;
|
|
|
|
|
|
|
|
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 {
|
|
|
|
for my $each (@entries) {
|
|
|
|
if (defined $match) {
|
|
|
|
my $def = $each->{'definition'} . "\n" . $each->{'example'};
|
|
|
|
next if $def !~ m/$match/im;
|
|
|
|
}
|
|
|
|
show_definition($each);
|
|
|
|
print "\n";
|
|
|
|
$shown++;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($@) {
|
|
|
|
my $err = $@;
|
2017-05-14 08:42:43 +02:00
|
|
|
|
2017-05-14 08:40:56 +02:00
|
|
|
$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]);
|
|
|
|
}
|