From a74d46aaffc01258959a744d35aaad30d58f425d Mon Sep 17 00:00:00 2001 From: mitty Date: Sun, 10 Mar 2013 09:53:02 +0000 Subject: [PATCH] * 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 --- misc/save.pl | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100755 misc/save.pl 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); + } +} -- 1.7.9.5