#!/usr/bin/perl use strict; use warnings; use WWW::Mechanize; use HTTP::Response; my ($user, $passwd) = ("anotherTime", "no_abuse_please"); my $homepage = 'http://www.perlmonks.org/'; print STDERR "getting homepage [$homepage]\n"; my $mech = WWW::Mechanize->new( agent => 'Virtual Monk 0.1' ); $mech->get($homepage)->is_success() || die "unable to get $homepage, stopped"; print STDERR "logging in\n"; $mech->submit_form( form_name => 'login', fields => { user => $user, passwd => $passwd }, button => 'login' )->is_success() || die "unable to log in $homepage, stopped"; # Ensure we've logged in successfully my $logout = $mech->find_link( text => "log $user out" ); die "did not log in, stopped" unless $logout; my $votes = 0; for (my $maxcount = 0; $maxcount < 100; ++$maxcount) { print STDERR "going in my home node\n"; $mech->follow_link( text => $user )->is_success() || die "could not get into my home node, stopped"; print STDERR "assessing if I've spare votes\n"; last unless $mech->response()->content() =~ /You have ([0-9]+)<\/b> votes? left today\./; last unless $1; # Find a page where I can vote. Index indicates the index of the voting form my $index = 0; for (my $maxcount2 = 0; $maxcount2 < 100 && ! $index; ++$maxcount2) { print STDERR "getting a random page\n"; $mech->follow_link( text => 'Random Node' )->is_success() || die "could not get a random node, stopped"; print STDERR "verifying I can vote anywhere in it\n"; my $count = 0; foreach my $form ($mech->forms()) { ++$count; # Form numbers start from 1 if (defined($form->find_input('sexisgreat', 'submit'))) { $index = $count; last; } } } if ($index) { # A voting form is available print STDERR "casting a vote\n"; $mech->form_number($index); $mech->set_visible( [ radio => "1" ] ); $mech->click('sexisgreat'); ++$votes; } } print STDERR "$votes vote" . ($votes != 1 ? "s" : "") . " cast\n"; # Log out print STDERR "logging out\n"; $mech->get($logout->url()) || die "unable to log out, stopped";