| 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.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 Mojo::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use HTML::Entities qw(decode_entities); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use List::AllUtils qw(first); | ||||||||
|
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 | ▶ | sub new { bless {}, shift } | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 17 | 0 | ▶ | sub ua { shift->{useragent} } | ||||||||
|
Subroutine
ua: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 18 | |||||||||||
| 19 | 0 | ▶ | sub Cobalt_register { | ||||||||
|
Subroutine
Cobalt_register: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 20 | 0 | my $self = shift; | |||||||||
| 21 | 0 | my $core = shift; | |||||||||
| 22 | 0 | my $conf = $core->get_plugin_cfg($self); | |||||||||
| 23 | |||||||||||
| 24 | 0 | $self->{useragent} = Mojo::UserAgent->new; | |||||||||
| 25 | |||||||||||
| 26 | 0 | register( $self, 'SERVER', 'public_msg' ); | |||||||||
| 27 | 0 | logger->info("Registered"); | |||||||||
| 28 | |||||||||||
| 29 | 0 | return PLUGIN_EAT_NONE; | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | 0 | ▶ | sub Cobalt_unregister { | ||||||||
|
Subroutine
Cobalt_unregister: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 33 | 0 | my $self = shift; | |||||||||
| 34 | 0 | my $core = shift; | |||||||||
| 35 | |||||||||||
| 36 | 0 | logger->info("Unregistered"); | |||||||||
| 37 | |||||||||||
| 38 | 0 | return PLUGIN_EAT_NONE; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 0 | ▶ | sub Bot_public_msg { | ||||||||
|
Subroutine
Bot_public_msg: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 42 | 0 | my $self = shift; | |||||||||
| 43 | 0 | my $core = shift; | |||||||||
| 44 | 0 | my $msg = ${ shift() }; | |||||||||
| 45 | |||||||||||
| 46 | 0 | my $context = $msg->context; | |||||||||
| 47 | 0 | my $channel = $msg->target; | |||||||||
| 48 | |||||||||||
| 49 | 0 | foreach my $uri ( list_uris($msg->message) ) { | |||||||||
| 50 | 0 | ▶ | next if not $uri; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | |||||||||||
| 52 | 0 | ▶ | my $text = $self->ua->get($uri)->result->dom->at('title')->text or next; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | |||||||||||
| 54 | 0 | my @split = split /\n/, unidecode($text); # Remove unicode characters and split on newlines | |||||||||
| 55 | 0 | ▶ | my $title = first { /\S/ } @split; # Grab first element with non-space characters | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 56 | |||||||||||
| 57 | # Remove leading and trailing whitespace | ||||||||||
| 58 | 0 | $title =~ s/^\s+(.+)/$1/; | |||||||||
| 59 | 0 | $title =~ s/(.+)\s+$/$1/; | |||||||||
| 60 | |||||||||||
| 61 | 0 | my $resp = sprintf('[ %s ]', $title); | |||||||||
| 62 | 0 | broadcast( 'message', $context, $channel, $resp ); | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 0 | return PLUGIN_EAT_NONE; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 1 | 1; | |||||||||
| 69 | |||||||||||
| 70 | __END__ | ||||||||||