#!/usr/bin/perl -w use Mac::AppleScript qw(RunAppleScript); use POE; use POE::Component::Client::HTTP; use URI::URL; use CGI qw(a); use Cache::FileCache; use HTTP::Request::Common; use Log::Log4perl qw(:easy); use XML::Simple; our $BASE_URL = "http://perlmonks.org/?node_id="; our $FETCH_URL = "${BASE_URL}30175"; $FROM_ACCOUNT = "m0nkb0t"; $TO_ACCOUNT = "m0nk22"; Log::Log4perl->easy_init({level=>$DEBUG,file=>">>apl.log"}); our $cache = new Cache::FileCache({ namespace => "pm2yim", }); DEBUG "setting up pm2yim 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 ..."; $poe_kernel->run(); sub pm_update { my($html_text) = @_; if(my @nws = latest_news($html_text)) { for(@nws) { INFO "Sending '$_' to IM"; im_update( $_ ); } } } sub im_update { my $scr; my $msg = shift; DEBUG "Attempting to update via IM"; ($scr = <<" EOS") =~ s/^\s+//gm; set fromAcct to "$FROM_ACCOUNT" set toAcct to "$TO_ACCOUNT" tell application "Adium" repeat with acc in accounts if display name of acc is fromAcct then connect acc repeat with ctc in contacts of acc if display name of ctc is toAcct then send ctc message "$msg" on account acc end if end repeat disconnect acc end if end repeat end tell EOS RunAppleScript($scr) or DEBUG "couldn't run applescript"; } sub latest_news { my($xml_string) = @_; my $max_node; my $saved = $cache->get("max-node"); $saved = 0 unless defined $saved; my @aimtext = (); my $data = XMLin($xml_string); for my $node (@{ $data->{NODE} }) { next unless $node->{nodetype}; next unless $node->{nodetype} =~ /perlquestion|monkdiscussion/; $node->{content} =~ s/\n//g; if($node->{node_id} > $saved) { INFO "New node $node->{content} ($node->{node_id})"; unshift @aimtext, "$node->{content} " . "${BASE_URL}$node->{node_id}"; } $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 @aimtext; } #### $/ = q#(\w)# ; sub sig { print scalar reverse join ' ', @_ } sig map { s$\$/\$/$\$2\$1$g && $_ } split( ' ', ",erckha rlPe erthnoa stJu" );