]> mj.ucw.cz Git - gallery.git/blob - bin/gal-date
Switched to common::sense
[gallery.git] / bin / gal-date
1 #!/usr/bin/perl
2 # UCW Gallery: Symlink photos according to their EXIF timestamps
3 # (c) 2013 Martin Mares <mj@ucw.cz>
4
5 use common::sense;
6
7 use Image::EXIF;
8 use Getopt::Long;
9
10 if (@ARGV && $ARGV[0] eq '--help') {
11         die <<AMEN ;
12 Usage:  cat list | gal date [<options>]
13    or:  gal date [<options>] <files>
14
15 Options:
16 -d, --destdir=<dir>     Create symlinks in specified directory (default: current dir)
17 -n, --dry-run           Perform a trial run with no changes made
18 -o, --offset=<offset>   Adjust timestamps by a given offset (<h>[:<m>[:<s>]])
19     --suffix=<s>        Add suffix to all names
20 -s, --symbolic          Create symbolic links (default: hardlinks)
21 AMEN
22 }
23
24 my $destdir = '.';
25 my $dry_run = 0;
26 my $offset = 0;
27 my $suffix = "";
28 my $symbolic = 0;
29 Getopt::Long::Configure('bundling');
30 GetOptions(
31         'destdir|d=s' => \$destdir,
32         'dry-run|n!' => \$dry_run,
33         'offset|o=s' => \$offset,
34         'suffix=s' => \$suffix,
35         'symbolic|s!' => \$symbolic,
36 ) or die "Try gal date --help\n";
37
38 my @src = @ARGV;
39 if (!@src) {
40         while (<STDIN>) {
41                 chomp;
42                 push @src, $_;
43         }
44 }
45
46 if ($offset =~ m{^([+-])?(\d)+(:(\d+)(:(\d+))?)?$}) {
47         $offset = $2 * 3600 + ($4 // 0) * 60 + ($6 // 0);
48         if ($1 eq '-') { $offset = -$offset; }
49 } else {
50         die "Invalid offset: $offset\n";
51 }
52
53 foreach my $f (@src) {
54         my $e = new Image::EXIF($f);
55         my $i = $e->get_all_info();
56         if ($e->error) {
57                 print STDERR "EXIF error on $f: ", $e->error, "\n";
58                 next;
59         }
60         # print STDERR Dumper($i), "\n";
61
62         my $d = $i->{'image'}->{'Image Created'};
63         if (!defined $d) {
64                 print STDERR "No date for $f\n";
65                 next;
66         }
67         my ($ty, $tm, $td, $tH, $tM, $tS) = ($d =~ m{^(\d{4}):(\d{2}):(\d{2}) (\d{2}):(\d{2}):(\d{2})$}) or die "EXIF date parse error: $d\n";
68         my $t = 3600*$tH + 60*$tM + $tS;
69         $t = int($t + $offset);
70         $tH = int($t / 3600);
71         $tM = int(($t % 3600) / 60);
72         $tS = $t % 60;
73
74         my $retry = 0;
75         my $dest = "";
76         do {
77                 $dest = sprintf("%s/%04d-%02d-%02d-%02d:%02d:%02d%s%s.jpg",
78                         $destdir,
79                         $ty, $tm, $td,
80                         $tH, $tM, $tS,
81                         $suffix,
82                         $retry ? sprintf("-%d", $retry) : "");
83                 $retry++;
84         } while (-f $dest);
85
86         print "$f $dest\n";
87
88         unless ($dry_run) {
89                 if ($symbolic) {
90                         symlink $f, $dest or die "Cannot symlink $f to $dest: $!\n";
91                 } else {
92                         link $f, $dest or die "Cannot hardlink $f to $dest: $!\n";
93                 }
94         }
95 }
96
97 STDOUT->autoflush(1);