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