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

filter: Statementstmt 39.1% 27 / 69 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 69.2% 9 / 13 Podpod 0.0% 0 / 5 totaltotal 34.6% 36 / 104 CC 10 SCAR 36.4
CC
10
Cov
32%
Bot_public_msg
90.0
Cobalt_register
6.0
twitter
2.0
CRAP 38.0
↑ Summary
1 package Bot::Cobalt::Plugin::URLTitle;
2 # ABSTRACT: Bot::Cobalt plugin for printing the title of a URL
3 1 $Bot::Cobalt::Plugin::URLTitle::VERSION = '0.002';
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 Text::Unidecode qw(unidecode);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use URI::Title qw(title);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use URI::Find::Simple qw(list_uris);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶sub new { bless {}, shift }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
16 0 ▶sub twitter { shift->{twitter} }
Subroutine twitter: not called CC=1 SCAR=6.9
Pod undocumented
17  
18 sub Cobalt_register {
19 0 ▶ my $self = shift;
Subroutine Cobalt_register: not called CC=2 SCAR=17.9
Pod undocumented
20 0 my $core = shift;
21 0 my $conf = $core->get_plugin_cfg($self);
22  
23 0 eval {
24 0 require Net::Twitter;
25  
26 $self->{twitter} = Net::Twitter->new(
27 traits => [ qw(API::RESTv1_1 RetryOnError) ],
28 consumer_key => $conf->{consumer_key},
29 consumer_secret => $conf->{consumer_secret},
30 access_token => $conf->{access_token},
31 access_token_secret => $conf->{access_token_secret},
32 0 ssl => 1,
33 );
34 };
35  
36 0 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 0 0
37 0 logger->warn("Unable to create Net::Twitter object: $err");
38 }
39  
40 0 register( $self, 'SERVER', 'public_msg' );
41 0 logger->info("Registered");
42  
43 0 return PLUGIN_EAT_NONE;
44 }
45  
46 sub Cobalt_unregister {
47 0 ▶ my $self = shift;
Subroutine Cobalt_unregister: not called CC=1 SCAR=6.9
Pod undocumented
48 0 my $core = shift;
49  
50 0 logger->info("Unregistered");
51
52 0 return PLUGIN_EAT_NONE;
53 }
54  
55 sub Bot_public_msg {
56 0 ▶ my $self = shift;
Subroutine Bot_public_msg: not called CC=9 SCAR=45.0
Pod undocumented
57 0 my $core = shift;
58 0 my $msg = ${ shift() };
59  
60 0 my $context = $msg->context;
61 0 my $channel = $msg->target;
62  
63 0 foreach my $uri ( list_uris($msg->message) ) {
64 0 ▶ next if not $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri 0 0 0
65  
66 # There's probably a less fragile way to do this...
67 0 ▶ if ($uri =~ /twitter\.com\/\w+\/status\/(\d+)/ and $self->twitter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ m[twitter\.com/\w+/status/(\d+)] and $self->twitter) { } 0 0 0
Condition0%0/3
$uri =~ m[twitter\.com/\w+/status/(\d+)] and $self->twitter
!ll&&!rl&&r
000
MC/DC0%0/2
$uri =~ m[twitter\.com/\w+/status/(\d+)] and $self->twitter
$uri =~ m[twitter\.com/\w+/status/(\d+)] $self->twitter
Truth table0%0/3
A and B
A= $uri =~ m[twitter\.com/\w+/status/(\d+)]B= $self->twitter
ABresult
0X0
100
111
68 0 my $id = $1;
69  
70 0 my $tweet = $self->twitter->show_status($id);
71 0 my $text = $tweet->{text};
72 0 my $name = $tweet->{user}->{name};
73 0 my $sname = $tweet->{user}->{screen_name};
74 0 my $user = sprintf '%s (@%s)', $name, $sname;
75  
76 0 $text = unidecode(decode_entities($text));
77 0 my @lines = split /\n/, $text;
78  
79 0 ▶ if (@lines == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines == 1) { } 0 0 0
80 0 broadcast( 'message', $context, $channel, "$user - $lines[0]" );
81 }
82 else {
83 0 broadcast( 'message', $context, $channel, $user );
84 broadcast( 'message', $context, $channel, " - $_" )
85 0 foreach @lines;
86 }
87 }
88 else {
89 0 ▶ my $title = decode_entities(title($uri)) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $title = decode_entities(title($uri)) or next 0 0 0
90 0 my $uni = unidecode($title);
91 0 ▶ my $resp = sprintf('[ %s ]', $uni ? $uni : $title);
Branch: 1 branch
BranchTrueFalseTotal
$uni ? : 0 0 0
92 0 broadcast( 'message', $context, $channel, $resp );
93 }
94 }
95  
96 0 return PLUGIN_EAT_NONE;
97 }
98  
99 1 1;
100  
101 __END__
102  
103 =pod
104  
105 =encoding UTF-8
106  
107 =head1 NAME
108  
109 Bot::Cobalt::Plugin::URLTitle - Bot::Cobalt plugin for printing the title of a URL
110  
111 =head1 VERSION
112  
113 version 0.002
114  
115 =head1 SYNOPSIS
116  
117 ## In plugins.conf
118 URLTitle:
119 Module: Bot::Cobalt::Plugin::URLTitle
120 Config: plugins/twitter.conf # optional
121  
122 ## In plugins/twitter.conf
123 ---
124 consumer_key: <twitter consumer key>
125 consumer_secret: <twitter consumer secret>
126 access_token: <twitter access token>
127 access_token_secret: <twitter access token secret>
128  
129 =head1 DESCRIPTION
130  
131 A L<Bot::Cobalt> plugin.
132  
133 This plugin retrieves the title of any URL in a message using
134 L<URI::Title> and prints the message to the channel.
135  
136 It has optional support for links of tweets via L<Net::Twitter> in which it
137 will print the contents of the tweet rather than the title.
138  
139 #mychannel> https://twitter.com/twitter/status/145344012
140 < mybot> Twitter (@twitter) - working on iphones via 'hahlo' and 'pocket tweets' - fun!
141  
142 =head1 AUTHOR
143  
144 Scott Miller <scott.j.miller@gmail.com>
145  
146 =head1 COPYRIGHT AND LICENSE
147  
148 This software is copyright (c) 2014 by Scott Miller.
149  
150 This is free software; you can redistribute it and/or modify it under
151 the same terms as the Perl 5 programming language system itself.
152  
153 =cut
↑ Summary