User:ZyMOS/Meta:upload

a batch upload script


 * 1) !/usr/bin/perl -w
 * 2) Set the username and password below:

$username = "xxxxxxxxxx"; $password = "xxxxxxxxxx";

$loginURL = "http://howto.wikia.com/index.php?title=Special:Userlogin&amp;action=submitlogin"; $uploadURL = "http://howto.wikia.com/wiki/Special:Upload";
 * 1) Set Login and Upload Page locations

$uploadFileDesc = "Licence: GFDL"; #batch description for all uploaded files
 * 1) Uploaded File  Description


 * 1) make a file called files.txt with each line the filename of the file u want to upload
 * 2) $ ls --color=none . > files.txt


 * 1) Then run the script on the command line using
 * 2) $ perl upload.pl dirname
 * 3) where dirname/ is the name of a directory containing the files to
 * 4) be uploaded, and a file named files.txt in the following format
 * 1) where dirname/ is the name of a directory containing the files to
 * 2) be uploaded, and a file named files.txt in the following format

use LWP::Simple; use LWP::UserAgent; use HTTP::Request; use HTTP::Response; use HTTP::Cookies; use Encode qw(encode); use warnings;
 * 1) We need these libraries. They should be part of a standard Perl
 * 2) distribution.

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

$dir=~s|\\|/|gi;
 * 1) Make Unix style path

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

open(FILELIST,"<$dir/files.txt") or die "Could not find file list at $dir/files.txt.\n$docstring";
 * 1) Now try to get the list of files

$standard_text[0]=""; $default_text[0]=""; $stx=0; $dtx=0; while() { $line=$_; chomp($line); #system("sleep 1.5s"); if($line=~m/^@/) { $line=~s/^@//; $standard_text[$stx]=$line; $stx++; $stw=1; }       elsif($line=~m/^°/) { $line=~s/^°//; $default_text[$dtx]=$line; $dtx++; $dtw=1; }       elsif($line=~m/.*/) { $line=~s/^>//;

# New file, but last one doesn't have a description yet - # add current default. if($currentfile) { # If there's been a change of the default or standard # text, we need to apply the old text to the previous # file, not the new one. $dx= $dtw? $dtx-2 : $dtx -1; $sx= $stw? $stx-2 : $stx -1; if(!$desc_added) { $file{$currentfile}.="\n".$default_text[$dx]; }                       $file{$currentfile}.="\n\n".$standard_text[$sx]; }                                              # Abort the whole batch if this file doesn't exist. if(!-e "$dir/$line") { die "Could not find $dir/$line. Uploading no files.\n" }                              $currentfile=$line; $desc_added=0; $dtw=0;$stw=0; }else { # If this is a header comment, # we just ignore it. Otherwise # it's a file description. if($currentfile) { $file{$currentfile}.="\n".$line; $desc_added=1; }       } }

if($currentfile) { $dx= $dtw? $dtx-2 : $dtx -1; $sx= $stw? $stx-2 : $stx -1; if(!$desc_added) { $file{$currentfile}.="\n".$default_text[$dx]; }       $file{$currentfile}.="\n\n".$standard_text[$sx]; }
 * 1) Last file needs to be processed, too

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("$loginURL", @ns_headers, Content=>[wpName=>$username,wpPassword=>$password,wpRemember=>"1",wpLoginattempt=>"Log in"]);

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

Solution: Edit upload.pl and change them. are uploading to has cookie check disabled. Solution: Try setting \$ignore_login_error to 1.
 * The username ($username) or password may be incorrect.
 * You are trying to hack this script for other wikis. The wiki you

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         close(DEBUG);        exit 1; }

foreach $key(keys(%file)) { print "Uploading $key to site. Description: $uploadFileDesc\n"; print $file{$key}."\n". "-" x 75. "\n"; uploadfile: $eckey=encode('utf8',$key); if($eckey ne $key) { symlink("$key","$dir/$eckey"); }       $response=$browser->post("$uploadURL",        @ns_headers,Content_Type=>'form-data',Content=>        [                wpUploadFile=>["$dir/$eckey"],                wpDestFile=>["$eckey"],		wpUploadDescription=> "$uploadFileDesc",                wpUpload=>"Upload file",                wpIgnoreWarning=>"1"        ]); push @responses,$response->as_string; if($response->code!=302 && $response->code!=200) { print "Upload failed! Will try again. Output was:\n"; print $response->as_string; goto uploadfile; } else { print "Uploaded successfully.\n"; }                system("sleep 2"); }
 * 1)               wpUploadAffirm=>"1",

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;