Upload Script

From www.ChopStork.com

Jump to: navigation, search

This upload script is based on a script by Erik Möller - moeller AT scireview DOT de - public domain Developed for the Wikimedia Commons

Usage

Put files to upload in a directory then run the script on the command line using

$ perl path/to/upload.pl path/to/directory

Each file will be uploaded into a category with the same name as the directory, ie...

[[Category:<directory name>]]

Notes/Warnings

Warning: as you can see, there is very little error checking so avoid the following:

  • Don't use subdirectories
  • Don't use ' in the filenames
  • I'm sure there are others

Note: Before usage, create an account following this naming schema: "File Upload Bot (Username)", for example, File Upload Bot (Eloquence). This way, these bots can be easily identified and associated with a specific user.

The Script

#!/usr/bin/perl
# Luke Bunselmeyer
# 5/17/2005
#
# Based on upload script by Erik Möller - moeller AT scireview DOT de - public domain
# Developed for the Wikimedia Commons
#
# == Usage ==
# Put files to upload in a directory then run the script on the command line using
#
#	$ perl path/to/upload.pl path/to/directory
#
# Each file will be uploaded into a category with the same name as the directory, ie...
#
#	[[Category:<directory name>]]
#
# == Notes/Warnings ==
# Warning: as you can see, there is very little error checking so avoid the following:
#	* Don't use subdirectories
#	* Don't use ' in the filenames
#	* I'm sure there are others
#
# Note: Before usage, create an account following this naming 
# schema: "File Upload Bot (Username)", for example,
# File Upload Bot (Eloquence). This way, these bots can be easily 
# identified and associated with a specific user.
#

# Set the user login and file upload url's below
$sitename = "ChopStork";
$userlogin_url = "http://www.chopstork.com/wiki/index.php?title=Special:Userlogin&action=submitlogin";
$upload_url = "http://www.chopstork.com/wiki/index.php?title=Special:Upload";

# Set the username and password below:
$username = "File Upload Bot (Wmluke)";
$password = "******";

#
# Don't edit below unless you know what you're doing.

# We need these libraries. They should be part of a standard Perl
# distribution.
use LWP::Simple;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Response;
use HTTP::Cookies;
use Cwd 'abs_path';

$docstring="Please read upload.pl for documentation.\n";
my $dir=$ARGV[0] or die "Syntax: perl upload.pl directory\n$docstring";

# Make Unix style path
$dir=~s|\\|/|gi;

# Remove trailing slashes
$sep=$/; $/="/"; chomp($dir); $/=$sep;

# Get the absolute filename of the given directory
$dir  = abs_path($dir);

# Get the basename of the given directory
@path = split(/\//, $dir);
$dirname = pop(@path);

# Init some stuff
$standard_text[0]="";
$default_text[0]="";
$stx=0; $dtx=0;

# Open...
opendir(DIR, "$dir") || die "can't opendir $dir: $!";

# Read . and ..
$dot = readdir(DIR);
$dotdot = readdir(DIR);

# Setup each file for upload
while($f = readdir(DIR)) {
		chomp($f);
		$standard_text[$stx]= "[[Category:$dirname]]";
		$stx++;
		$stw=1;

		$currentfile=$f;
		$desc_added=0;
		$dtw=0;$stw=0;

		$file{$currentfile}.="\n".$standard_text[$sx];

		# Abort the whole batch if this file doesn't exist.
		if(!-e "$dir/$f") {
				die "Could not find $dir/$f. Uploading no files.\n"
		}
}

# Close...
closedir DIR;

my $browser=LWP::UserAgent->new();
my @ns_headers = (
	'User-Agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20041107 Firefox/1.0',
	'Accept' => 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*',
	'Accept-Charset' => 'iso-8859-1,*,utf-8',
	'Accept-Language' => 'en-US',
	);

$browser->cookie_jar( {} );

$response=$browser->post($userlogin_url,
@ns_headers, Content=>[wpName=>$username,wpPassword=>$password,wpRemember=>"1",wpLoginAttempt=>"Log in"]);

# After logging in, we should be redirected to another page. 
# If we aren't, something is wrong.
#
if($response->code!=302 && !$ignore_login_error) {
        print 
"We weren't able to login. This could have the following causes:

* The username ($username) or password may be incorrect.
  Solution: Edit upload.pl and change them.
* The Wikimedia Commons software has been upgraded.
  Solution: Go to http://commons.wikimedia.org/wiki/Commons:File_upload_service
  and get a new version of the upload script.
* You are trying to hack this script for other wikis. The wiki you
  are uploading to has cookie check disabled.
  Solution: Try setting \$ignore_login_error to 1.

Regardless, we will now try to write the output from the server to 
$dir/debug.txt....\n\n";
        open(DEBUG,">$dir/debug.txt") or die "Could not write file.\n";
        print DEBUG $response->as_string;
        print 
"This seems to have worked. Take a look at the file for further information or
send it to moeller AT scireview DOT de if you need help debugging the script.\n";
        close(DEBUG);
        exit 1;
}

@file_keys = keys(%file);
$size = scalar (@file_keys);
$i = 1;
foreach $key(sort(keys(%file))) {
		print "File " .$i . " of " . $size . "\n";
		print "File: $key\n";      
		print "Description: " . $file{$key}. "\n";
		print "\n";

		$response=$browser->post($upload_url,
		@ns_headers,Content_Type=>'form-data',Content=>
		[
		wpUploadFile=>["$dir/$key"],
		wpUploadDescription=>$file{$key},
		wpUploadAffirm=>"1",
		wpUpload=>"Upload file",
		wpIgnoreWarning=>"1"
		]);
		#print $response->as_string;
		push @responses,$response->as_string;
		$i++;
}

print "Everything seems to be OK. Log will be written to $dir/debug.txt.\n";
open(DEBUG,">$dir/debug.txt") or die "Could not write file.\n";
print DEBUG @responses;