From: mitty Date: Sun, 10 Mar 2013 09:53:02 +0000 (+0000) Subject: * download files with LWP::UserAgent X-Git-Url: http://lab.mitty.jp/git/?a=commitdiff_plain;h=a74d46aaffc01258959a744d35aaad30d58f425d;p=lab.git * download files with LWP::UserAgent * filename from Content-Disposition and decode with selected coding git-svn-id: https://lab.mitty.jp/svn/lab/trunk@202 7d2118f6-f56c-43e7-95a2-4bb3031d96e7 --- diff --git a/misc/save.pl b/misc/save.pl new file mode 100755 index 0000000..6b673a8 --- /dev/null +++ b/misc/save.pl @@ -0,0 +1,50 @@ +#! /usr/bin/perl -w + +use strict; +use warnings; +use utf8; + +use LWP::UserAgent; +use Encode; +use Encode::Guess qw/shift-jis euc-jp 7bit-jis/; +use File::Temp qw/ :POSIX /; + +my $target = shift @ARGV || die "$0: URL or file-of-url-list [coding]\n"; +my $coding = shift @ARGV || 'utf8'; + +my $ua = LWP::UserAgent->new; +my $enc = find_encoding($coding); + +my @URLs; +if ($target !~ /^http/ && -f $target) { + open(my $fh, "<$target"); + @URLs = <$fh>; +} +else { + push @URLs, $target; +} + +foreach my $url (@URLs) { + chomp $url; + my $tmpfile = tmpnam(); + my $res = $ua->mirror($url, $tmpfile); + + if ($res->is_success) { + my $filename = $res->filename; + my $decoder = Encode::Guess->guess($filename); + if (ref($decoder)) { + $filename = $enc->encode($decoder->decode($filename)); + } + + my $suffix = 1; + my $savename = $filename; + while (-e $savename) { + $savename = "$filename.$suffix"; + $suffix++; + } + rename($tmpfile, $savename); + } + else { + unlink($tmpfile); + } +}