blib/lib/Bot/BasicBot/Pluggable/Module/VieDeMerde.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Bot::BasicBot::Pluggable::Module::VieDeMerde;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use WWW::VieDeMerde;
Subroutine BEGIN: called CC=1 SCAR=2.6
8  
9 use base qw(Bot::BasicBot::Pluggable::Module);
10  
11 our $VERSION = '0.2';
12  
13 sub render_item {
14 my $msg = shift;
15 my $response = decode_entities($msg->text);
16 $response .= ' #' . $msg->id;
17 $response .= ' (+' . $msg->agree . ',-' . $msg->deserved . ')';
18  
19 return $response;
20 }
21  
22 sub render_comment {
23 my $msg = shift;
24 my $response = decode_entities($msg->text);
25 $response .= ' (' . $msg->author . ')';
26  
27 return $response;
28 }
29  
30 sub said {
31 my ($self, $mess, $pri) = @_;
32  
33 my $body = $mess->{body};
34 my $who = $mess->{who};
35  
36 return unless $body =~ /^\@?(vdm|fml)(?:\s+(.*))?$/;
37 my $site = $1;
38 my $cmd = $2 // 'random';
39  
40 my $lang;
41 $lang = 'fr' if $site eq 'vdm';
42 $lang = 'en' if $site eq 'fml';
43 my $vdm = WWW::VieDeMerde->new(lang => $lang);
44  
45 my $msg;
46 if ($cmd) {
47 if ($cmd eq 'random') {
48 return render_item($vdm->random());
49 }
50 if ($cmd =~ /comments\s+#?(\d+)(?:\s+(?:(?:#?(\d+))|(--all)))?/) {
51 my @comments = $vdm->comments($1);
52 if (@comments) {
53 if (defined($3)) {
54 return join("\n", map { render_comment($_) } @comments[0,15]);
55 }
56 else {
57 if (defined($2)) {
58 return 'Error comments begin at #1' if $2 == 0;
59 return "Warning, your request can be accurate due to deleted comments\n". render_comment($comments[$2 - 1]);
60 }
61 else {
62 return render_comment($comments[int(rand(@comments))]);
63 }
64 }
65 }
66 else {
67 return "No comments found for id $1 in viedemerde.fr" if $lang eq 'fr';
68 return "No comments found for id $1 in fmylife.com" if $lang eq 'en';
69 return "No comments found for id $1 and language $lang";
70 }
71 }
72 if ($cmd =~ /#?(\d+)/) {
73 my $item = $vdm->get($1);
74 if (defined $item) {
75 return render_item($item);
76 }
77 else {
78 return "No item found for id $1 in viedemerde.fr" if $lang eq 'fr';
79 return "No item found for id $1 in fmylife.com" if $lang eq 'en';
80 return "No item found for id $1 and language $lang";
81 }
82 }
83 if ($cmd =~ /top\s*(day|week|month|all)(\s+--all)?/) {
84 my @items;
85 @items = $vdm->top_day() if $1 eq 'day';
86 @items = $vdm->top_week() if $1 eq 'week';
87 @items = $vdm->top_month() if $1 eq 'month';
88 @items = $vdm->top() if $1 eq 'all';
89 if (@items) {
90 if (defined($2)) {
91 return join("\n", map { render_item($_) } @items);
92 }
93 else {
94 return render_item($items[int(rand(@items))]);
95 }
96 }
97 else {
98 return "Error, no top for this $1." unless @items;
99 }
100 }
101 if ($cmd =~ /flop\s*(day|week|month|all)(\s+--all)?/) {
102 my @items;
103 @items = $vdm->flop_day() if $1 eq 'day';
104 @items = $vdm->flop_week() if $1 eq 'week';
105 @items = $vdm->flop_month() if $1 eq 'month';
106 @items = $vdm->flop() if $1 eq 'all';
107 if (@items) {
108 if (defined($2)) {
109 return join("\n", map { render_item($_) } @items);
110 }
111 else {
112 return render_item($items[int(rand(@items))]);
113 }
114 }
115 else {
116 return "Error, no flop for this $1." unless @items;
117 }
118 }
119 if ($cmd =~ /lasts(\s+--all)?/) {
120 my @items = $vdm->last();
121 if (@items) {
122 if (defined($1)) {
123 return join("\n", map { render_item($_) } @items);
124 }
125 else {
126 return render_item($items[int(rand(@items))]);
127 }
128 }
129 else {
130 return "No items, this shouldn't happen." unless @items;
131 }
132 }
133 if ($cmd =~ /last/) {
134 my @items = $vdm->last();
135 return render_item($items[0]);
136 }
137 return "Unknown command '$site $cmd' in Bot::BasicBot::Pluggable::Module::VieDeMerde";
138 }
139 }
140  
141 sub help {
142 "Display a quote from viedemerde.fr or fmylife.com.\n"
143 . "Usage vdm, vdm #id, vdm comments #id, vdm last, vdm lasts [--all], vdm top day|week|month|global [--all], vdm flop day|week|month|global [--all], vdm comments #id [#id_comment|--all].\n
144 "
145 . "You can replace vdm by fml.\nYou can prefix every command with a @ for drop-in compatibilty with the geekquotes module for supybot."
146 }
147  
148 1;
149  
150 __END__
↑ Summary