source: lab/trunk/misc/save.pl @ 202

Last change on this file since 202 was 202, checked in by mitty, 11 years ago
  • download files with LWP::UserAgent
  • filename from Content-Disposition and decode with selected coding
  • Property svn:executable set to *
File size: 1.1 KB
Line 
1#! /usr/bin/perl -w
2
3use strict;
4use warnings;
5use utf8;
6
7use LWP::UserAgent;
8use Encode;
9use Encode::Guess qw/shift-jis euc-jp 7bit-jis/;
10use File::Temp qw/ :POSIX /;
11
12my $target = shift @ARGV || die "$0: URL or file-of-url-list [coding]\n";
13my $coding = shift @ARGV || 'utf8';
14
15my $ua  = LWP::UserAgent->new;
16my $enc = find_encoding($coding);
17
18my @URLs;
19if ($target !~ /^http/ && -f $target) {
20    open(my $fh, "<$target");
21    @URLs = <$fh>;
22}
23else {
24    push @URLs, $target;
25}
26
27foreach my $url (@URLs) {
28    chomp $url;
29    my $tmpfile = tmpnam();
30    my $res = $ua->mirror($url, $tmpfile);
31   
32    if ($res->is_success) {
33        my $filename = $res->filename;
34        my $decoder = Encode::Guess->guess($filename);
35        if (ref($decoder)) {
36            $filename = $enc->encode($decoder->decode($filename));
37        }
38       
39        my $suffix = 1;
40        my $savename = $filename;
41        while (-e $savename) {
42            $savename = "$filename.$suffix";
43            $suffix++;
44        }
45        rename($tmpfile, $savename);
46    }
47    else {
48        unlink($tmpfile);
49    }
50}
Note: See TracBrowser for help on using the repository browser.