]> git.sesse.net Git - pr0n/blob - perl/update-image-info.pl
Allow () in filenames.
[pr0n] / perl / update-image-info.pl
1 #! /usr/bin/perl
2 use lib qw(.);
3 use DBI;
4 use POSIX;
5 use Image::ExifTool;
6 use Encode;
7 use strict;
8 use warnings;
9
10 use Sesse::pr0n::Config;
11 eval {
12         require Sesse::pr0n::Config_local;
13 };
14         
15 my $dbh = DBI->connect("dbi:Pg:dbname=pr0n;host=" . $Sesse::pr0n::Config::db_host,
16         $Sesse::pr0n::Config::db_username, $Sesse::pr0n::Config::db_password)
17         or die "Couldn't connect to PostgreSQL database: " . DBI->errstr;
18 $dbh->{RaiseError} = 1;
19
20 my $q = $dbh->prepare('SELECT id FROM images WHERE id NOT IN ( SELECT DISTINCT image FROM exif_info ) ORDER BY id');
21 $q->execute;
22
23 while (my $ref = $q->fetchrow_hashref) {
24         my $id = $ref->{'id'};
25
26         # Copied almost verbatim from Sesse::pr0n::Common::update_image_info
27         my $info = Image::ExifTool::ImageInfo(get_disk_location($id));
28         my $width = $info->{'ImageWidth'} || -1;
29         my $height = $info->{'ImageHeight'} || -1;
30         my $datetime = undef;
31                         
32         if (defined($info->{'DateTimeOriginal'})) {
33                 # Parse the date and time over to ISO format
34                 if ($info->{'DateTimeOriginal'} =~ /^(\d{4}):(\d\d):(\d\d) (\d\d):(\d\d):(\d\d)(?:\+\d\d:\d\d)?$/ && $1 > 1990) {
35                         $datetime = "$1-$2-$3 $4:$5:$6";
36                 }
37         }
38
39         {
40                 local $dbh->{AutoCommit} = 0;
41
42                 $dbh->do('UPDATE images SET width=?, height=?, date=? WHERE id=?',
43                          undef, $width, $height, $datetime, $id)
44                         or die "Couldn't update width/height in SQL: $!";
45
46                 $dbh->do('DELETE FROM exif_info WHERE image=?',
47                         undef, $id)
48                         or die "Couldn't delete old EXIF information in SQL: $!";
49
50                 my $q = $dbh->prepare('INSERT INTO exif_info (image,tag,value) VALUES (?,?,?)')
51                         or die "Couldn't prepare inserting EXIF information: $!";
52
53                 for my $key (keys %$info) {
54                         next if ref $info->{$key};
55                         $q->execute($id, $key, guess_charset($info->{$key}))
56                                 or die "Couldn't insert EXIF information in database: $!";
57                 }
58
59                 # update the last_picture cache as well (this should of course be done
60                 # via a trigger, but this is less complicated :-) )
61                 $dbh->do('UPDATE last_picture_cache SET last_picture=GREATEST(last_picture, ?) WHERE event=(SELECT event FROM images WHERE id=?)',
62                         undef, $datetime, $id)
63                         or die "Couldn't update last_picture in SQL: $!";
64         }
65
66         print "Updated $id.\n";
67 }
68
69 sub get_disk_location {
70         my ($id) = @_;
71         my $dir = POSIX::floor($id / 256);
72         return "/srv/pr0n.sesse.net/images/$dir/$id.jpg";
73 }
74
75 sub guess_charset {
76         my $text = shift;
77         my $decoded;
78
79         eval {
80                 $decoded = Encode::decode("utf-8", $text, Encode::FB_CROAK);
81         };
82         if ($@) {
83                 $decoded = Encode::decode("iso8859-1", $text);
84         }
85
86         return $decoded;
87 }
88