#!/usr/bin/perl ########################################### # pm2irc - Scrape perlmonks.com and post # new questions to #pm2irc on # irc.freenode.net ########################################### use strict; use Bot::BasicBot; use HTML::TreeBuilder; use URI::URL; use CGI qw(a); use Cache::FileCache; use HTTP::Request::Common; use Log::Log4perl 1.05 qw(:easy); use POE; use POE::Component::Client::HTTP; use XML::Simple; use Pod::Usage; our $CHANNEL_ALL = "#pm2irc"; our $CHANNEL_NEW = "#pm2ircnew"; our $FETCH_INTERVAL = 300; our $BASE_URL = "http://perlmonks.com/?node_id="; # RSS feed our $FETCH_URL = "${BASE_URL}30175"; our($USER, $PASSWD) = @ARGV; pod2usage("Argument missing") unless defined $PASSWD; Log::Log4perl->init(\q{ log4perl.logger = DEBUG, Logfile log4perl.appender.Logfile = Log::Log4perl::Appender::File log4perl.appender.Logfile.filename = /tmp/pm2irc.log log4perl.appender.Logfile.mode = append log4perl.appender.Logfile.recreate = 1 log4perl.appender.Logfile.layout = Log::Log4perl::Layout::PatternLayout log4perl.appender.Logfile.layout.ConversionPattern = %d %F{1} %L> %m %n }); our $cache = new Cache::FileCache({ namespace => "pm2irc", }); my $Bot = PerlmonksBot->new( server => 'irc.freenode.net', channels => [$CHANNEL_ALL, $CHANNEL_NEW], nick => $USER, password => $PASSWD, ); DEBUG "Setting up pm2irc POE components"; POE::Component::Client::HTTP->spawn( Alias => "ua", Timeout => 60, ); POE::Session->create( inline_states => { _start => sub { # Wait 20 secs before the first post $poe_kernel->delay('http_start', 20); }, http_start => sub { DEBUG "Fetching url $FETCH_URL"; $poe_kernel->post("ua", "request", "http_ready", GET $FETCH_URL); $poe_kernel->delay('http_start', $FETCH_INTERVAL); }, http_ready => sub { DEBUG "http_ready $FETCH_URL"; my $resp= $_[ARG1]->[0]; if($resp->is_success()) { pm_update($resp->content()); } else { ERROR "Can't fetch $FETCH_URL: ", $resp->message(); } }, } ); DEBUG "The dance begins ..."; $Bot->run(); ########################################### sub pm_update { ########################################### my($html_text) = @_; my $rexall = qr/perlquestion|monkdiscussion|note/; my $rexnew = qr/perlquestion/; if(my @nws = latest_news($html_text, $rexall)) { for(@nws) { my($text, $type) = @$_; if($type =~ /$rexnew/) { INFO "Sending '$text' to channel $CHANNEL_NEW " . "(message type $type)"; $Bot->say(channel => $CHANNEL_NEW, body => "$text", ); } INFO "Sending '$text' to channel $CHANNEL_ALL"; $Bot->say(channel => $CHANNEL_ALL, body => "$text", ); } } } ########################################### sub latest_news { ########################################### my($xml_string, $types) = @_; my $max_node; my $saved = $cache->get("max-node"); $saved = 0 unless defined $saved; my @newest = (); my $data = XMLin($xml_string); for my $node (@{ $data->{NODE} }) { next unless $node->{nodetype}; next unless $node->{nodetype} =~ /$types/; $node->{content} =~ s/\n//g; if($node->{node_id} > $saved) { INFO "New node $node->{content} ($node->{node_id})"; unshift @newest, ["$node->{authortitle}: $node->{content} " . "${BASE_URL}$node->{node_id}", $node->{nodetype}]; } $max_node = $node->{node_id} if !defined $max_node or $max_node < $node->{node_id}; } $cache->set("max-node", $max_node) if $saved < $max_node; return @newest; } ########################################### package PerlmonksBot; ########################################### use base qw( Bot::BasicBot ); use Log::Log4perl qw(:easy); ########################################### sub log { ########################################### my($self, @msgs) = @_; DEBUG @msgs; } ########################################### sub chanjoin { ########################################### my($self, $mesg) = @_; return undef if $mesg->{who} eq $main::USER; $self->say(channel => "msg", who => $mesg->{who}, body => <{who}. Currently, we're offering two channels, $CHANNEL_ALL and $CHANNEL_NEW. While $CHANNEL_ALL posts all new questions and responses on Perlmonks, $CHANNEL_NEW just posts new questions. Have fun! EOT } __END__ =head1 NAME pm2irc - Fetch perlmonks.com questions and funnel them to IRC =head1 DOWNLOAD _SRC_HERE_ =head1 SYNOPSIS pm2irc [pm2irc|user] passwd =head1 DESCRIPTION C fetches the perlmonks.com RSS feed, extracts the latest questions and funnels them to several IRC channels. Channel #pm2irc on irc.freenode.net receives all new questions and their responses. Channel #pm2ircnew on irc.freenode.net receives just the new questions. B =head1 LEGALESE Copyright 2006 by Mike Schilli, all rights reserved. This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself. =head1 AUTHOR 2006, Mike Schilli