line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package POE::Component::IRC::Plugin::WWW::Reddit::TIL; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
13244
|
use 5.010; |
|
1
|
|
|
|
|
3
|
|
4
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
18
|
|
5
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
36
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.09'; |
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
452
|
use POE::Component::IRC::Plugin qw( :ALL ); |
|
1
|
|
|
|
|
379
|
|
|
1
|
|
|
|
|
112
|
|
10
|
1
|
|
|
1
|
|
482
|
use Mojo::UserAgent; |
|
1
|
|
|
|
|
247170
|
|
|
1
|
|
|
|
|
9
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
1
|
|
|
1
|
0
|
481
|
my $package = shift; |
14
|
1
|
|
|
|
|
3
|
return bless {}, $package; |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub PCI_register { |
18
|
0
|
|
|
0
|
0
|
0
|
my ($self, $irc) = splice @_, 0, 2; |
19
|
|
|
|
|
|
|
|
20
|
0
|
|
|
|
|
0
|
$irc->plugin_register($self, 'SERVER', qw(public)); |
21
|
0
|
|
|
|
|
0
|
return 1; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# This is method is mandatory but we don't actually have anything to do. |
25
|
|
|
|
|
|
|
sub PCI_unregister { |
26
|
0
|
|
|
0
|
0
|
0
|
return 1; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub S_public { |
30
|
0
|
|
|
0
|
0
|
0
|
my ($self, $irc) = splice @_, 0, 2; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# Parameters are passed as scalar-refs including arrayrefs. |
33
|
0
|
|
|
|
|
0
|
my $nick = (split /!/, ${$_[0]})[0]; |
|
0
|
|
|
|
|
0
|
|
34
|
0
|
|
|
|
|
0
|
my $channel = ${$_[1]}->[0]; |
|
0
|
|
|
|
|
0
|
|
35
|
0
|
|
|
|
|
0
|
my $msg = ${$_[2]}; |
|
0
|
|
|
|
|
0
|
|
36
|
|
|
|
|
|
|
|
37
|
0
|
|
0
|
|
|
0
|
my $reply = $msg =~ /^!til\b/i && $self->_get_TIL; |
38
|
|
|
|
|
|
|
|
39
|
0
|
0
|
|
|
|
0
|
if ($reply) { |
40
|
0
|
|
|
|
|
0
|
$irc->yield(privmsg => $channel => "$nick: $reply"); |
41
|
0
|
|
|
|
|
0
|
return PCI_EAT_PLUGIN; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# Default action is to allow other plugins to process it. |
45
|
0
|
|
|
|
|
0
|
return PCI_EAT_NONE; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub _get_TIL { |
49
|
1
|
|
|
1
|
|
237
|
my $ua = Mojo::UserAgent->new; |
50
|
1
|
|
|
|
|
6
|
my @posts; |
51
|
|
|
|
|
|
|
|
52
|
1
|
|
|
|
|
6
|
my $json = $ua->get('https://www.reddit.com/r/todayilearned.json')->res->json; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# Children are posts in reddit world. |
55
|
1
|
|
|
|
|
420257
|
foreach my $post (@{$json->{data}{children}}) { |
|
1
|
|
|
|
|
8
|
|
56
|
|
|
|
|
|
|
my %info = ( |
57
|
|
|
|
|
|
|
url => 'https://reddit.com/' . $post->{data}{id}, |
58
|
|
|
|
|
|
|
title => $post->{data}{title}, |
59
|
25
|
|
|
|
|
75
|
); |
60
|
25
|
|
|
|
|
27
|
push @posts, \%info; |
61
|
|
|
|
|
|
|
} |
62
|
1
|
50
|
|
|
|
4
|
unless (@posts) { |
63
|
0
|
|
|
|
|
0
|
warn q@Found no posts! Something's probably wrong.@; |
64
|
0
|
|
|
|
|
0
|
return; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
1
|
|
|
|
|
5
|
my $random = $posts[rand @posts]; |
68
|
1
|
|
|
|
|
18
|
return $random->{title} . ' ' . $random->{url}; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
1; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
__END__ |