星期四, 2月 17, 2005

自動轉貼圖

Gea-Suan Lin’s BLOG » 自動轉貼圖片
http://gslin.org/2005/02/16/569/

#!/usr/bin/perl

use File::Basename;
use Getopt::Std;
use WWW::Mechanize;
use strict;

my %opt;
getopts('dp:', \%opt);

my $debug = 0;
$debug = 1 if (defined($opt{'d'}));

my $proxy;
if (defined($opt{'p'}))
{
$proxy = $opt{'p'};
}
elsif (defined($ENV{'http_proxy'}))
{
$proxy = $ENV{'http_proxy'};
}

my $url = shift() or die();

chdir('/tmp');
&main();

sub main
{
my $outfile = sprintf('%s', basename($url));
print('* Filename: ', $outfile, \"\n\");

# Get
my $agent = WWW::Mechanize->new();
$agent->proxy($proxy) if ($proxy ne '');
$agent->get($url);

my $content = $agent->content();

open(O, '> ' . $outfile);
print(O $content);
close(O);

print('* Filesize: ', length($content), \"\n\");

# then upload
$agent->get('http://www.imagevenue.com/');
$agent->form_number(1);
$agent->field('file1', $outfile);
$agent->submit();

print($agent->content()) if ($debug);

# tell me the url
$agent->form_name('form8');
my $imgurl = $agent->value('select');
chomp($imgurl);
$imgurl =~ s/\s+//g;

print('* Output URL: ', $imgurl, \"\n\");

# delete temp file
unlink($outfile);
}