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.
[tags]imageshack, batch upload, image upload, perl, WWW::Mechanize, programming[/tags]
Your script is so useful, and I resolved the problem of the moving search box. see
my blog(Chinese)
or
my blog(English translation)
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. :)
Pingback: 方便的 imageshack 上传脚本 | 貓の中秋’s blog~
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….
Hi Luke
Thanks for posting this script. Just what I was looking for. I’ve updated it so that it works again. I pasted the source code here: http://pastebin.ca/1379076
Thanks. Could you please tell how to take images from a site and upload it on your own site?