Skip to content

Instantly share code, notes, and snippets.

@RogerDodger
Last active December 17, 2015 14:49
Show Gist options
  • Save RogerDodger/5627218 to your computer and use it in GitHub Desktop.
Save RogerDodger/5627218 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use Mojo::Base -strict;
use Mojo::UserAgent;
use DateTime;
use DateTime::Format::Strptime;
my $id = shift or print <<"USAGE" and exit(1);
usage: $0 thread
USAGE
if ($id =~ /[^0-9]/) {
say "Invalid thread: `$id`. Must be an integer";
exit(1);
}
my $base_url = "http://forums.heroesofnewerth.com/showthread.php?$id";
my $ua = Mojo::UserAgent->new;
my $fmt = DateTime::Format::Strptime->new(
pattern => '%m-%d-%Y, %I:%M %p',
on_error => 'croak',
);
my $today = DateTime->now->mdy;
my $yesterday = DateTime->now->subtract(days => 1)->mdy;
my (%post_count, %last_post);
for (my ($page, $pages) = (1, 1); $page <= $pages; $page++) {
say STDERR "Fetching page $page" . ($pages == 1 ? '' : " of $pages") . '...';
my $dom = $ua->get("$base_url/page$page")->res->dom;
if ($pages == 1) {
if (defined(my $e = $dom->at('#pagination_top .popupctrl'))) {
($pages) = $e->text =~ /Page \d+ of (\d+)/;
}
}
$dom->find('.postcontainer')->each(sub {
my $user = $_->at('a.username')->all_text =~ s/^\[.+\]//r;
my $datetime = $fmt->parse_datetime(
$_->at('.postdate .date')->all_text
=~ s/Yesterday/$yesterday/r
=~ s/Today/$today/r
);
$last_post{$user} = $datetime;
$post_count{$user}++;
});
}
say "For $base_url";
say q{};
say ' Username Last post Total posts';
say '---------------------------------------------';
for (reverse sort { $last_post{$a} cmp $last_post{$b} } keys %last_post) {
printf " %12s %17s %d\n", $_,
$last_post{$_}->strftime('%d %b %Y %H:%M'), $post_count{$_};
}
say q{};
say "Data gathered " . DateTime->now->strftime('%d %b %Y %H:%M');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment