iPhoto.pm untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 2 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 68 CC 2 SCAR 11.0
CC
2
Cov
0%
parse
6.0
new
2.0
DESTROY
2.0
CRAP 3.0
1 # $Id: iPhoto.pm,v 1.4 2003/12/04 04:16:42 dk99034 Exp $
2 #
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 #
17 package Mac::iPhoto;
18 #
19 use Mac::PropertyList;
20 use FileHandle;
21 use strict;
22  
23 use vars qw /$VERSION/;
24  
25 $VERSION = "0.1";
26  
27 =head1 NAME
28  
29 iPhoto - reads in photo albums plist files of iPhoto.app on MacOS X
30 and presents them as perl data structure.
31  
32 =head1 SYNOPSIS
33  
34 use Mac::iPhoto;
35  
36 my $a = new Mac::iPhoto("./AlbumData.xml");
37  
38 $a->parse;
39  
40 =head1 DESCRIPTION
41  
42 Reads in photo albums plist files of iPhoto.app on MacOS X and
43 presents them as perl data structure.
44  
45 =head1 FUNCTIONS
46  
47 =head2 &new();
48  
49 my $AlbumData = Mac::iPhoto->new( "/Users/Shared/Photo/AlbumData.xml" );
50  
51 Creates new data object. Takes one parameter - UNIX path to
52 AlbumData.xml file.
53  
54  
55 =head2 &parse()
56  
57 $AlbumData->&parse();
58  
59 Parses XML file and populates data structure $AlbumData->{'Data'}.
60  
61 =head1 Structure of the data format of iPhoto.pm
62  
63 Mac::iPhoto->parse() populates a hash iPhoto->{Data} that has following
64 structure:
65  
66 {
67 Properties => \%Properties,
68 Albums => \@Albums,
69 Images => \@Images
70 }
71  
72 =head2 %Properties is hash of the top-level properties in the AlbumData.xml file.
73  
74 %Properties has following structure:
75  
76 {
77 'Application Version' => string,
78 'Archive Path' => string,
79 'Major Version' => string,
80 'Minor Version' => string,
81 }
82  
83 =head2 @Albums - array describing all albums in parsed XML file.
84  
85 @Albums has following structure:
86  
87 {
88 'AlbumName' => string,
89 'BookDesignName' => string,
90 'RepeatSlideShow' => string,
91 'SecondsPerSlide' => string,
92 'SongPath' => string,
93 'KeyList' => \@KeyList,
94 }
95  
96 =head2 @Images - array describing all images in the AlbumData.xml file.
97  
98 @Images has following structure:
99  
100 (
101 'ImagePath' => string,
102 'Caption' => string,
103 'Comment' => string,
104 'Date' => string,
105 'ThumbPath' => string,
106 'ModificationDate' => string,
107 );
108  
109 =head1 CHANGES
110  
111 Nov 19, 2003 - 1st actually working version of module.
112  
113 =head1 EXAMPLE
114  
115 use Mac::iPhoto;
116  
117 my $a = new Mac::iPhoto("./AlbumData.xml");
118  
119 $a->parse;
120  
121 printf "Created by iTunes v. %s / maj.%s / min.%s\n",
122 $a->{'Properties'}->{'Application Version'},
123 $a->{Properties}->{'Major Version'},
124 $a->{Properties}->{'Minor Version'};
125  
126 printf "Album path: %s\n", $a->{Properties}->{'Archive Path'};
127  
128 for my $album (@{$a->{Data}->{Albums}}) {
129  
130 printf "Name: %s \n", $album->{'AlbumName'};
131 printf "BookDesignName: %s \n", $album->{'BookDesignName'};
132 for my $key ( @{$album->{'KeyList'}}) {
133 print $key, ": \n";
134 printf "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n",
135 $a->{Data}->{Images}->[$key]->{'Date'},
136 $a->{Data}->{Images}->[$key]->{'ImagePath'},
137 $a->{Data}->{Images}->[$key]->{'ThumbPath'},
138 $a->{Data}->{Images}->[$key]->{'Caption'},
139 $a->{Data}->{Images}->[$key]->{'Comment'},
140 $a->{Data}->{Images}->[$key]->{'ModificationDate'};
141 }
142 }
143  
144 =head1 CREDITS
145  
146 Mac::iPhoto relies on Mac::PropertyList module for actual parsing of
147 XML file. Thanks for Brian D Foy, <bdfoy@cpan.org> for developing it.
148  
149 =head1 AUTHOR
150  
151 Dmytro Kovalov, 2003. kov at tokyo dot email dot ne dot jp.
152  
153 http://yarylo.sytes.net/
154 http://www.asahi-net.or.jp/~as9d-kvlv/
155  
156 =head1 SEE ALSO
157  
158 https://sourceforge.net/projects/brian-d-foy/
159  
160 =cut
161  
162 sub new {
163 my($class) = shift;
164 my $self = {};
165 bless($self, $class);
166 $self->{AlbumDataFile} = shift;
167 return $self;
168 }
169  
170 sub DESTROY {
171 my $self = shift;
172 $self->{AlbumDataFile}->close();
173 }
174  
175  
176 sub parse () {
177 my $self = shift;
178 my $AlbumData_file = $self->{AlbumDataFile};
179 my $AlbumData = new FileHandle ;
180 die "Can not open file $AlbumData_file for reading: $!\n" unless $AlbumData->open("< $AlbumData_file");
181 my @Albums;
182 my $save_SLASH = $/; undef $/;
183 my $txt = <$AlbumData>;
184 my $data = Mac::PropertyList::parse_plist( $txt );
185 my %Properties =
186 (
187 'Application Version' => $data->{value}->{'Application Version'}->{value},
188 'Archive Path' => $data->{value}->{'Archive Path'}->{value},
189 'Major Version' => $data->{value}->{'Major Version'}->{value},
190 'Minor Version' => $data->{value}->{'Minor Version'}->{value},
191 );
192 #
193 # all albums
194 #
195 my @data_albums = $data->{value}->{'List of Albums'}->{value};
196 #
197 # rotate trhough albums
198 #
199 for my $l (@data_albums) {
200 for my $data_album (@{$l}) {
201 # Album properties:
202 # $album->{type} : dict
203 #
204 # Keys:
205 # ~~~~~
206 # $album->{value}->{'KeyList'}->{'value'}
207 # array values: $album->{value}->{'KeyList'}->{'value'}->{'value'}
208 my @KeyList = ();
209 for my $key (my @keys = $data_album->{value}->{'KeyList'}->{'value'}) {
210 for my $key2 ( @{$key}) {
211 # push @albumKeys, $key2->{'value'};
212 # push $albumsHash{$AlbumName}->{Photos} = \@albumKeys;
213 push @KeyList, $key2->{'value'};
214 }
215 }
216 ;
217 my %album =
218 (
219 'AlbumName' => $data_album->{value}->{'AlbumName'}->{value},
220 'BookDesignName' => $data_album->{value}->{'BookDesignName'}->{value},
221 'RepeatSlideShow' => $data_album->{value}->{'RepeatSlideShow'}->{value},
222 'SecondsPerSlide' => $data_album->{value}->{'SecondsPerSlide'}->{value},
223 'SongPath' => $data_album->{value}->{'SongPath'}->{value},
224 'KeyList' => \@KeyList,
225 );
226 push @Albums, \%album;
227 }
228 }
229 #
230 # Images:
231 # ~~~~~~~
232 my @Images = ();
233 for my $image (sort keys %{$data->{value}->{'Master Image List'}->{'value'}}) {
234 #
235 # Caption Comment Date ImagePath ModificationDate ThumbPath
236 #
237 my %image =
238 (
239 'ImagePath' => %{$data->{value}->{'Master Image List'}->{'value'}}->{$image}->{'value'}->{'ImagePath'}->{'value'},
240 'Caption' => %{$data->{value}->{'Master Image List'}->{'value'}}->{$image}->{'value'}->{'Caption'}->{'value'},
241 'Comment' => %{$data->{value}->{'Master Image List'}->{'value'}}->{$image}->{'value'}->{'Comment'}->{'value'},
242 'Date' => %{$data->{value}->{'Master Image List'}->{'value'}}->{$image}->{'value'}->{'Date'}->{'value'},
243 'ThumbPath' => %{$data->{value}->{'Master Image List'}->{'value'}}->{$image}->{'value'}->{'ThumbPath'}->{'value'},
244 'ModificationDate' => %{$data->{value}->{'Master Image List'}->{'value'}}->{$image}->{'value'}->{'ModificationDate'}->{'value'},
245 );
246 @Images[$image] = \%image;
247 }
248 $/ = $save_SLASH; # restore $/
249 my %AlbumData =
250 (
251 Albums => \@Albums,
252 Images => \@Images,
253 );
254  
255 $self->{Data} = \%AlbumData;
256 $self->{Properties} = \%Properties;
257 }
258  
259 # ============================================================
260 # END
261 #
262 1;