Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

XChat RSS script

by OeufMayo (Curate)
on Jan 12, 2002 at 22:54 UTC ( #138310=sourcecode: print w/replies, xml ) Need Help??
Category: IRC
Author/Contact Info Briac Pilpré
Description:

This XChat command grab, parse and print RSS newsfeed from sites defined in a configuration file

Since the configuration file is read each time the command is called, you can add/remove sites without having to reload the script

Usage in XChat:

/rss channel count

'channel' is the name of the RSS channel you want to see, and 'count' is the number of item to display (default 10)

#!/usr/bin/perl -w
use strict;
use LWP::Simple;
use XML::Parser;
use vars qw(%RSS $newsmax %inside $config);

$config = "/home/briac/.xchat/getRSS.conf";

IRC::print("** Loading RSS Alert **\n");
IRC::add_command_handler( "RSS", "rss_command_handler" );

sub rss_command_handler {
    my ( $feed, $count ) = split ( /\s+/, shift () );

    $newsmax = $count || 10;

    do $config;

    # Error checking
    if ( !%RSS ) {
        IRC::print("** Error reading config file\n");
        return 1;
    }
    if ( !$feed ) {
        IRC::print("usage: /rss channel [count]\n");
        IRC::print( "Available channels: " . join (', ', keys %RSS) . 
+"\n" );
        return 1;
    }
    if ( !$RSS{lc($feed)} ) {
        IRC::print("Channel '$feed' unknown.\n");
        IRC::print( "Available channels: " . join (', ', keys %RSS) . 
+"\n" );
        return 1;
    }

    my $xml = get( $RSS{lc($feed)} );
    if ( !$xml ) {
        IRC::print("Can't fetch the RSS\n");
        return 1;
    }

    my $parser = XML::Parser->new( Style => 'Stream' );
    $parser->parse($xml);

    IRC::print("---\n\n");
    return 1;
}

sub StartTag {
    my $elt = $_[1];
    $newsmax-- if $elt eq 'item';
    $inside{$elt}++;
}

sub EndTag {
    my $elt = $_[1];
    IRC::print("\n") if $elt eq 'channel';
    $inside{$elt}--;
}

sub Text {
    return unless /\S/;
    return unless $newsmax >= 0;

    # Channel infos
    IRC::print("> $_")  if ( $inside{channel} && $inside{title} );
    IRC::print(" $_")   if ( $inside{channel} && $inside{description} 
+);
    IRC::print(" ($_)") if ( $inside{channel} && $inside{link} );

    # News Items
    IRC::print("* $_\n")   if ( $inside{item} && $inside{title} );
    IRC::print("    $_\n") if ( $inside{item} && $inside{link} );

}
__END__
# Sample getRSS.conf file
%RSS = (
    slashdot  => 'http://www.slashdot.org/slashdot.rdf',
    useperl   => 'http://use.perl.org/useperl.rdf',
    perlmonks => 'http://www.perlmonks.org/headlines.rdf',
    cpan      => 'http://search.cpan.org/recent.rdf',
    theregister => 'http://www.theregister.co.uk/tonys/slashdot.rdf',
    memepool => 'http://memepool.com/memepool.rss',
);

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://138310]
help
Chatterbox?
[thezip]: Amazing speed and stealth in that one.
[GotToBTru]: You can't tunafish, but some seafood companies can tunafish. More fun with English.
[virtualsue]: a veterinarian told me to cut down on my cat's food. i said, when i do that she just catches more mice
[thezip]: My dogs are also excellent mousers, at least that's what they tell me.
[GotToBTru]: our beagle has dispatched 3 rabbits to the Great Hutch in the Sky since March.
[thezip]: I can always tell when the dogs have been mousing, as there is a concentric ring of mud around the end of their snouts when they do.
[thezip]: As for more physical evidence, that always seems to be missing somehow
[Rabbi Bob]: We have a Jack Russell: off the leash she is a torpedo against mice, rats, chipmunks and squirrels
[Rabbi Bob]: Doesn't eat them, just snaps, kills, drops and moves on.

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (15)
As of 2017-11-17 19:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (272 votes). Check out past polls.

    Notices?