root/trunk/plagger/lib/Plagger/Plugin/Publish/iPhoto.pm

Revision 1924 (checked in by franck, 13 years ago)

r2174@franck-mbp: franck | 2007-02-27 21:16:14 +0100
add default tags to an entry
r2175@franck-mbp: franck | 2007-02-27 21:17:25 +0100
publish-iphoto deps
r2176@franck-mbp: franck | 2007-02-27 21:18:43 +0100
add images from enclosures to iPhoto
r2177@franck-mbp: franck | 2007-02-27 21:52:16 +0100
AddTags? (test & correction in .pm)
r2178@franck-mbp: franck | 2007-02-27 21:54:49 +0100

import to iphoto if there is no Mac
Glue

Line 
1 package Plagger::Plugin::Publish::iPhoto;
2
3 use strict;
4 use warnings;
5
6 use base qw( Plagger::Plugin );
7
8 sub register {
9     my ( $self, $context ) = @_;
10     $context->register_hook(
11         $self,
12         'plugin.init'  => \&initialize,
13         'publish.feed' => \&publish_feed,
14     );
15 }
16
17 sub initialize {
18     my ( $self, $c ) = @_;
19
20     unless ( exists $self->conf->{album} ) {
21         $c->error("'album' config is missing");
22     }
23
24     $self->{has_macglue} = eval {
25         require Mac::Glue;
26         $self->{iphoto} = new Mac::Glue 'iPhoto';
27         $self->{iphoto_album}
28             = $self->{iphoto}->obj( album => $self->conf->{album} );
29         1;
30     };
31 }
32
33 sub publish_feed {
34     my ( $self, $c, $args ) = @_;
35
36     my $feed = $args->{feed};
37     for my $entry ( $feed->entries ) {
38         for my $enclosure ( $entry->enclosures ) {
39             if ( $self->{has_macglue} ) {
40                 $c->log( info => "add via Mac::Glue" );
41
42                 # check if the album exists
43                 my $exists = $self->{iphoto}->exists( $self->{iphoto_album} );
44
45                 if ( $exists == 0 ) {
46                     $c->log(
47                         info => "Creating album " . $self->conf->{album} );
48                     $self->{iphoto}
49                         ->new_album( name => $self->conf->{album} );
50                 }
51
52                 # we import the file
53                 $c->log( info => "import " . $enclosure->filename );
54                 $self->{iphoto}->open( $enclosure->local_path );
55
56                 # we get the last added image
57                 my $last_roll = $self->{iphoto}->prop("last_rolls_album");
58                 my $select    = $self->{iphoto}->select($last_roll);
59
60                                                                 # are we importing ?
61                 my $imp = $self->{iphoto}->prop("importing");
62                 sleep(2) if $imp;
63
64                 # we count
65                 my $count = $select->prop("photos")->count();
66                 for ( my $index = $count; $index > 0; $index-- ) {
67                     my $photo = $select->obj( photo => 1 );
68                     my $name = $photo->prop("image_filename")->get;
69                     $c->log( info => "add $name to " . $self->conf->{album} );
70                     $self->{iphoto}
71                         ->add( $photo, to => $self->{iphoto_album} );
72                 }
73             }
74             else {
75                 $c->log( info => "add via applescript" );
76                 system( 'osascript', 'bin/import_to_iphoto.scpt',
77                     $enclosure->local_path, $self->conf->{album} ) == 0
78                     or $c->error(
79                     error => 'Could not add ' . $enclosure->filename );
80             }
81
82         }
83     }
84 }
85
86 1;
87
88 __END__
89
90 =head1 NAME
91
92 Plagger::Plugin::Publish::iPhoto - Store images in iPhoto
93
94 =head1 SYNOPSIS
95
96   - module: Publish::iPhoto
97                 config:
98                 album: plagger
99
100 =head1 DESCRIPTION
101
102 store photos from enclosure to iPhoto. You need to create a glue for iphoto:
103
104         gluemac /Application/iPhoto.app/
105
106 =head1 AUTHOR
107
108 Franck Cuny
109
110 =head1 SEE ALSO
111
112 L<Plagger>
113
114 =cut
115
Note: See TracBrowser for help on using the browser.