X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=src%2FModules%2FRSSFeeds.pl;h=2eb349afddaf8cf65c4ffd813049ff4650fdfdb8;hb=8addf484a5b78fe043ac663129be6c05e075f260;hp=035ea981a3f86bad9ef88f2ee2b6b260f7e2f15f;hpb=ccc02c22796957aa2f2951c1146d58645261ad0f;p=infobot.git diff --git a/src/Modules/RSSFeeds.pl b/src/Modules/RSSFeeds.pl index 035ea98..2eb349a 100644 --- a/src/Modules/RSSFeeds.pl +++ b/src/Modules/RSSFeeds.pl @@ -13,149 +13,152 @@ use XML::Feed; use vars qw(%channels %param $dbh $who $chan); sub getCacheEntry { - my ( $file, $url ) = @_; - my @entries; + my ( $file, $url ) = @_; + my @entries; - &::DEBUG("rssFeed: Searching cache for $url"); + &::DEBUG("rssFeed: Searching cache for $url"); - open CACHE, "<$file" or return; - binmode( CACHE, ":encoding(UTF-8)" ); + open CACHE, "<$file" or return; + binmode( CACHE, ":encoding(UTF-8)" ); - while () { - next unless /^$url:/; - chop; - s/^$url:(.*)/$1/; - push @entries, $_; - } - close CACHE; + while () { + next unless /^$url:/; + chop; + s/^$url:(.*)/$1/; + push @entries, $_; + } + close CACHE; - return @entries; + return @entries; } sub saveCache { - my ( $file, $url, @entries ) = @_; + my ( $file, $url, @entries ) = @_; - open IN, "<$file" or return; - open OUT, ">$file.tmp" or return; + open IN, "<$file" or return; + open OUT, ">$file.tmp" or return; - binmode( IN, ":encoding(UTF-8)" ); - binmode( OUT, ":encoding(UTF-8)" ); + binmode( IN, ":encoding(UTF-8)" ); + binmode( OUT, ":encoding(UTF-8)" ); - # copy all but old ones - while () { - next if /^$url:/; - print OUT $_; - } + # copy all but old ones + while () { + next if /^$url:/; + print OUT $_; + } - # append new ones - foreach (@entries) { - print OUT "$url:$_\n"; - } + # append new ones + foreach (@entries) { + print OUT "$url:$_\n"; + } - close IN; - close OUT; + close IN; + close OUT; - rename "$file.tmp", "$file"; + rename "$file.tmp", "$file"; } sub createCache { - my $file = shift; + my $file = shift; - &::status("rssFeed: Creating cache in $file"); + &::status("rssFeed: Creating cache in $file"); - open CACHE, ">$file" or return; - close CACHE; + open CACHE, ">$file" or return; + close CACHE; } sub getFeed { - my ( $cacheFile, $chan, $rssFeedUrl ) = @_; + my ( $cacheFile, $chan, $rssFeedUrl ) = @_; - &::DEBUG("rssFeed: URL: $rssFeedUrl"); + &::DEBUG("rssFeed: URL: $rssFeedUrl"); - my $feed = XML::Feed->parse( URI->new($rssFeedUrl) ) - or return XML::Feed->errstr; + my $feed = XML::Feed->parse( URI->new($rssFeedUrl) ) + or return XML::Feed->errstr; - my $curTitle = $feed->title; - &::DEBUG("rssFeed: TITLE: $curTitle"); - my @curEntries; + my $curTitle = $feed->title; + &::DEBUG("rssFeed: TITLE: $curTitle"); + my @curEntries; - for my $entry ( $feed->entries ) { - &::DEBUG( "rssFeed: ENTRY: " . $entry->title ); - push @curEntries, $entry->title; - } + for my $entry ( $feed->entries ) { + &::DEBUG( "rssFeed: ENTRY: " . $entry->title ); + push @curEntries, $entry->title; + } - # Create the cache if it doesnt exist - &createCache($cacheFile) - if ( !-e $cacheFile ); + # Create the cache if it doesnt exist + &createCache($cacheFile) + if ( !-e $cacheFile ); - my @oldEntries = &getCacheEntry( $cacheFile, $rssFeedUrl ); - my @newEntries; - foreach (@curEntries) { - &::DEBUG("rssFeed: CACHE: $_"); - last if ( $_ eq $oldEntries[0] ); - push @newEntries, $_; - } + my @oldEntries = &getCacheEntry( $cacheFile, $rssFeedUrl ); + my @newEntries; + foreach (@curEntries) { + &::DEBUG("rssFeed: CACHE: $_"); + last if ( $_ eq $oldEntries[0] ); + push @newEntries, $_; + } - if ( scalar @newEntries == 0 ) { # if there wasn't anything new - return "rssFeed: No new headlines for $curTitle."; - } + if ( scalar @newEntries == 0 ) { # if there wasn't anything new + return "rssFeed: No new headlines for $curTitle."; + } - # save to hash again - &saveCache( $cacheFile, $rssFeedUrl, @curEntries ) - or return "rssFeed: Could not save cache!"; + # save to hash again + &saveCache( $cacheFile, $rssFeedUrl, @curEntries ) + or return "rssFeed: Could not save cache!"; - my $reply = &::formListReply( 0, $curTitle, @newEntries ); - &::msg( $chan, $reply ); + my $reply = &::formListReply( 0, $curTitle, @newEntries ); + &::msg( $chan, $reply ); - # "\002<<\002$curTitle\002>>\002 " . join( " \002::\002 ", @newEntries ) ); + # "\002<<\002$curTitle\002>>\002 " . join( " \002::\002 ", @newEntries ) ); - return; + return; } sub RSS { - my ($command) = @_; - my $cacheFile = "$::param{tempDir}/rssFeed.cache"; - my %feeds; - - if ( not $command =~ /^(flush|update)?$/i ) { - &::status("rssFeed: Unknown command: $command"); - return; - } - - if ( $command =~ /^flush$/i ) { - if ( not &::IsFlag("o") ) { - &::status("rssFeed: User $::who tried to flush the cache, but isn't +o!"); - return; - } - unlink $cacheFile if ( -e $cacheFile ); - &::status("rssFeed: Flushing cache."); - &::performStrictReply("$::who: Flushed RSS Feed cache."); - return; - } - - if ( $command =~ /^update$/i ) { - if ( not &::IsFlag("o") ) { - &::status("rssFeed: User $::who tried to manually update feeds, but isn't +o!"); - return; - } - &::status("rssFeed: Manual update of feeds requested by $::who."); - } - - foreach my $chan ( keys %::channels ) { - my $rssFeedUrl = &::getChanConf( 'rssFeedUrl', $chan ); - my @urls = split / /, $rssFeedUrl; - - # Store by url then chan to allow for same url's in multiple channels - foreach (@urls) { $feeds{$chan}{$_} = 1 } - } - - foreach my $chans ( keys %feeds ) { - foreach ( keys %{ $feeds{$chans} } ) { - my $result = &getFeed( $cacheFile, $chans, $_ ); - &::status($result) if $result; - } - } - return; + my ($command) = @_; + my $cacheFile = "$::param{tempDir}/rssFeed.cache"; + my %feeds; + + if ( not $command =~ /^(flush|update)?$/i ) { + &::status("rssFeed: Unknown command: $command"); + return; + } + + if ( $command =~ /^flush$/i ) { + if ( not &::IsFlag("o") ) { + &::status( + "rssFeed: User $::who tried to flush the cache, but isn't +o!"); + return; + } + unlink $cacheFile if ( -e $cacheFile ); + &::status("rssFeed: Flushing cache."); + &::performStrictReply("$::who: Flushed RSS Feed cache."); + return; + } + + if ( $command =~ /^update$/i ) { + if ( not &::IsFlag("o") ) { + &::status( +"rssFeed: User $::who tried to manually update feeds, but isn't +o!" + ); + return; + } + &::status("rssFeed: Manual update of feeds requested by $::who."); + } + + foreach my $chan ( keys %::channels ) { + my $rssFeedUrl = &::getChanConf( 'rssFeedUrl', $chan ); + my @urls = split / /, $rssFeedUrl; + + # Store by url then chan to allow for same url's in multiple channels + foreach (@urls) { $feeds{$chan}{$_} = 1 } + } + + foreach my $chans ( keys %feeds ) { + foreach ( keys %{ $feeds{$chans} } ) { + my $result = &getFeed( $cacheFile, $chans, $_ ); + &::status($result) if $result; + } + } + return; } 1;