Current projects/upload images/upload-images.pl

From CrawlWiki
Revision as of 02:13, 9 January 2013 by CommanderC (talk | contribs) (Created page with "#!/usr/bin/perl my $username = "USERNAME"; my $password = "PASSWORD"; # Set the pause in seconds after each upload my $pause = 60; # List the wiki PHP scripts where you have t...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
  1. !/usr/bin/perl

my $username = "USERNAME"; my $password = "PASSWORD";

  1. Set the pause in seconds after each upload

my $pause = 60;

  1. 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';

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

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

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 (@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,
               file     => [$f]
       } );
       die unless defined($r);

}


$mw->logout();