01st Jul 2007

imageshack 上传脚本 ── 改进版

前几天介绍的 imageshack 上传脚本 有一个缺点, 就是 imageshack 会不时修改上传页面, 导致使用模拟方法的脚本失效. 而且每次都要先下载上传页面, 比较慢. 于是我就想到了将上传页面放在本地:

将 imgshack 脚本改成这样(感谢:老狼):

#!/usr/bin/perl -w
use strict;
use WWW::Mechanize;
use FindBin qw($Bin);

# suppress warnings about malformed forms
$SIG{__WARN__} = sub {} ;

my $url = "file://$Bin/imgshack_client.html";

my $mech = WWW::Mechanize->new();

foreach (@ARGV)
{
    $mech->get($url);

    $mech->form_number(1);
    $mech->field('fileupload' => $_);
    $mech->submit();

    # follow the link to see the image
    $mech->follow_link( text => 'Show', n => 1 );

    # display the URL of the uploaded image
    print (($mech->images())[0]->url() . "\n");
}

然后在脚本的相同目录下建一个网页文件 imgshack_client.html :

<html>
    <head></head>
    <body>
        <form action="http://load.imageshack.us/" method="post" enctype="multipart/form-data" target="_blank">
            <input name="fileupload" type="file">
            <input type="submit">
        </form>
    </body>
</html>

这样的话就不受远程上传页面的控制了

Leave a Reply