Dotfiles, utilities, and other apparatus.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

124 lines
2.4 KiB

#!/usr/bin/env perl
=pod
=head1 NAME
photocp - copy files from digital camera media to my home dir
=head1 SYNOPSIS
# try a couple of defaults
photocp
# look in source_directory
photocp [source_directory]
=head1 DESCRIPTION
This operates on a handful of assumptions, some of which are becoming obsolete
as "cameras" are mostly phones these days:
=over
=item A given source will have a bunch of unique filenames.
=item You're only likely to be copying from one source on a given day.
=item You want to copy all of the files on the source (this is the big one).
=back
=head1 AUTHOR
Brennen Bearnes
=cut
use strict;
use warnings;
use 5.10.0;
use POSIX qw(strftime);
use File::Basename;
use File::Copy;
use File::Find;
use File::HomeDir;
my $home = File::HomeDir->my_home;
my $archive_dir = $home . '/workspace/photos';
unless (-d $archive_dir) {
die "$archive_dir is not a directory";
}
my @sources = (
'/media/' . $ENV{'USER'} . '/EOS_DIGITAL',
'/media/' . $ENV{'USER'} . '/CANON_DC',
'/media/' . $ENV{'USER'} . '/NIKON D600',
'/media/' . $ENV{'USER'} . '/NO NAME',
'/media/' . $ENV{'USER'} . '/9016-4EF8',
'/media/CANON_DC',
'/media/EOS_DIGITAL',
'/media/disk/DCIM',
'/media/NIKON D600',
'/run/user/1000/gvfs/' # XXX: this is fairly stupid.
);
my @t = localtime;
my $daydir = strftime("%Y-%m-%d", @t);
my $src;
if ((defined $ARGV[0]) && (-d $ARGV[0])) {
$src = $ARGV[0];
} else {
foreach my $source (@sources) {
if (-d $source) {
$src = $source;
last;
}
}
unless ($src) {
die 'source not mounted? tried ' . join(' ', @sources);
}
}
my $dest = $archive_dir . '/' . $daydir;
if ((-e $dest) && (! -d $dest)) {
die 'target exists but is not a directory';
}
if (! -d $dest) {
say "creating $dest";
mkdir($dest);
} else {
say "$dest exists";
}
my $count = 0;
# XXX: What this doesn't *really* handle is the case where you sync
# from multiple cameras and they have overlapping filenames for
# the same sync date.
find(
sub {
if ( /[.] (jpe?g | mpe?g | mp4 | avi | raw | cr2 | tiff? | bmp | png | gif | mov) $/ix ) {
$count++;
my $copy_to = "$dest/$_";
if (-e $copy_to) {
say "$copy_to already exists - skipping copy";
} else {
say "copying $File::Find::name to $copy_to";
copy($File::Find::name, $copy_to)
or die "copy failed: $!";
}
}
},
$src
);
say "$count files copied";