-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess
More file actions
executable file
·250 lines (211 loc) · 7.05 KB
/
process
File metadata and controls
executable file
·250 lines (211 loc) · 7.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env perl
use warnings;
use strict;
use v5.22.0;
no warnings 'experimental::smartmatch';
use Image::ExifTool qw/:Public/;
use File::Find::Rule;
use File::Path qw/make_path/;
use File::Copy qw/cp/;
use Furl;
use JSON;
use Carp;
use List::Util qw/first/;
use Digest::SHA qw/sha256_hex/;
use Time::Piece;
use Data::Dumper;
%Image::ExifTool::UserDefined::Options = (
LargeFileSupport => 1,
);
use constant MONTH => [
qw/_ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/
];
my $furl = Furl->new(
headers => [ 'Accept-Encoding' => 'gzip' ],
);
die "Usage: $0 [files...] [target]\n" unless @ARGV > 1;
my $target = pop @ARGV;
die "$target is not a directory" unless -d $target;
processFile($_, $target) for grep { $_ !~ m|/\.[^/]+$| } files(@ARGV);
sub files {
return map {
-d $_ ? findFiles($_) :
-e $_ ? $_ : die "Could not find file $_";
} @_;
}
sub findFiles {
File::Find::Rule->file->name(qr/\.(mov|mp4|avi)$/i)->in(shift);
}
sub formatDate {
my $datetime = shift;
$datetime .= ":00" unless $datetime =~ /\d+:\d+:\d+/;
$datetime .= "-05:00" unless $datetime =~ /-/;
my ($date, $time) = split " ", $datetime;
$date =~ tr/:/-/;
$time =~ s/([+-].*)$/.0$1/;
return "${date}T${time}";
}
sub extractRecords {
my $info = shift;
my $records = {};
for (keys %$info) {
next unless /^AcquisitionRecordGroupItem(Name|Value)(.*)/;
$records->{$2}{$1} = $info->{$_};
}
return { map { $_->{Name} => $_->{Value} } values %$records };
}
# Rund to ~100m (at the equator)
sub roundLatLong {
my $value = shift;
return int($value * 1000 + 0.5) / 1000;
}
sub formatSonyLatLong {
return undef unless defined $_[0];
my ($deg, $min, $sec) = split ":", shift;
return roundLatLong((shift ~~ [qw/S W/] ? "-": "") . ($deg + ($min / 60) + ($sec / 3600)));
}
sub formatIPhoneLatLong {
return undef unless defined $_[0];
my ($deg, $min, $sec, $direction) = shift =~ /^(\d+) deg (\d+)' ([\d.]+)" ([NWSE])$/;
return roundLatLong(($direction ~~ [qw/S W/] ? "-": "") . ($deg + ($min / 60) + ($sec / 3600)));
}
# memoize and rate limit
use constant LIMIT => 5; # delay between requests
my $last;
sub place {
my ($lat, $long) = @_;
return undef unless defined $lat && defined $long;
say "Looking up place ($lat, $long)";
state $places = {};
if (my $place = $places->{$lat}{$long}) {
say "Found $place (in cache)";
return $place;
}
my $sleep = $last ? LIMIT - time + $last : 0;
if ($sleep > 0) {
say "Waiting $sleep seconds to hit Geo API";
sleep $sleep;
}
$last = time;
my $res = decode_json $furl->get("https://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$long&sensor=true")->content;
return undef if $res->{status} eq "ZERO_RESULTS";
confess "Result not OK: " . Dumper $res unless $res->{status} eq "OK";
my $match = $res->{results}[0]{address_components};
confess "Failed to get match: " . Dumper $res unless $match;
my $neighborhood = first { $_->{types}[0] eq "neighborhood" } @$match;
my $locality = first { $_->{types}[0] eq "locality" } @$match;
my $sublocality = first { $_->{types}[1] eq "sublocality" } @$match;
my $area2 = first { $_->{types}[0] eq "administrative_area_level_2" } @$match;
$locality ||= $sublocality || $area2;
confess "Failed to get locality: " . Dumper $match unless $locality;
my $place = $places->{$lat}{$long} = $neighborhood ? "$neighborhood->{long_name} ($locality->{long_name})" : $locality->{long_name};
say "Found $place";
return $place;
}
sub sonyInfo {
my $info = shift;
my $records = extractRecords($info);
return {
date => formatDate($info->{CreationDateValue}),
latitude => formatSonyLatLong($records->{Latitude}, $records->{LatitudeRef}),
longitude => formatSonyLatLong($records->{Longitude}, $records->{LongitudeRef}),
}
}
sub canonInfo {
my $info = shift;
return {
date => formatDate($info->{MediaCreateDate}),
}
}
sub iPhoneInfo {
my $info = shift;
return {
date => formatDate($info->{CreationDate}),
latitude => formatIPhoneLatLong($info->{GPSLatitude}),
longitude => formatIPhoneLatLong($info->{GPSLongitude}),
}
}
sub generalInfo {
my $info = shift;
return {
date => formatDate($info->{CreationDate} // $info->{CreateDate} // $info->{DateTimeOriginal} // confess "Could not find date: " . Dumper $info),
latitude => formatIPhoneLatLong($info->{GPSLatitude}),
longitude => formatIPhoneLatLong($info->{GPSLongitude}),
}
}
sub prettyDate {
my ($date) = shift =~ /^(\d+-\d+-\d+T\d+:\d+):/;
return Time::Piece->strptime($date, "%Y-%m-%dT%H:%M")->strftime("%A, %B %e, %Y, %I:%M %p");
}
sub cameraInfo {
my $info = shift;
return sonyInfo($info) if ($info->{DeviceModelName} // "") eq "ILCE-6500";
return canonInfo($info) if ($info->{Model} // "") eq "Canon EOS 60D";
return iPhoneInfo($info) if ($info->{Model} // "") =~ /iPhone/;
return generalInfo($info);
}
sub targetExists {
my ($target, $file, $date, $sha) = @_;
my ($year, $day) = $date =~ /^(\d+)-(\d\d-\d\d)/;
my ($extension) = map { lc } $file =~ /.([\w]+)$/;
# find directory
my $directory = findDirectory($target, $year, $day);
return if !$directory;
my $targetFile = "$directory/$sha.$extension";
return unless -e $targetFile;
say "$targetFile already exists";
return 1;
}
sub extractInfo {
my $file = shift;
my $sha = sha256_hex($file);
my $info = { %{ImageInfo($file)}, PreviewImage => undef };
my $cameraInfo = cameraInfo($info);
# check if file exists before we hit google API
return if targetExists($target, $file, $cameraInfo->{date}, $sha);
my $place = place($cameraInfo->{latitude}, $cameraInfo->{longitude});
return {
%$cameraInfo,
sha => $sha,
place => $place,
}
}
sub findDirectory {
my ($target, $year, $day) = @_;
return unless -d "$target/$year";
opendir my $dir, "$target/$year" or confess "Failed to open directory: $!";
my ($directory) = first { m|^$day| } readdir $dir;
closedir $dir;
return $directory ? "$target/$year/$directory" : undef;
}
sub copy {
my ($file, $target, $info) = @_;
my ($year, $day) = $info->{date} =~ /^(\d+)-(\d\d-\d\d)/;
my ($extension) = map { lc } $file =~ /.([\w]+)$/;
# find directory
my $directory = findDirectory($target, $year, $day);
if (!$directory) {
$directory = $info->{place} ? "$target/$year/$day $info->{place}" : "$target/$year/$day";
make_path $directory;
}
elsif ($info->{place} && $directory !~ /\Q$info->{place}\E/) {
my $new = $directory =~ m|/\d{2}-\d{2}$| ? "$directory $info->{place}" : "$directory, $info->{place}";
rename $directory, $new;
$directory = $new;
}
my $destination = "$directory/$info->{sha}.$extension";
if (-e $destination) {
say "Target $destination exists";
return;
}
cp($file, $destination) || confess "Failed to copy $file → $destination: $!";
return $destination;
}
sub processFile {
my $file = shift;
my $target = shift;
my $info = extractInfo($file, $target) || return;
say "Processing $file";
# copy file to target
copy($file, $target, $info);
}