iphoto2 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/iphoto2.rb +117 -0
  2. data/test_iphoto2.rb +46 -0
  3. metadata +51 -0
data/iphoto2.rb ADDED
@@ -0,0 +1,117 @@
1
+ require 'plist'
2
+
3
+ class IPhoto2
4
+ def initialize( filename="#{ENV['HOME']}/Pictures/iPhoto Library/AlbumData.xml" )
5
+ @plist = Plist.parse_xml( filename )
6
+ @images = Hash.new
7
+ end
8
+
9
+ def library
10
+ @library ||= Album.new( @plist["List of Albums"].find() do |a|
11
+ a["Master"]
12
+ end, self )
13
+ end
14
+
15
+ def last_roll
16
+ @last_roll ||= Album.new( @plist["List of Albums"].find() do |a|
17
+ a["AlbumName"] == "Last Roll"
18
+ end, self )
19
+ end
20
+
21
+ def last_month
22
+ @last_month ||= Album.new( @plist["List of Albums"].find() do |a|
23
+ a["AlbumName"] == "Last 12 Months"
24
+ end, self )
25
+ end
26
+
27
+ def rolls
28
+ @rolls ||= @plist["List of Rolls"].collect do |roll|
29
+ Album.new(roll, self)
30
+ end
31
+ end
32
+
33
+ def albums
34
+ @albums ||= @plist["List of Albums"].find_all() do |a|
35
+ a["Album Type"] == "Regular"
36
+ end.collect do |a|
37
+ Album.new( a, self )
38
+ end
39
+ end
40
+
41
+ def get_image( image_id )
42
+ @images[image_id] ||= Image.new(@plist["Master Image List"][image_id.to_s], self)
43
+ end
44
+
45
+ def path
46
+ @plist["Archive Path"]
47
+ end
48
+
49
+ end
50
+
51
+ class Album
52
+ attr_accessor :plist
53
+ include Enumerable
54
+
55
+ def initialize( plist, iphoto )
56
+ @plist = plist
57
+ @iphoto = iphoto
58
+ end
59
+
60
+ def album_id
61
+ @plist["AlbumId"]
62
+ end
63
+
64
+ def name
65
+ @plist["AlbumName"]
66
+ end
67
+
68
+ def images
69
+ @images ||= @plist["KeyList"].collect do |image_id|
70
+ @iphoto.get_image(image_id)
71
+ end
72
+ end
73
+
74
+ def [] key
75
+ images[key]
76
+ end
77
+
78
+ def size
79
+ images.size
80
+ end
81
+
82
+ def each
83
+ @images.each do |i|
84
+ yield i
85
+ end
86
+ end
87
+
88
+ end
89
+
90
+ class Image
91
+ attr_accessor :plist
92
+ def initialize( plist, iphoto )
93
+ @plist = plist
94
+ @iphoto = iphoto
95
+ end
96
+
97
+ def path
98
+ @plist["ImagePath"]
99
+ end
100
+
101
+ def thumb
102
+ @plist["ThumbPath"]
103
+ end
104
+
105
+ def caption
106
+ @plist["Caption"]
107
+ end
108
+
109
+ def comment
110
+ @plist["Comment"]
111
+ end
112
+
113
+ def date
114
+ @date ||= Time.at( Time.mktime( 2001, 1, 1 ).to_i + @plist["DateAsTimerInterval"].to_i )
115
+ end
116
+
117
+ end
data/test_iphoto2.rb ADDED
@@ -0,0 +1,46 @@
1
+ require 'test/unit'
2
+ require 'pp'
3
+
4
+ require 'iphoto2'
5
+
6
+ class TestIPhoto2 < Test::Unit::TestCase
7
+ def test_album_data
8
+ iphoto = IPhoto2.new( "AlbumData.xml" )
9
+
10
+ assert_equal( "/Users/username/Pictures/iPhoto Library", iphoto.path )
11
+
12
+ assert_kind_of( Album, iphoto.library )
13
+ assert_equal( 999000, iphoto.library.album_id )
14
+ assert_kind_of( Album, iphoto.last_roll )
15
+ assert_equal( 999001, iphoto.last_roll.album_id )
16
+ assert_kind_of( Album, iphoto.last_month )
17
+ assert_equal( 999002, iphoto.last_month.album_id )
18
+
19
+ assert_kind_of( Array, iphoto.rolls )
20
+ assert_equal( 1, iphoto.rolls.size )
21
+ assert_equal( 6, iphoto.rolls.first.album_id )
22
+ assert_kind_of( Array, iphoto.albums )
23
+ assert_equal( 1, iphoto.albums.size )
24
+ assert_equal( 9, iphoto.albums.first.album_id )
25
+
26
+ album = iphoto.albums.first
27
+ assert_equal( "An Album", album.name )
28
+ assert_equal( 1, album.size )
29
+ assert_kind_of( Enumerable, album )
30
+
31
+ image = album[0]
32
+ assert_equal( "/Users/username/Pictures/iPhoto Library/2006/01/07/fallow_keep.png.450x450.2005-12-04.jpg",
33
+ image.path )
34
+ assert_equal( "/Users/username/Pictures/iPhoto Library/2006/01/07/Thumbs/7.jpg",
35
+ image.thumb )
36
+ assert_equal( "fallow_keep.png.450x450.2005-12-04",
37
+ image.caption )
38
+ assert_equal( "a comment",
39
+ image.comment )
40
+ assert_equal( "Sat Jan 07 15:43:09 EST 2006",
41
+ image.date.to_s )
42
+ end
43
+
44
+ end
45
+
46
+ __END__
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11.3
3
+ specification_version: 1
4
+ name: iphoto2
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.0
7
+ date: 2006-01-08 00:00:00 -05:00
8
+ summary: iphoto contains methods to parse and access the contents of the iPhoto pictures.
9
+ require_paths:
10
+ - "."
11
+ email: plist@hexane.org
12
+ homepage: http://www.narf-lib.org
13
+ rubyforge_project: narf
14
+ description: ''
15
+ autorequire: iphoto2
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ -
22
+ - ">"
23
+ - !ruby/object:Gem::Version
24
+ version: 0.0.0
25
+ version:
26
+ platform: ruby
27
+ signing_key:
28
+ cert_chain:
29
+ authors:
30
+ - Patrick May
31
+ files:
32
+ - iphoto2.rb
33
+ - test_iphoto2.rb
34
+ test_files:
35
+ - test_iphoto2.rb
36
+ rdoc_options: []
37
+ extra_rdoc_files: []
38
+ executables: []
39
+ extensions: []
40
+ requirements: []
41
+ dependencies:
42
+ - !ruby/object:Gem::Dependency
43
+ name: plist
44
+ version_requirement:
45
+ version_requirements: !ruby/object:Gem::Version::Requirement
46
+ requirements:
47
+ -
48
+ - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 1.0.0
51
+ version: