* download files with LWP::UserAgent
authormitty <mitty@7d2118f6-f56c-43e7-95a2-4bb3031d96e7>
Sun, 10 Mar 2013 09:53:02 +0000 (09:53 +0000)
committermitty <mitty@7d2118f6-f56c-43e7-95a2-4bb3031d96e7>
Sun, 10 Mar 2013 09:53:02 +0000 (09:53 +0000)
 * 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 [new file with mode: 0755]

diff --git a/misc/save.pl b/misc/save.pl
new file mode 100755 (executable)
index 0000000..6b673a8
--- /dev/null
@@ -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);
+    }
+}