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
  • Take a Speed Test
  • Show me your desktop
  • Biggest Regex In The Word
  • Screen Scraping for RSS
  • Latex: Rotate Inserted Images
  • Saving Bandwidth and Preventing Hotlinking With Coral Cache

  • 4 Responses to “Batch Upload Images to ImageShack using Perl”

    1. Gravatar AutumnCat CHINA Says: Reply to this comment

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

      Posted using Mozilla Firefox Mozilla Firefox 2.0.0.4 on Linux Linux
    2. Gravatar Luke UNITED STATES Says: Reply to this comment

      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 using Mozilla Firefox Mozilla Firefox 2.0.0.4 on Windows Windows XP
    3. Gravatar 方便的 imageshack 上传脚本 | 貓の中秋’s blog~ CHINA Says: Reply to this comment

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

      Posted using WordPress WordPress wordpress
    4. Gravatar Ayan INDIA Says: Reply to this comment

      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 Mozilla Firefox Mozilla Firefox 3.0 on Windows Windows XP

    Leave a Reply

    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <pre lang=""> <em> <i> <strike> <strong>

    [Quote selected]