User:AnomieBOT/source/trial.pl

From Wikipedia, the free encyclopedia
#!/usr/bin/perl -w

use strict;

# binmodes
$|=1;
binmode STDOUT, ':utf8';
binmode STDERR, ':utf8';

die "USAGE: $0 botnum taskfile editlimit\n" if @ARGV!=3;
die "editlimit must be a positive integer\n" unless($ARGV[2]=~/^\d+$/ && $ARGV[2]>0);

use Cwd;
use File::Basename;
use lib File::Basename::dirname( Cwd::realpath( __FILE__ ) );
use AnomieBOT::API;

my $task='';
open(X, '<:utf8', $ARGV[1]) or die("Could not open task file: $!\n");
while(<X>){
    $task=$1 if /^package (.*);$/;
}
close(X);
die "Invalid task file" unless $task ne '';
AnomieBOT::API::load($ARGV[1]);
$task=$task->new();

my $api=AnomieBOT::API->new('conf.ini', $ARGV[0]);
$api->{'nopause'}=1;
$api->{'editlimit'}=$ARGV[2];
$api->{'assert_edit'}='user' if $api->{'assert_edit'} eq 'bot';
$api->DEBUG(-1);
$api->login();

while(1){
    my $wait=60; # maximum wait time

    warn "Starting task (".ref($task).")\n" if $api->DEBUG;
    my $w=$task->run($api);
    if(!defined($w)){
        die "Task returned undef\n";
    } else {
        warn "Task returned $w\n" if $api->DEBUG;
        $wait=$w if $w<$wait;
    }

    warn "Sleeping for $wait seconds\n" if($wait>0 && $api->DEBUG);
    sleep($wait) if $wait>0;
}