photos.pl: cleanup.
[sharing-cli] / scripts / photos.pl
1 use strict;
2 use vars qw($VERSION %IRSSI);
3
4 use File::stat;
5 use Time::localtime;
6
7 use Irssi qw(
8     command_bind settings_get_str settings_add_str timeout_add server_find_chatnet
9 );
10
11 $VERSION = '1.00';
12 %IRSSI = (
13     authors     => 'Tuomas Kulve',
14     contact     => 'tuomas@kulve.fi',
15     name        => 'Print URLs for new photos',
16     description => 'This script polls for new images' .
17                    'and prints their URLs and meta information' .
18                    'to IRC',
19     license     => 'Public Domain',
20 );
21
22
23 sub handle_photo {
24     my ($dir_in, $dir_public, $file) = @_;
25
26     if (!rename("$dir_in/$file", "$dir_public/$file")) {
27            print "Failed to rename $dir_in/$file to $dir_public/$file: $!";
28            return;
29     }
30
31     my $filename = "$dir_public/$file";
32
33     if (! -f "/usr/bin/exiftool") {
34         print "No exiftool, cannot get image details";
35         return;
36     }
37
38     my $cmd = "exiftool -f -s -s -s -t -c %.6f -ObjectName -Description -GPSPosition -Subject $filename";
39
40     my $msg ="";
41     my $exifmsg ="";
42
43     # Add EXIF info to IRC message
44     if (!open(EXIF, "$cmd |")) {
45         print "Failed to run exiftool: $!";
46     } else {
47
48       my $exifout = <EXIF>;
49       chomp $exifout;
50
51       close(EXIF);
52
53       my @data = split("\t", $exifout);
54
55
56       # Title
57       if ($data[0] ne "-") {
58           $exifmsg .= "$data[0]: "
59       }
60
61       # Description
62       if ($data[1] ne "-") {
63           $exifmsg .= "$data[1]. "
64       }
65
66       #  Tags
67       if ($data[3] ne "-") {
68           $exifmsg .= "\[$data[3]\] "
69       }
70
71       #  GPS Location
72       if ($data[2] ne "-") {
73           $exifmsg .= "($data[2]) "
74       }
75         }
76
77         $msg = $exifmsg;
78
79         # Append public http URL
80     my $url_prefix = settings_get_str('photos_url_prefix');
81     $msg .= "$url_prefix/$file";
82
83         # Send IRC message to appropriate channel
84     my $networkname = settings_get_str('photos_notify_server_name');
85     my $ircserver = server_find_chatnet($networkname);
86     if (defined $ircserver) {
87         my $channel  = settings_get_str('photos_notify_channel');
88         $ircserver->command("/msg -channel $channel $msg");
89     } else {
90           print "Failed to find server '$ircserver' in network '$network'";
91           return
92         }
93 }
94
95 sub timeouttest {
96     my ($data) = @_;
97
98     my $dir_in     = settings_get_str('photos_dir_incoming');
99     my $dir_public = settings_get_str('photos_dir_public');
100
101     if (! -d $dir_in) {
102         print "Invalid incoming photos dir: $dir_in";
103         return;
104     }
105
106     if (! -d $dir_public) {
107         print "Invalid public photos dir: $dir_public";
108         return;
109     }
110
111     if (!opendir(DIR, $dir_in)) {
112         print "Can't opendir $dir_in: $!";
113         return;
114     }
115
116     my $file;
117     while (defined ($file = readdir(DIR))) {
118         my $filename = "$dir_in/$file";
119
120         # TODO: what about videos? Or other non-image files.
121         if (-f "$filename") {
122             my $now = time();
123             my $date_str = stat($filename)->mtime;
124             my $diff = $now - $date_str;
125
126             # Try to ensure that the whole image is there instead of moving
127             # a file that's still being copied.
128             if ($diff > 60) {
129                 handle_photo($dir_in, $dir_public, $file);
130             }
131         }
132     }
133
134     closedir(DIR);
135 }
136
137
138 # Edit this to match your command line in the sharing-cli plugin
139 settings_add_str('photos', 'photos_dir_incoming',
140                  "$ENV{HOME}/photos_incoming");
141 # Edit this to be your public www-directory name
142 settings_add_str('photos', 'photos_dir_public',
143                  '$ENV{HOME}/~public_html/');
144 # Edit this to match your IRC network name
145 settings_add_str('photos', 'photos_notify_server_name',
146                                  'NAME_OF_YOUR_IRC_NETWORK');
147 # Edit this to match your IRC channel
148 settings_add_str('photos', 'photos_notify_channel',
149                                  '#CHANNEL_NAME_HERE');
150 # Edit this to match your www address
151 settings_add_str('photos', 'photos_url_prefix',
152                                  "http://WWW.DOMAIN.COM/~$ENV{USER}/");
153
154 # Let's poll for new images every 30 secs
155 timeout_add(30 * 1000, "timeouttest", "");