User:CommanderC/image-bot.pl

From CrawlWiki
Jump to: navigation, search

#!/usr/bin/perl


# Set the pause in seconds after each upload
#my $pause = 120;
my $pause = 5;

# List the wiki PHP scripts where you have the username/password pair
my $api_url = 'http://crawl.chaosforge.org/api.php';
my $upload_url = 'http://crawl.chaosforge.org/Special:Upload';

#Then run the script on the command line using
#
# $ perl image.pl dirname
#
# where dirname/ is the name of a directory containing the files to
# be uploaded

use strict;
use warnings;
use MediaWiki::API;
use File::Spec;
use IO::Dir;
use IO::Prompt;

my $username = prompt('Username:');
my $password = prompt('Password:', -e => '*');

my $dir = $ARGV[0] or die "Syntax: perl image.pl dirname\n";
my $d = IO::Dir->new($dir);
die unless defined $d;
my @files = grep {m/\.png/ && ! m/-melee/} $d->read();
undef $d;

my $mw = MediaWiki::API->new( {
    api_url => $api_url,
    upload_url => $upload_url,
    on_error => \&on_error
  } );  

sub on_error {
  print "Error code: " . $mw->{error}->{code} . "\n";
  print $mw->{error}->{stacktrace}."\n";
  die;
}

my $r = $mw->login( { lgname => $username, lgpassword => $password } );
die unless defined($r);

for (sort(@files)) {
        sleep $pause;
        my $f = File::Spec->join($dir, $_);
        my $page = $_;
        print "Uploading $f to the wiki.\n";

        my $r = $mw->edit( {
                action   => 'upload',
                filename => $page,
                #comment = 'description',
                ignorewarnings => 1, 
                file     => [$f]
        } );
        die unless defined($r);

}


$mw->logout();