Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Check for recent replies to your posts

by jdporter (Chancellor)
on Jan 27, 2003 at 16:55 UTC ( #230274=sourcecode: print w/replies, xml ) Need Help??
Category: PerlMonks Related Scripts
Author/Contact Info jdporter@perlmonk.org (John Douglas Porter)
Description: In response to castaway's idea for a 'My Active Nodes' nodelet, I have written a console-based solution using my PerlMonksTickers module. It is a working example. You can use it as is, or enhance it to meet your particular needs. Run it with no arguments to get a usage statement. It does not require a PerlMonks login.
use strict;
use warnings;
use PerlMonksTickers;
use Getopt::Long;
use Time::Local;
$|++;

# as an optimization, we can avoid looking for replies to nodes
# we posted more than a certain number of days ago.
# to inhibit this, set to 0 or undef.
my $ignore_my_posts_older_than_days;

my $window_in_hours = 1;

my $user;

sub usage
{
  die <<EOF;
@{_}Usage: $0 --user USERNAME [--ignore DAYS] [--window HOURS]
--ignore says to not look for replies to nodes I posted more than
that many days ago.  Default is no limit.
--window says to report only nodes that have been posted within
the past so many hours.  Default is 1.
EOF
}

GetOptions(
  'user=s' => \$user,
  'window=i' => \$window_in_hours,
  'ignore|old=i' => \$ignore_my_posts_older_than_days,
) or usage;
defined $user or usage "--user argument is required.\n";


my $timestamp_of_my_earliest_post = '';
my @root_nodes_of_interest = do {
  my $ar = PerlMonksTickers->user_nodes( foruser => $user ); @$ar };

if ( $ignore_my_posts_older_than_days )
{
  $timestamp_of_my_earliest_post = 
    timestamp_of_hours_ago( $ignore_my_posts_older_than_days * 24 );

  warn "Ignoring my nodes prior to $timestamp_of_my_earliest_post\n";

  @root_nodes_of_interest = PerlMonksTickers->only_new_nodes(
    $timestamp_of_my_earliest_post, @root_nodes_of_interest );

  @root_nodes_of_interest or die
   "Sorry, you haven't posted any nodes in the last $ignore_my_posts_o
+lder_than_days days.\n";
}

@root_nodes_of_interest or die
  "Sorry, you haven't posted any nodes!\n";

my $timestamp_of_earliest_reply = timestamp_of_hours_ago( $window_in_h
+ours );

warn "Showing only replies since $timestamp_of_earliest_reply\n\n";

for my $node ( @root_nodes_of_interest )
{
  my $hr = PerlMonksTickers->thread_nodes( id => $node->{'id'} );
  my %ids;
  @ids{ keys %$hr, map @$_, values %$hr } = ();
  # while ( my($k,$v) = each %$hr ) { @ids{ $k, @$v } = (); } # altern
+ative way.
  delete $ids{ $node->{'id'} }; # the root node is included in the set
+. Ignore it.

  my $ts = $timestamp_of_earliest_reply;
  my @replies = map { PerlMonksTickers->any_node( node_id => $_ ) } ke
+ys %ids;
  @replies = PerlMonksTickers->only_new_nodes( $ts, @replies );
  @replies or next;

  printf( "%s (%s) %s\n",
    $node->{'createtime'},
    $node->{'id'},
    $node->{'title'},
  );
  print "has the following replies:\n";
  for my $reply ( @replies )
  {
    printf( "  %s (%s) <%s> %s\n    %s\n",
      $reply->{'createtime'},
      $reply->{'node_id'},
      $reply->{'author'},
      $reply->{'title'},
    );
  }
  print "\n";
}


sub timestamp_of_hours_ago
{
  my $hours_ago = shift;
  my($sec,$min,$hour,$mday,$mon,$year) =
    localtime( time - $hours_ago * 60 * 60 );
  sprintf "%04d-%02d-%02d %02d:%02d:%02d",
    $year+1900, $mon+1, $mday, $hour, $min, $sec;
}

__END__

Structures returned by user_nodes():
{
  id         => 230228,
  title      => 'Re: Edit your scratch',
  createtime => '2003-01-27 09:19:03',
  lastedit   => '2003-01-27 09:19:03'
  lastupdate => '2003-01-27 09:35:09',
}

Structure returned by thread_nodes():
{
  229651 => [ 229669, ],
  229669 => [ 229699, 229846, ],
  229699 => [ 229739, ],
}

Structures returned by any_node():
{
  node_id    => 230258,
  type_id    => 11,
  type       => 'note'
  author_id  => 114691,
  author     => 'Aristotle',
  createtime => '2003-01-27 11:15:27',
  updatetime => '2003-01-27 11:18:32',
  title      => 'Re^3: Edit your scratchpad... should be easier ',
  data       =>
    {
      root_node   => 229965,
      parent_node => 230228,
      doctext     => 'I have wanted to look at your code...'
    },
}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://230274]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2023-09-26 00:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?