blib/lib/Bot/IRC/X/Feeds.pm

filter: Statementstmt 75.0% 6 / 8 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 81.8% 9 / 11 CC 1 SCAR 0.9
CC
1
Cov
75%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Bot::IRC::X::Feeds;
2 # ABSTRACT: Bot::IRC plugin to watch and notify on changes in RSS feeds
3  
4 2 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use exact;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 0 ▶use XML::RSS;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use LWP::UserAgent;
9 use LWP::Protocol::https;
10 use Date::Parse 'str2time';
11 use WWW::Shorten qw( TinyURL makeashorterlink );
12  
13 our $VERSION = '1.10'; # VERSION
14  
15 sub init {
16 my ($bot) = @_;
17 $bot->load('Store');
18 $bot->load('Join');
19  
20 $bot->hook(
21 {
22 to_me => 1,
23 text => qr/
24 ^feed[s]?
25 \s+(?<action>add|list|remove)
26 (?:
27 \s+(?<url>\S+)
28 (?:
29 \s+(?<forums>\S+)
30 )?
31 )?
32 /x,
33 },
34 sub {
35 my ( $bot, $in, $m ) = @_;
36 my @urls = @{ $bot->store->get('urls') || [] };
37  
38 if ( $m->{action} eq 'add' ) {
39 my $url = lc( $m->{url} );
40 if ( grep { $_->{url} eq $url } @urls ) {
41 $bot->reply_to(q{I'm already tracking that feed.});
42 }
43 else {
44 push( @urls, {
45 url => $url,
46 forums => [ split( ',', lc $m->{forums} ) ],
47 } );
48 $bot->reply_to(q{OK. I'll report changes from the feed you provided.});
49 $bot->store->set( 'urls' => \@urls );
50 }
51 }
52 elsif ( $m->{action} eq 'list' ) {
53 if (@urls) {
54 $bot->reply($_) for ( map {
55 ( @{ $_->{forums} } )
56 ? $_->{url} . ' (' . join( ', ', @{ $_->{forums} } ) . ')'
57 : $_->{url}
58 } @urls );
59 }
60 else {
61 $bot->reply_to(q{I'm currently not tracking any feeds.});
62 }
63 }
64 elsif ( $m->{action} eq 'remove' ) {
65 my $url = lc( $m->{url} );
66 if ( $url eq 'all' ) {
67 $bot->reply_to(q{OK. I'll stop reporting on all the feeds I've been tracking.});
68 $bot->store->set( 'urls' => [] );
69 }
70 elsif ( grep { $_->{url} eq $url } @urls ) {
71 $bot->reply_to(q{OK. I'll stop reporting on the feed you provided.});
72 $bot->store->set( 'urls' => [ grep { $_->{url} ne $url } @urls ] );
73 }
74 else {
75 $bot->reply_to(qq{I wasn't able to find the feed you specified. ($url)});
76 }
77 }
78  
79 return 1;
80 },
81 );
82  
83 my $ua = LWP::UserAgent->new;
84 my $rss = XML::RSS->new;
85 my $interval = ( $bot->vars->{interval} || 10 ) * 60;
86 my $max_per = $bot->vars->{max_per} || 5;
87  
88 $bot->tick(
89 $interval,
90 sub {
91 my ($bot) = @_;
92 my $now = time;
93 my $channels = $bot->channels;
94  
95 for my $url ( @{ $bot->store->get('urls') || [] } ) {
96 my $res = $ua->get( $url->{url} );
97 next unless ( $res->is_success );
98  
99 eval {
100 $rss->parse( $res->decoded_content );
101 };
102 if ($@) {
103 warn $@;
104 next;
105 }
106  
107 my $printed = 0;
108 for my $item ( @{ $rss->{items} } ) {
109 my $key = join( '|',
110 $url->{url},
111 $item->{title},
112 $item->{link},
113 );
114 next if ( $bot->store->get($key) );
115 $bot->store->set( $key => $now );
116  
117 unless ( $printed++ >= $max_per ) {
118 my $msg =
119 'Feed: ' . $rss->channel('title') .
120 ' [ ' . $item->{title} . ' ]' .
121 ' (' . makeashorterlink( $item->{link} ) . ')';
122 $msg .= ' -- ' . $item->{comments} if ( $item->{comments} );
123  
124 $bot->msg( $_, $msg ) for ( ( @{ $url->{forums} } ) ? @{ $url->{forums} } : @$channels );
125 }
126 }
127 }
128 },
129 );
130  
131 $bot->helps( feeds =>
132 'Watch and notify on changes in RSS feeds. ' .
133 'Usage: bot feed add URL [FORUMS], bot feed list, bot feed remove URL. ' .
134 'See also: https://metacpan.org/pod/Bot::IRC::X::Feeds'
135 );
136 }
137  
138 1;
139  
140 __END__
↑ Summary