Batch Upload Images to ImageShack using Perl
Someone asked about this so I felt compelled to deliver. The question was: “how to batch upload bunch of images to some free image hosting service?” Here is the answer. I picked ImageShack because you don’t need to register it, and from what I remember they had lax rules about allowed content. Anyway, here is the Perl code. You will need WWW::Mechanize from CPAN. The script takes a list of images to be uploaded as arguments:
#!/usr/bin/perl -w use strict; use WWW::Mechanize; # suppress warnings about malformed forms $SIG{__WARN__} = sub {} ; my $url = "http://www.imageshack.us/"; my $mech = WWW::Mechanize->new(); foreach (@ARGV) { $mech->get($url); $mech->form_number(2); $mech->field('fileupload' => $_); $mech->submit(); # follow the link to see the image $mech->follow_link( text => 'Show', n => 1 ); my @im = $mech->images(); # display the URL of the uploaded image print $im[0]->url() . "\n"; }
To run it just do something like:
upload.pl ~/img/image1.jpg ~/img/image2.jpg
Alternatively to upload all the files in the current directory you can do:
ls | xargs | upload.pl
The output of the script are the URL’s of the uploaded images, appearing in order in which you specify them in arguments - this way you don’t loose track of your images.
One thing to watch for is the form that you specify in the line:
$mech->form_number(2);
When I started writing this script, I was using 1 instead of 2 and it was working fine. Then I went to eat something, and when I came back, it no longer worked. Not sure what happened, but looking at the amount of javascript on imageshack website it’s possible that they sometimes move around the search box on the page - possibly to prevent exactly what I’m showing you here.
Enjoy.
Related Posts:


June 30th, 2007 at 11:36 pm (5025) [Quote]
Your script is so useful, and I resolved the problem of the moving search box. see
Posted usingmy blog(Chinese)
or
my blog(English translation)
July 1st, 2007 at 12:40 am (5026) [Quote]
Thanks! So you essentially download a copy of their homepage and then access it locally?
The translation is not very helpfull - I see english words there, but it doesn’t make much sense. I bet English to Chinese is probably even worse than the Chinese to English. LOL
Thanks for the tip.
Posted usingNovember 4th, 2007 at 2:27 am (6826) [Quote]
[…] 自从有了 nopaste 脚本, 贴文字信息(特别是程序输出)就成了件很轻松的事 ── 只要灌到管道线就可以了. 但是贴图还是比较麻烦. 今日一番搜索, 找到了这个: http://www.terminally-incoherent.com/blog/2007/06/27/batch-upload-imag es-to-imageshack-using-perl/ […]
Posted usingJuly 12th, 2008 at 1:59 am (9594) [Quote]
An excellent script. I was looking for it for a long long time.
can you please update the script so that I can ‘batch upload’ in my own a/c ?
I have dev-key by imageshack.
Please do reply….
Posted using