]> git.donarmstrong.com Git - bin.git/blob - archive_photos
add archive photos command
[bin.git] / archive_photos
1 #!/usr/bin/perl
2 # archive_photos archives photos into a directory structure
3 # and is released under the terms of the GNU GPL version 3, or any
4 # later version, at your option. See the file README and COPYING for
5 # more information.
6 # Copyright 2013 by Don Armstrong <don@donarmstrong.com>.
7
8
9 use warnings;
10 use strict;
11
12 use Getopt::Long;
13 use Pod::Usage;
14
15 =head1 NAME
16
17 archive_photos - archives photos into a directory structure
18
19 =head1 SYNOPSIS
20
21 archive_photos [options] path/to/photos [additional photos]
22
23  Options:
24   --recurse, -r recurse into subdirectories (default)
25   --archive-dir, -a directory to archive into (~/media/photos)
26   --debug, -d debugging level (Default 0)
27   --help, -h display this help
28   --man, -m display manual
29
30 =head1 OPTIONS
31
32 =over
33
34 =item B<--recurse, -r>
35
36 Recurse into subdirectories (default; use --no-recurse to disable
37
38 =item B<--archive-dir, -a>
39
40 Directory to archive to (default is ~/media/photos)
41
42 =item B<--debug, -d>
43
44 Debug verbosity. (Default 0)
45
46 =item B<--help, -h>
47
48 Display brief usage information.
49
50 =item B<--man, -m>
51
52 Display this manual.
53
54 =back
55
56 =head1 EXAMPLES
57
58 archive_photos
59
60 =cut
61
62
63 use vars qw($DEBUG);
64 use User;
65 use File::Find;
66 use Image::ExifTool qw(ImageInfo);
67 use POSIX qw(strftime);
68 use File::Copy;
69 use File::Path qw(make_path);
70 use File::Basename;
71 use Date::Parse qw(str2time);
72
73 my %options = (debug           => 0,
74                help            => 0,
75                man             => 0,
76                archive_dir     => User->Home."/media/photos",
77                recurse         => 1,
78               );
79
80 GetOptions(\%options,
81            'archive_dir|archive-dir=s',
82            'recurse!',
83            'debug|d+','help|h|?','man|m');
84
85 pod2usage() if $options{help};
86 pod2usage({verbose=>2}) if $options{man};
87
88 $DEBUG = $options{debug};
89
90 my @USAGE_ERRORS;
91 if (not @ARGV) {
92      push @USAGE_ERRORS,"You must give at least one directory";
93 }
94
95 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
96
97 our @files;
98
99 # loads files into @files as appropriate
100 sub file_pusher {
101     if (-d $_ and not $options{recurse}) {
102         $File::Find::prune = 1;
103         return;
104     }
105     if (-f $_ and $_ =~ /\.(?:NEF|JPG|MOV)$/i) {
106         push @files,$File::Find::name;
107     }
108 }
109
110 my @dirs;
111
112 for my $arg (@ARGV) {
113     if (-d $arg) {
114         push @dirs,$arg;
115     } else {
116         push @files,$arg;
117     }
118 }
119
120 # search all of the directories and store the files in @files;
121 find(\&file_pusher,@dirs) if @dirs;
122
123 # find already existing files
124 my %existing_files;
125 find(sub { if (-f $_) {$existing_files{$_} = $File::Find::name;} },$options{archive_dir});
126
127 for my $file (@files) {
128     my $file_basename=basename($file);
129     # find out when the photo was shot
130     my $info = ImageInfo($file);
131     if (not defined $info->{CreateDate}) {
132         print STDERR "No date information for $file\n";
133         next;
134     }
135     my $epoch = str2time($info->{CreateDate});
136     my $dir = strftime('%Y/%m_%B/%Y_%m_%d/',localtime($epoch)).'orig/';
137     if (not -d $options{archive_dir}.'/'.$dir) {
138         make_path($options{archive_dir}.'/'.$dir) or
139             die "Unable to make dir $!";
140     }
141     my $end_location = $options{archive_dir}.'/'.$dir.$file_basename;
142     if (-e $end_location) {
143         print STDERR "$file already exists in $end_location\n";
144     } elsif (exists $existing_files{$file_basename}) {
145         print STDERR "$file already exists in $existing_files{$file_basename}\n";
146     } else {
147         print STDERR "copying $file to $end_location\n";
148         copy($file,$end_location);
149     }
150 }
151
152
153
154 __END__