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:

  • Is anyone else having issues with Tumblr?
  • Using CPAN version of WWW::Mechanize with ActiveState Perl on Windows
  • Flickr Image Limit Sucks
  • Command Line SCP for Windows
  • Show me your desktop
  • Biggest Regex In The Word
  • Take a Speed Test
  • Overswamped
  • Screen Scraping for RSS
  • Latex: Rotate Inserted Images

  • 5 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. [...] 自从有了 nopaste 脚本, 贴文字信息(特别是程序输出)就成了件很轻松的事 ── 只要灌到管道线就可以了. 但是贴图还是比较麻烦. 今日一番搜索, 找到了这个: http://www.terminally-incoherent.com/blog/2007/06/27/batch-upload-imag es-to-imageshack-using-perl/ [...]

    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

    Leave a Reply