| 1 | package Bot::Cobalt::Plugin::RSS; | ||
| 2 | our $VERSION = '0.002'; | ||
| 3 | |||
| 4 | 1 | ▶ | use 5.12.1; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use Bot::Cobalt; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Bot::Cobalt::Common; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 1 | ▶ | use File::Spec; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 0 | ▶ | use XML::RSS::Feed; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | |||
| 13 | sub new { | ||
| 14 | my $class = shift; | ||
| 15 | |||
| 16 | my $self = { | ||
| 17 | MSGTIMERS => {}, | ||
| 18 | HEAP => { | ||
| 19 | POOL => {}, | ||
| 20 | ANN => {}, | ||
| 21 | FEEDS => {}, | ||
| 22 | }, | ||
| 23 | }; | ||
| 24 | |||
| 25 | bless $self, $class; | ||
| 26 | return $self | ||
| 27 | } | ||
| 28 | |||
| 29 | sub pending { $_[0]->{HEAP}->{POOL} } | ||
| 30 | |||
| 31 | sub announce { | ||
| 32 | ## announce($name, $context, $channel) | ||
| 33 | ## add new announce for feed/context/channel | ||
| 34 | my ($self, $feedname, $context, $channel) = @_; | ||
| 35 | return unless $feedname and $context and $channel; | ||
| 36 | |||
| 37 | my $a_heap = $self->{HEAP}->{ANN}; | ||
| 38 | |||
| 39 | push(@{$a_heap->{$feedname}->{$context}}, $channel); | ||
| 40 | |||
| 41 | return $channel | ||
| 42 | } | ||
| 43 | |||
| 44 | sub get_announce { | ||
| 45 | ## get_announce($name [, $context ]) | ||
| 46 | ## get arrayref of channels for this feed/context | ||
| 47 | my ($self, $feedname, $context) = @_; | ||
| 48 | return unless $feedname; | ||
| 49 | |||
| 50 | my $a_heap = $self->{HEAP}->{ANN}; | ||
| 51 | |||
| 52 | return $a_heap->{$feedname}->{$context} if $context; | ||
| 53 | return $a_heap->{$feedname} | ||
| 54 | } | ||
| 55 | |||
| 56 | sub track { | ||
| 57 | ## track($name, $url, $delay) | ||
| 58 | ## add new feed to {FEEDS} | ||
| 59 | my ($self, $feedname, $url, $delay, $spacing) = @_; | ||
| 60 | return unless $feedname and $url; | ||
| 61 | |||
| 62 | $delay = 120 unless $delay; | ||
| 63 | $spacing = 5 unless $spacing; | ||
| 64 | |||
| 65 | my $p_heap = $self->{HEAP}->{FEEDS}; | ||
| 66 | |||
| 67 | return if exists $p_heap->{$feedname}; | ||
| 68 | |||
| 69 | $p_heap->{$feedname} = { | ||
| 70 | hasrun => 0, | ||
| 71 | url => $url, | ||
| 72 | delay => $delay, | ||
| 73 | space => $spacing, | ||
| 74 | }; | ||
| 75 | |||
| 76 | ## Can create our XML::RSS:Feed now (requires proper hash above): | ||
| 77 | $p_heap->{obj} = $self->_create_feed($feedname); | ||
| 78 | |||
| 79 | ## add to timer pool | ||
| 80 | my $pool = $self->pending; | ||
| 81 | $pool->{$feedname} = { | ||
| 82 | LastRun => 0, | ||
| 83 | }; | ||
| 84 | |||
| 85 | return $feedname | ||
| 86 | } | ||
| 87 | |||
| 88 | sub list_feed_names { | ||
| 89 | ## list_feed_names() | ||
| 90 | ## returns list of {FEEDS} keys | ||
| 91 | my ($self) = @_; | ||
| 92 | |||
| 93 | my $p_heap = $self->{HEAP}->{FEEDS}; | ||
| 94 | return keys(%$p_heap) | ||
| 95 | } | ||
| 96 | |||
| 97 | sub get_feed_meta { | ||
| 98 | ## get_feed_meta($name) | ||
| 99 | ## returns {FEEDS} element w/ url and delay | ||
| 100 | my ($self, $feedname) = @_; | ||
| 101 | return unless $feedname; | ||
| 102 | |||
| 103 | return $self->{HEAP}->{FEEDS}->{$feedname} | ||
| 104 | } | ||
| 105 | |||
| 106 | sub Cobalt_register { | ||
| 107 | my ($self, $core) = splice @_, 0, 2; | ||
| 108 | |||
| 109 | my $pcfg = $core->get_plugin_cfg($self); | ||
| 110 | my $feeds = $pcfg->{Feeds}; | ||
| 111 | |||
| 112 | if ($feeds && ref $feeds eq 'HASH' && keys %$feeds) { | ||
| 113 | FEED: for my $feedname (keys %$feeds) { | ||
| 114 | my $thisfeed = $feeds->{$feedname}; | ||
| 115 | |||
| 116 | my $url = $thisfeed->{URL}; | ||
| 117 | unless ($url) { | ||
| 118 | logger->warn( | ||
| 119 | "Could not add feed $feedname: missing URL directive", | ||
| 120 | ); | ||
| 121 | next FEED | ||
| 122 | } | ||
| 123 | |||
| 124 | my $annto = $thisfeed->{AnnounceTo}; | ||
| 125 | unless ($annto && ref $annto eq 'HASH') { | ||
| 126 | logger->warn( | ||
| 127 | "Could not add feed $feedname: invalid AnnounceTo directive", | ||
| 128 | ); | ||
| 129 | next FEED | ||
| 130 | } | ||
| 131 | |||
| 132 | my $delay = $thisfeed->{Delay} || 120; | ||
| 133 | my $spacing = $thisfeed->{Spaced} || 5; | ||
| 134 | $self->track($feedname, $url, $delay, $spacing) | ||
| 135 | or logger->warn("Could not add $feedname: track() failed"); | ||
| 136 | |||
| 137 | CONTEXT: for my $context (keys %$annto) { | ||
| 138 | my $thiscont = $annto->{$context}; | ||
| 139 | unless (ref $thiscont eq 'ARRAY') { | ||
| 140 | logger->warn( | ||
| 141 | "Configured AnnounceTo directive for $context not a list.", | ||
| 142 | "Check your configuration." | ||
| 143 | ); | ||
| 144 | next CONTEXT | ||
| 145 | } | ||
| 146 | |||
| 147 | $self->announce($feedname, $context, $_) | ||
| 148 | for @$thiscont; | ||
| 149 | } ## CONTEXT | ||
| 150 | |||
| 151 | } ## FEED | ||
| 152 | } else { | ||
| 153 | logger->warn( | ||
| 154 | "There are no RSS feeds configured; doing nothing.", | ||
| 155 | "You may want to inspect this plugin's Config: file.", | ||
| 156 | "See perldoc Bot::Cobalt::Plugin::RSS", | ||
| 157 | ); | ||
| 158 | } | ||
| 159 | |||
| 160 | register( $self, 'SERVER', | ||
| 161 | [ | ||
| 162 | 'executed_timer', | ||
| 163 | 'rssplug_check_timer_pool', | ||
| 164 | 'rssplug_got_resp', | ||
| 165 | ], | ||
| 166 | ); | ||
| 167 | |||
| 168 | $self->{POOLTIMERID} = $core->timer_set( 6, | ||
| 169 | { Event => 'rssplug_check_timer_pool' } | ||
| 170 | ); | ||
| 171 | |||
| 172 | my $count = $self->list_feed_names; | ||
| 173 | logger->info("Loaded - $VERSION - watching $count feeds"); | ||
| 174 | |||
| 175 | return PLUGIN_EAT_NONE | ||
| 176 | } | ||
| 177 | |||
| 178 | |||
| 179 | sub Cobalt_unregister { | ||
| 180 | my ($self, $core) = splice @_, 0, 2; | ||
| 181 | |||
| 182 | $core->timer_del($self->{POOLTIMERID}); | ||
| 183 | $core->timer_del($_) for keys %{$self->{MSGTIMERS}}; | ||
| 184 | |||
| 185 | delete $self->{HEAP}; | ||
| 186 | |||
| 187 | logger->info("Unloaded"); | ||
| 188 | |||
| 189 | return PLUGIN_EAT_NONE | ||
| 190 | } | ||
| 191 | |||
| 192 | sub Bot_executed_timer { | ||
| 193 | my ($self, $core) = splice @_, 0, 2; | ||
| 194 | my $id = ${ $_[0] }; | ||
| 195 | delete $self->{MSGTIMERS}->{$id}; | ||
| 196 | return PLUGIN_EAT_NONE | ||
| 197 | } | ||
| 198 | |||
| 199 | sub Bot_rssplug_check_timer_pool { | ||
| 200 | my ($self, $core) = splice @_, 0, 2; | ||
| 201 | |||
| 202 | my $pool = $self->pending; | ||
| 203 | |||
| 204 | ## check timestamps on pool (keyed on name) | ||
| 205 | ## if ts is up, execute _request, schedule new | ||
| 206 | for my $feedname (keys %$pool) { | ||
| 207 | my $feedmeta = $self->get_feed_meta($feedname) | ||
| 208 | || return PLUGIN_EAT_NONE; | ||
| 209 | my $thistimer = $pool->{$feedname}; | ||
| 210 | my $lastts = $thistimer->{LastRun} || 0; | ||
| 211 | my $delay = $feedmeta->{delay}; | ||
| 212 | |||
| 213 | if (time - $lastts >= $delay) { | ||
| 214 | $self->_request($feedname); | ||
| 215 | $thistimer->{LastRun} = time; | ||
| 216 | } | ||
| 217 | } | ||
| 218 | |||
| 219 | $self->{POOLTIMERID} = $core->timer_set( 3, | ||
| 220 | { Event => 'rssplug_check_timer_pool' } | ||
| 221 | ); | ||
| 222 | |||
| 223 | return PLUGIN_EAT_NONE | ||
| 224 | } | ||
| 225 | |||
| 226 | sub Bot_rssplug_got_resp { | ||
| 227 | my ($self, $core) = splice @_, 0, 2; | ||
| 228 | my $response = ${ $_[1] }; | ||
| 229 | my $args = ${ $_[2] }; | ||
| 230 | my ($feedname) = @$args; | ||
| 231 | |||
| 232 | if ($response->is_success) { | ||
| 233 | my $feedmeta = $self->get_feed_meta($feedname) | ||
| 234 | || return PLUGIN_EAT_NONE; | ||
| 235 | my $handler = $feedmeta->{obj}; | ||
| 236 | |||
| 237 | local $SIG{__WARN__} = sub {}; | ||
| 238 | if ( eval { $handler->parse($response->content); 1 } ) { | ||
| 239 | $self->_send_announce($feedname, $handler); | ||
| 240 | } | ||
| 241 | } else { | ||
| 242 | logger->warn( | ||
| 243 | "Unsuccessful HTTP request: $feedname: ".$response->status | ||
| 244 | ); | ||
| 245 | return PLUGIN_EAT_NONE | ||
| 246 | } | ||
| 247 | |||
| 248 | return PLUGIN_EAT_NONE | ||
| 249 | } | ||
| 250 | |||
| 251 | sub _send_announce { | ||
| 252 | my ($self, $name, $handler) = @_; | ||
| 253 | |||
| 254 | my $title = $handler->title; | ||
| 255 | |||
| 256 | my $feedmeta = $self->get_feed_meta($name); | ||
| 257 | |||
| 258 | my $a_heap = $self->get_announce($name); | ||
| 259 | |||
| 260 | unless ($feedmeta->{hasrun}) { | ||
| 261 | $feedmeta->{hasrun} = 1; | ||
| 262 | $handler->init_headlines_seen(1); | ||
| 263 | ## for some reason init_headlines_seen sometimes fails ... | ||
| 264 | $handler->late_breaking_news; | ||
| 265 | return | ||
| 266 | } | ||
| 267 | |||
| 268 | my $spacing = $feedmeta->{space}; | ||
| 269 | my $spcount = 0; | ||
| 270 | my @headlines = $handler->late_breaking_news; | ||
| 271 | HEAD: while (my $headline = pop @headlines) { | ||
| 272 | my $this_line = $headline->headline; | ||
| 273 | my $this_url = $headline->url; | ||
| 274 | my $str = "RSS: " | ||
| 275 | .color('bold', $name) | ||
| 276 | .": $this_line ( $this_url )" ; | ||
| 277 | |||
| 278 | CONTEXT: for my $context (keys %$a_heap) { | ||
| 279 | my $irc = irc_object($context) || next CONTEXT; | ||
| 280 | |||
| 281 | for my $channel ( @{ $a_heap->{$context} } ) { | ||
| 282 | my $tid = core()->timer_set( 1 + $spcount, | ||
| 283 | { | ||
| 284 | Type => 'msg', | ||
| 285 | Context => $context, | ||
| 286 | Target => $channel, | ||
| 287 | Text => $str, | ||
| 288 | }, | ||
| 289 | ); | ||
| 290 | $self->{MSGTIMERS}->{$tid} = $name; | ||
| 291 | } | ||
| 292 | |||
| 293 | } ## CONTEXT | ||
| 294 | |||
| 295 | $spcount += $spacing; | ||
| 296 | |||
| 297 | } ## HEAD | ||
| 298 | } | ||
| 299 | |||
| 300 | sub _create_feed { | ||
| 301 | ## _create_feed($name) | ||
| 302 | ## create (and return) new XML::RSS::Feed based on get_feed_meta | ||
| 303 | my ($self, $feedname) = @_; | ||
| 304 | |||
| 305 | my $feedmeta = $self->get_feed_meta($feedname); | ||
| 306 | |||
| 307 | my %feedopts = ( | ||
| 308 | name => $feedname, | ||
| 309 | url => $feedmeta->{url}, | ||
| 310 | delay => $feedmeta->{delay}, | ||
| 311 | init_headlines_seen => 0, | ||
| 312 | |||
| 313 | ## tmpdir caching seems to result in memory leaks: | ||
| 314 | # tmpdir => File::Spec->tmpdir(), | ||
| 315 | ); | ||
| 316 | |||
| 317 | if ( my $rss = XML::RSS::Feed->new(%feedopts) ) { | ||
| 318 | $feedmeta->{obj} = $rss; | ||
| 319 | } else { | ||
| 320 | logger->warn( | ||
| 321 | "Could not create XML::RSS::Feed obj for $feedname" | ||
| 322 | ); | ||
| 323 | } | ||
| 324 | |||
| 325 | return $feedmeta->{obj} | ||
| 326 | } | ||
| 327 | |||
| 328 | sub _request { | ||
| 329 | my ($self, $feedname) = @_; | ||
| 330 | |||
| 331 | my $feedmeta = $self->get_feed_meta($feedname); | ||
| 332 | |||
| 333 | unless (core()->Provided->{www_request}) { | ||
| 334 | logger->warn("You seem to be missing Bot::Cobalt::Plugin::WWW!"); | ||
| 335 | return PLUGIN_EAT_NONE | ||
| 336 | } | ||
| 337 | |||
| 338 | ## send request tagged w/ feedname | ||
| 339 | my $url = $feedmeta->{url}; | ||
| 340 | my $req = HTTP::Request->new( 'GET', $url ); | ||
| 341 | broadcast( 'www_request', | ||
| 342 | $req, | ||
| 343 | 'rssplug_got_resp', | ||
| 344 | [ $feedname ], | ||
| 345 | ); | ||
| 346 | |||
| 347 | return 1 | ||
| 348 | } | ||
| 349 | |||
| 350 | 1; | ||
| 351 | __END__ | ||