| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Bot::Cobalt::Plugin::Urban; |
|
2
|
|
|
|
|
|
|
# ABSTRACT: Bot::Cobalt plugin for looking up urban dictionary terms |
|
3
|
|
|
|
|
|
|
$Bot::Cobalt::Plugin::Urban::VERSION = '0.001'; |
|
4
|
1
|
|
|
1
|
|
25914
|
use strict; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
38
|
|
|
5
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
29
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
492
|
use Bot::Cobalt; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use Bot::Cobalt::Common; |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use HTTP::Tiny; |
|
11
|
|
|
|
|
|
|
use JSON qw(decode_json); |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my $URBAN_API = 'http://api.urbandictionary.com/v0'; |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub new { bless {}, shift } |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub Cobalt_register { |
|
18
|
|
|
|
|
|
|
my $self = shift; |
|
19
|
|
|
|
|
|
|
my $core = shift; |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
register( $self, 'SERVER', 'public_cmd_urban', 'public_cmd_urban_random' ); |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
logger->info("Registered, commands: !urban, !urban_random"); |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
return PLUGIN_EAT_NONE; |
|
26
|
|
|
|
|
|
|
} |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub Cobalt_unregister { |
|
29
|
|
|
|
|
|
|
my $self = shift; |
|
30
|
|
|
|
|
|
|
my $core = shift; |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
logger->info("Unregistered"); |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
return PLUGIN_EAT_NONE; |
|
35
|
|
|
|
|
|
|
} |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub Bot_public_cmd_urban_random { |
|
38
|
|
|
|
|
|
|
my $self = shift; |
|
39
|
|
|
|
|
|
|
my $core = shift; |
|
40
|
|
|
|
|
|
|
my $msg = ${ shift() }; |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
my $context = $msg->context; |
|
43
|
|
|
|
|
|
|
my $channel = $msg->target; |
|
44
|
|
|
|
|
|
|
my $nick = $msg->src_nick; |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
my $url = "$URBAN_API/random"; |
|
47
|
|
|
|
|
|
|
my $resp = HTTP::Tiny->new->get($url); |
|
48
|
|
|
|
|
|
|
my $json = decode_json($resp->{content}); |
|
49
|
|
|
|
|
|
|
my $total = scalar(@{$json->{list}}); |
|
50
|
|
|
|
|
|
|
my $idx = int(rand($total)); |
|
51
|
|
|
|
|
|
|
my $entry = $json->{list}->[$idx]; |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
my $example = ''; |
|
54
|
|
|
|
|
|
|
if ($entry->{example}) { |
|
55
|
|
|
|
|
|
|
$example = sprintf( |
|
56
|
|
|
|
|
|
|
'%sExample: %s', |
|
57
|
|
|
|
|
|
|
$entry->{definition} =~ /\n/ ? "\n" : '- ', |
|
58
|
|
|
|
|
|
|
$entry->{example}, |
|
59
|
|
|
|
|
|
|
); |
|
60
|
|
|
|
|
|
|
} |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
my $def = sprintf( |
|
63
|
|
|
|
|
|
|
'[%d/%d] %s: %s %s', |
|
64
|
|
|
|
|
|
|
$idx + 1, |
|
65
|
|
|
|
|
|
|
$total, |
|
66
|
|
|
|
|
|
|
$entry->{word}, |
|
67
|
|
|
|
|
|
|
$entry->{definition}, |
|
68
|
|
|
|
|
|
|
$example, |
|
69
|
|
|
|
|
|
|
); |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
broadcast( 'message', $context, $channel, $_ ) |
|
72
|
|
|
|
|
|
|
foreach split /\n/, $def; |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
return PLUGIN_EAT_ALL; |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub Bot_public_cmd_urban { |
|
78
|
|
|
|
|
|
|
my $self = shift; |
|
79
|
|
|
|
|
|
|
my $core = shift; |
|
80
|
|
|
|
|
|
|
my $msg = ${ shift() }; |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
my $context = $msg->context; |
|
83
|
|
|
|
|
|
|
my $channel = $msg->target; |
|
84
|
|
|
|
|
|
|
my $nick = $msg->src_nick; |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
my $url = "$URBAN_API/define?"; |
|
87
|
|
|
|
|
|
|
my @split = split(/ /, $msg->stripped); |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
shift @split; # shift off the command |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
my $idx = ($split[-1] =~ m/^\d$/) ? ( pop(@split) - 1) : 0; |
|
92
|
|
|
|
|
|
|
my $term = join('+', @split); |
|
93
|
|
|
|
|
|
|
my $resp = HTTP::Tiny->new->get($url . "term=$term"); |
|
94
|
|
|
|
|
|
|
my $json = decode_json($resp->{content}); |
|
95
|
|
|
|
|
|
|
my $total = scalar(@{$json->{list}}); |
|
96
|
|
|
|
|
|
|
my $entry = $json->{list}->[$idx]; |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
if (not defined $entry) { |
|
99
|
|
|
|
|
|
|
my $no_results = "$nick: That doesn't even exist in urban dictionary, " |
|
100
|
|
|
|
|
|
|
. "stop making stuff up."; |
|
101
|
|
|
|
|
|
|
broadcast( 'message', $context, $channel, $no_results ); |
|
102
|
|
|
|
|
|
|
return PLUGIN_EAT_ALL; |
|
103
|
|
|
|
|
|
|
} |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
my $example = ''; |
|
106
|
|
|
|
|
|
|
if ($entry->{example}) { |
|
107
|
|
|
|
|
|
|
$example = sprintf( |
|
108
|
|
|
|
|
|
|
'%sExample: %s', |
|
109
|
|
|
|
|
|
|
$entry->{definition} =~ /\n/ ? "\n" : '- ', |
|
110
|
|
|
|
|
|
|
$entry->{example}, |
|
111
|
|
|
|
|
|
|
); |
|
112
|
|
|
|
|
|
|
} |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
my $def = sprintf( |
|
115
|
|
|
|
|
|
|
'[%d/%d] %s: %s %s', |
|
116
|
|
|
|
|
|
|
$idx + 1, |
|
117
|
|
|
|
|
|
|
$total, |
|
118
|
|
|
|
|
|
|
$entry->{word}, |
|
119
|
|
|
|
|
|
|
$entry->{definition}, |
|
120
|
|
|
|
|
|
|
$example, |
|
121
|
|
|
|
|
|
|
); |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
broadcast( 'message', $context, $channel, $_ ) |
|
124
|
|
|
|
|
|
|
foreach split /\n/, $def; |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
return PLUGIN_EAT_ALL; |
|
127
|
|
|
|
|
|
|
} |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
1; |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
__END__ |