App-PFT
view release on metacpan or search on metacpan
bin/pft-grab view on Meta::CPAN
Getopt::Long::Configure ("bundling");
use PFT::Tree;
use URI;
use LWP::Simple;
my %date;
my %opts = (
dst => 'a',
date => 0,
);
GetOptions(
'year|y=i' => \$date{year},
'month|m=s' => \$date{month},
'day|d=i' => \$date{day},
'today|t!' => \$opts{date_prefix},
'picture|p!' => sub { $opts{dst} = 'p' },
#'attach|a!' => sub { $opts{dst} = 'a' },
'rename|r=s' => \$opts{rename},
'help|h' => sub {
pod2usage
-exitval => 1,
-verbose => 2,
-input => App::PFT::help_of 'grab',
}
) or exit 1;
@ARGV or do {
say STDERR 'Any file?';
exit 1;
};
@ARGV > 1 and $opts{rename} and do {
say STDERR '--rename is not allowed with multiple files';
exit 1;
};
my $content = eval{ PFT::Tree->new->content } or do {
say STDERR $@ =~ s/ at.*$//rs;
exit 1
};
my $dst_base = do {
my $o = $opts{dst};
$o eq 'a' ? $content->dir_attachments :
$o eq 'p' ? $content->dir_pics :
die;
};
my $dst_dir = do {
if ($opts{date_prefix} || grep defined @date{qw/year month day/}) {
catdir($dst_base, PFT::Date->from_spec(%date)->repr('-'))
} else {
$dst_base
}
};
make_path encode(locale_fs => $dst_dir);
ITEM: for my $orig_path (@ARGV) {
my $uri = URI->new($orig_path);
my $fn = basename $uri->path . ($uri->query || '');
my $dst_path = catfile($dst_dir, $opts{rename} || $fn);
if ($uri->has_recognized_scheme) {
my $status = LWP::Simple::getstore($uri->as_string, $dst_path);
if ($status < 200 || $status >= 300) {
say STDERR 'Failed to retrieve ', $uri->as_iri, ': ', $status;
next ITEM;
}
}
else {
File::Copy::copy($orig_path, $dst_path) or do {
say STDERR "Copy failed: $!";
exit 2
}
}
my $relative = abs2rel($dst_path, $dst_base);
say STDOUT
$opts{dst} eq 'a' ? "[$fn]: :attach:$relative" :
$opts{dst} eq 'p' ? "" :
die;
}
( run in 1.088 second using v1.01-cache-2.11-cpan-ceb78f64989 )