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.

[tags]imageshack, batch upload, image upload, perl, WWW::Mechanize, programming[/tags]

This entry was posted in programming and tagged , , . Bookmark the permalink.



6 Responses to Batch Upload Images to ImageShack using Perl

  1. AutumnCat CHINA Mozilla Firefox Linux says:

    Your script is so useful, and I resolved the problem of the moving search box. see
    my blog(Chinese)
    or
    my blog(English translation)

    Reply  |  Quote
  2. Luke UNITED STATES Mozilla Firefox Windows says:

    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. :)

    Reply  |  Quote
  3. Pingback: 方便的 imageshack 上传脚本 | 貓の中秋’s blog~ CHINA WordPress

  4. Ayan INDIA Mozilla Firefox Windows says:

    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….

    Reply  |  Quote
  5. PerlDude DENMARK Mozilla Firefox Ubuntu Linux says:

    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

    Reply  |  Quote
  6. Thanks. Could you please tell how to take images from a site and upload it on your own site?

    Reply  |  Quote

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>