blib/lib/Bot/Cobalt/Plugin/Twitter.pm

filter: Statementstmt 26.5% 31 / 117 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 47.6% 10 / 21 Podpod 33.3% 4 / 12 totaltotal 22.7% 45 / 198 CC 22 SCAR 50.1
CC
22
Cov
20%
Bot_public_msg
342.0
Cobalt_register
20.0
Bot_topic_changed
6.0
CRAP 149.4
↑ Summary
1 package Bot::Cobalt::Plugin::Twitter;
2 # ABSTRACT: Bot::Cobalt plugin for automatic tweeting
3 1 $Bot::Cobalt::Plugin::Twitter::VERSION = '0.004';
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Bot::Cobalt;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Bot::Cobalt::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use HTML::Entities qw(decode_entities);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Twitter::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Text::Unidecode qw(unidecode);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use URI::Find::Simple qw(list_uris);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 my $status_rx = qr/twitter\.com\/\w+\/status\/(\d+)/;
17  
18 1 ▶sub new { bless {}, shift }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
19 0 ▶sub twitter { shift->{twitter} }
Subroutine twitter: not called CC=1 SCAR=6.9
Pod undocumented
20  
21 0 ▶sub display_tweets { shift->{display_tweets} }
Subroutine display_tweets: not called CC=1 SCAR=6.9
Pod documented
22 0 ▶sub tweet_topics { shift->{tweet_topics} }
Subroutine tweet_topics: not called CC=1 SCAR=6.9
Pod documented
23 0 ▶sub tweet_links { shift->{tweet_links} }
Subroutine tweet_links: not called CC=1 SCAR=6.9
Pod documented
24 0 ▶sub retweet_tweets { shift->{retweet_tweets} }
Subroutine retweet_tweets: not called CC=1 SCAR=6.9
Pod documented
25 0 ▶sub ua { shift->{useragent} }
Subroutine ua: not called CC=1 SCAR=6.9
Pod undocumented
26  
27 0 ▶sub Cobalt_register {
Subroutine Cobalt_register: not called CC=4 SCAR=30.0
Pod undocumented
28 0 my $self = shift;
29 0 my $core = shift;
30 0 my $conf = $core->get_plugin_cfg($self);
31  
32 0 ▶ $self->{display_tweets} = $conf->{display_tweets} // 1;
Condition0%0/2
$conf->{"display_tweets"} // 1
l!l
00
MC/DC0%0/1
$conf->{"display_tweets"} // 1
$conf->{"display_tweets"}
Truth table0%0/2
A
A= $conf->{"display_tweets"}
Aresult
11
00
33 0 ▶ $self->{tweet_topics} = $conf->{tweet_topics} // 1;
Condition0%0/2
$conf->{"tweet_topics"} // 1
l!l
00
MC/DC0%0/1
$conf->{"tweet_topics"} // 1
$conf->{"tweet_topics"}
Truth table0%0/2
A
A= $conf->{"tweet_topics"}
Aresult
11
00
34 0 $self->{tweet_links} = $conf->{tweet_links};
35 0 $self->{retweet_tweets} = $conf->{retweet_tweets};
36 0 $self->{useragent} = Mojo::UserAgent->new;
37  
38 0 eval {
39 $self->{twitter} = Twitter::API->new_with_traits(
40 traits => 'Enchilada',
41 consumer_key => $conf->{consumer_key},
42 consumer_secret => $conf->{consumer_secret},
43 access_token => $conf->{access_token},
44 access_token_secret => $conf->{access_token_secret},
45 0 );
46 };
47  
48 0 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 0 0
49 0 logger->warn("Unable to create Twitter::API object: $err");
50 }
51  
52 0 register( $self, 'SERVER', qw(public_msg public_cmd_tweet topic_changed) );
53 0 logger->info("Registered, commands: !tweet");
54  
55 0 return PLUGIN_EAT_NONE;
56 }
57  
58 0 ▶sub Cobalt_unregister {
Subroutine Cobalt_unregister: not called CC=1 SCAR=6.9
Pod undocumented
59 0 my $self = shift;
60 0 my $core = shift;
61  
62 0 logger->info("Unregistered");
63
64 0 return PLUGIN_EAT_NONE;
65 }
66  
67 0 ▶sub Bot_topic_changed {
Subroutine Bot_topic_changed: not called CC=2 SCAR=17.9
Pod undocumented
68 0 my $self = shift;
69 0 my $core = shift;
70 0 my $topic = ${ shift() };
71  
72 0 ▶ return PLUGIN_EAT_NONE if not $self->tweet_topics;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->tweet_topics 0 0 0
73  
74 0 my $new_topic = $topic->stripped;
75 0 my $status = substr($new_topic, 0, 139);
76  
77 0 $self->twitter->update($status);
78  
79 0 return PLUGIN_EAT_NONE;
80 }
81  
82 0 ▶sub Bot_public_cmd_tweet {
Subroutine Bot_public_cmd_tweet: not called CC=1 SCAR=6.9
Pod undocumented
83 0 my $self = shift;
84 0 my $core = shift;
85 0 my $msg = ${ shift() };
86  
87 0 my $context = $msg->context;
88 0 my $channel = $msg->target;
89 0 my $nick = $msg->src_nick;
90  
91 0 my @split = split(/ /, $msg->stripped);
92  
93 0 shift @split; # shift off the command
94  
95 0 my $status = substr( join(' ', @split), 0, 139);
96  
97 0 $self->twitter->update($status);
98  
99 0 return PLUGIN_EAT_ALL;
100 }
101  
102 0 ▶sub Bot_public_msg {
Subroutine Bot_public_msg: not called CC=18 SCAR=58.3
Pod undocumented
103 0 my $self = shift;
104 0 my $core = shift;
105 0 my $msg = ${ shift() };
106  
107 0 ▶ return PLUGIN_EAT_NONE
Branch: 1 branch
BranchTrueFalseTotal
unless $self->display_tweets or $self->tweet_links or $self->retweet_tweets 0 0 0
Condition0%0/6
$self->display_tweets or $self->tweet_links or $self->retweet_tweets
l!l&&r!l&&!r
000
$self->display_tweets or $self->tweet_links
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->display_tweets or $self->tweet_links or $self->retweet_tweets
$self->display_tweets $self->tweet_links $self->retweet_tweets
Truth table0%0/4
A or B or C
A= $self->display_tweetsB= $self->tweet_linksC= $self->retweet_tweets
ABCresult
1XX1
01X1
0011
0000
108 unless $self->display_tweets or $self->tweet_links or $self->retweet_tweets;
109  
110 0 my $context = $msg->context;
111 0 my $channel = $msg->target;
112  
113 0 foreach my $uri ( list_uris($msg->message) ) {
114 0 ▶ next if not $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri 0 0 0
115  
116 0 ▶ if ($uri =~ $status_rx) {
Branch: 2 branches
BranchTrueFalseTotal
if ($uri =~ /$status_rx/) { } 0 0 0
elsif ($self->tweet_links) { } 0 0 0
117 0 my $id = $1;
118 0 ▶ if ($self->retweet_tweets) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->retweet_tweets) 0 0 0
119 0 eval { $self->twitter->retweet($id) };
120 0 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 0 0
121 0 logger->warn(
122 "Failed to retweet [$id] - " . $err->twitter_error_text,
123 );
124 }
125 }
126
127 0 ▶ if ($self->display_tweets) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->display_tweets) 0 0 0
128 0 my $tweet = $self->twitter->show_status($id, { tweet_mode => 'extended' });
129 0 ▶ my $text = $tweet->{truncated} ? $tweet->{text} : $tweet->{full_text};
Branch: 1 branch
BranchTrueFalseTotal
$tweet->{'truncated'} ? : 0 0 0
130 0 my $name = $tweet->{user}->{name};
131 0 my $sname = $tweet->{user}->{screen_name};
132 0 my $user = sprintf '%s (@%s)', $name, $sname;
133
134 0 $text = unidecode(decode_entities($text));
135 0 my @lines = split /\n/, $text;
136  
137 0 ▶ if (@lines == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines == 1) { } 0 0 0
138 0 broadcast( 'message', $context, $channel, "$user - $lines[0]" );
139 }
140 else {
141 0 broadcast( 'message', $context, $channel, $user );
142 broadcast( 'message', $context, $channel, " - $_" )
143 0 foreach @lines;
144 }
145 0 return PLUGIN_EAT_ALL;
146 }
147 }
148 elsif ($self->tweet_links) {
149 0 my $title = $self->ua->get($uri)->result->dom->at('title')->text;
150 0 ▶ my $uni = unidecode($title) || $title;
Condition0%0/3
unidecode($title) || $title
l!l&&r!l&&!r
000
MC/DC0%0/2
unidecode($title) || $title
unidecode($title) $title
Truth table0%0/3
A || B
A= unidecode($title)B= $title
ABresult
1X1
011
000
151 0 my $short = substr($uni, 0, 100);
152
153 0 ▶ next if not $title;
Branch: 1 branch
BranchTrueFalseTotal
unless $title 0 0 0
154  
155 0 ▶ if (length($short) < length($uni)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $short < length $uni) 0 0 0
156 0 $short = "$short...";
157 }
158  
159 0 eval { $self->twitter->update("$short - $uri") };
160  
161 0 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 0 0
162 0 logger->warn(
163 "Failed to tweet URL [$uri]: " . $err->twitter_error_text,
164 );
165 }
166 }
167 }
168 0 return PLUGIN_EAT_NONE;
169 }
170  
171 1 1;
172  
173 __END__
↑ Summary