ProsperMonkey

From Prospers
Jump to: navigation, search

Also see user_ProsperMonkey

The source code, for posterity:

#!/usr/bin/perl -w
use strict;

open(LOG, ">>log");
print LOG `date`;

use WWW::Mechanize;

my $agent = WWW::Mechanize->new( timeout => 60, onwarn => \&logit );
my $agent2 = WWW::Mechanize->new( timeout => 60, onwarn => \&logit );
$agent2->get("http://prospers.org/forum/index.php");
$agent2->form(1);
$agent2->field("user", "ProsperMonkey");
$agent2->field("passwrd", "...");
$agent2->click();

# switched to do local forums first, to combat crapping out when spreebb is down

# since we are already logged in, just use agent2
$agent = $agent2;
$agent->get("http://prospers.org/forum/index.php");
$agent->follow("Show unread posts since last visit.");

# assume there will only be one page
my @posts = $agent->find_all_links( url_abs_regex => qr/index.php\?topic=\d+\.msg\d+;topicseen#new/ );
#mark forums as read
$agent->follow("Mark ALL messages as read");
DoPosts(qr/^(.*?index.php\?topic=\d+)\.msg\d+;.*$/, "Prospers.ORG");

#$agent->get("http://prosper.spreebb.com/");
#$agent->follow("Log In");
#$agent->form_name("LOGIN");
#$agent->field("UserName", "ProsperMonkey");
#$agent->field("PassWord", "...");
#$agent->field("CookieDate", "0");
#$agent->click();
#$agent->follow("View New Posts");

## assume there will only be one page
## assume we check often enough that the "last post" links will do well enough
#@posts = $agent->find_all_links( url_abs_regex => qr/getlastpost/ );
##mark forums as read
#$agent->get("http://prosper.spreebb.com/index.php?act=Login&CODE=05");
#DoPosts(qr/^(.*?showtopic=\d+).*$/, "Official forum");


sub DoPosts
{
my ($urlmatch, $forumname) = @_;
print LOG "forum name: $forumname\n";
my $post;
foreach $post (@posts)
{
        my $post_url = $post->url_abs();
        $post_url =~ s/$urlmatch/$1/;
        $agent->get($post_url);
        my $post_title = $agent->title();
        $post_title =~ s/^.*?-> (.*)$/$1/;
        print LOG "Post: $post_title [$post_url]\n";

        # eat signatures, since people like to put listings in there and then
        # talk in unrelated threads
        # also "darkrow3" which is the www-button line
        my $html = $agent->content();
        $html =~ s!<div class=.signature.>.*?</div>!!isg;
        $html =~ s!<div align='left' class='darkrow3'.*?</div>!!isg;
        $html =~ s!<br />_________________<br />.*?</td>!!isg;
        $agent->update_html($html);

        my @listings = $agent->find_all_links( url_regex => qr/listingID=\d+/i );
        my %alreadyseen;
        my $listing;
        foreach $listing (@listings)
        {
                my $listing_id = $listing->url();
                $listing_id =~ s/^.*?listingID=(\d+).*$/$1/i;
                print LOG "Listing: $listing_id\n";
                my $skipit = 0;
                {
                        local $^W = 0;
                        $skipit = $alreadyseen{$listing_id};
                        if(not defined($skipit))
                        {
                                $skipit = 0;
                        }
                }
                if($skipit == 1)
                {
                                print LOG "Already seen this listing in this post\n";
                                next;
                }
                $alreadyseen{$listing_id} = 1;
                my $wiki_url = "http://prospers.org/wiki/$listing_id?action=edit";
                {
                        local $^W = 0;
                        $agent2->get($wiki_url);
                }
                $agent2->form(1);

                my $content = $agent2->value("savetext", 1);
                print LOG "Current content: $content\n";
                if($content eq "Describe $listing_id here.")
                {
                        print LOG "Blank page.  Adding monkey table\n";
                        $content = NewMonkeyTable($listing_id);
                }
                if(($content =~ /\|\| \[\[user:ProsperMonkey\]\] \|\|/) or ($content =~ /\|\| -- ProsperMonkey \|\|/))
                {
                        print LOG "Found monkey table\n";
                }
                else
                {
                        print LOG "No monkey table found.  Adding at top\n";
                        $content = NewMonkeyTable($listing_id).$content;
                }
                # if this post url is not already here, then insert it
                if(-1 == index($content, $post_url))
                {
                        my $newrow = "|| $forumname || [$post_url $post_title] ||";
                        print LOG "Adding row: $newrow\n";
                        # find the last line of the monkey table and insert before that
                        # only one of these should fire
                        $content =~ s/(\|\| \[\[user:ProsperMonkey\]\] \|\|)/$newrow\n$1/;
                        $content =~ s/(\|\| -- ProsperMonkey \|\|)/$newrow\n$1/;
                        $agent2->field("savetext", $content);
                        $agent2->field("comment", "Auto update");
                        $agent2->click("button_save");
                } # end of url not already there
                else
                {
                        print LOG "This url is already in the page, skipping\n";
                }
        } # end of foreach listing in post
} # end of foreach new post
} # end of DoPosts

sub NewMonkeyTable
{
        my $listing_id = $_[0];
        return <<EOT;
|| Prosper || [https://www.prosper.com/public/lend/listing.aspx?listingID=$listing_id Listing $listing_id] ||
|| -- ProsperMonkey ||
EOT
}
sub logit
{
        print LOG $_[0];
}