#!/usr/bin/perl -w use FindBin; use lib "$FindBin::Bin/../lib"; use PerlMonks::StatsWhore; use POSIX qw(strftime); my $mbox_node = '15848'; my $DBFILE = "$FindBin::Bin/../var/monks.dat"; my $MSGFILE = "$FindBin::Bin/../var/monk-msg.dat"; my $LOGFILE = "$FindBin::Bin/../var/monks.log"; my $verbose = 0; foreach (@ARGV) { $verbose++ if $_ eq '-v' }; my $sw = PerlMonks::StatsWhore->new(user => 'sfink', password => 'p455wyrd') or die "failed to create whore\n"; open(DB, $DBFILE) or die "open $DBFILE: $!"; my %info; # { nodeid => } while() { next if /^\s*\#/; chomp; my ($title) = /\s*\"(.*)\"$/; s/\s*\".*\"$//; my ($nodeid, $create_day, $create_time, $current, $lastcheck, @events) = split(/ /); my $create = "$create_day $create_time"; $info{$nodeid} = [ $current, $create, $title, $lastcheck, \@events ]; } close DB; open(LOG, ">>$LOGFILE") or die "append $LOGFILE: $!"; my $w = $sw->writeups_ref(); my $NOW = time(); foreach my $nodeid (keys %$w) { my $info = $info{$nodeid}; my $d = $w->{$nodeid}; if ($info) { my ($current, $create, undef, $lastcheck, $events) = @$info; next if $current eq 'D'; # Deleted node next if $current == $d->{rep}; my $curdate = strftime("%Y-%m-%d %H:%M:%S", localtime); my $diff = $d->{rep} - $current; $diff = "+$diff" if $diff > 0; print "Update! $diff rep for \"$d->{title}\" created $create\n"; print LOG "$curdate M $nodeid $diff \"$d->{title}\"\n"; push @$events, "$lastcheck:".-$diff; $info->[0] = $d->{rep}; } else { # New node! $info{$nodeid} = [ $d->{rep}, $d->{date}, $d->{title}, $NOW, [] ]; print LOG "$d->{date} C $nodeid \"$d->{title}\"\n"; print "New node! rep $d->{rep} \"$d->{title}\"\n"; } } # Find deleted nodes my %had; @had{keys %info} = (); delete $had{$_} foreach (keys %$w); foreach my $nodeid (keys %had) { my $info = $info{$nodeid}; next if $info->[0] eq 'D'; print "Deleted! rep $info->[0] node \"$info->[2]\"\n"; print LOG "$NOW R $nodeid $info->[0] \"$info->[2]\"\n"; $info->[0] = 'D'; } close LOG; open(DB, ">$DBFILE") or die "write $DBFILE: $!"; print DB "# NODEID CREATED CURRENT-REP LASTCHECK EVENTS... TITLE\n"; my @nodes = sort { $info{$b}->[1] cmp $info{$a}->[1] } keys %info; foreach my $nodeid (@nodes) { my $info = $info{$nodeid}; my $line = "$nodeid $info->[1] $info->[0] $NOW "; $line .= join(" ", @{ $info->[4] }); # Events $line .= " " unless $line =~ / $/; $line .= '"' . $info->[2] . '"'; $line .= "\n"; print DB $line; } close DB; # Look for new messages my %old_msgs; if (-r $MSGFILE) { open(MSG, $MSGFILE) or die "open $MSGFILE: $!"; while() { chomp; my ($id, $message) = split(/\s/, $_, 2); $old_msgs{$id} = $message; } close MSG; } my %messages; my $perlmonks = new PerlMonks::StatsWhore::XML(user => $sw->{user}, password => $sw->{password}); $perlmonks->set_query(node => $mbox_node); my $inbox = $perlmonks->fetch(); print $inbox if $verbose; while ($inbox =~ m!\]*message_id=.(\d+).[^>]*\>\s*(.*?)!sg) { my ($id, $message) = ($1, $2); next if defined($messages{$id} = delete $old_msgs{$id}); print " --- New message: $id ---\n\n$message\n\n"; $messages{$id} = $message; } while (my ($id, $message) = each %old_msgs) { print " --- Deleted message: $id ---\n\n$message\n\n"; } open(MSG, ">$MSGFILE") or die "create $MSGFILE: $!"; while (my ($id, $message) = each %messages) { print MSG "$id $message\n"; } close MSG;