forked from dmytro/Web-iPhoto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiphoto.rb
executable file
·164 lines (146 loc) · 5.38 KB
/
wiphoto.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env ruby
require 'json'
require 'optparse'
require 'ostruct'
require 'sqlite3'
require 'yaml'
module WIPhoto
class WIPhoto
attr_accessor :config, :options, :parents
def WIPhoto.main(argv)
options = OpenStruct.new
options.iPhotoLib = File.expand_path(File.join("~","Pictures","iPhoto Library"))
options.install = false
options.data = false
options.mkzip = false
opts = OptionParser.new do |opts|
opts.separator ""
opts.banner = "Usage: #{File.basename __FILE__} [options]"
opts.separator ""
opts.on('-v', '--version', "Print version") { puts "Version: #{version}" ; exit 0 }
opts.on('--do x,y,z', Array,
"Script action: ",
" install: Install wiPhoto files in iPhoto Library",
" data: Create wiPhoto data files in iPhoto Library",
" mkzip: Create installation ZIP file",
" all: all of the actions above"
) { |list|
list.each { |i|
options.install = true if i == 'install' or i == 'all'
options.data = true if i == 'data' or i == 'all'
options.mkzip = true if i == 'mkzip' or i == 'all'
}
}
opts.on('-D', '--dir DIR', String, "Path to iPhotoLibrary folder. (~/Pictures/iPhoto Library by default.)") {
|d| options.iPhotoLib = d }
opts.on_tail("-h", "--help", "Show this message") { puts opts; exit }
end
opts.parse!(argv)
unless options.install or options.data or options.mkzip then
puts "\nPlease specify one of install, data or mkzip with --do option\n\n";
puts opts.to_s
exit
end
iphoto = WIPhoto.new options.iPhotoLib
iphoto.options = options
iphoto.mkzip if options.mkzip
iphoto.install if options.install
iphoto.data if options.data
end
def initialize iPhotoLib
@dbpath = File.join iPhotoLib, "iPhotoMain.db"
@me = File.basename(__FILE__)
@rundir = File.dirname(__FILE__)
@datadir = File.join iPhotoLib,"wiphoto/js"
@db = SQLite3::Database.new @dbpath rescue raise "Cannot open iPhoto database file #{@dbpath}"
@db.type_translation = true
@config = YAML::load_file File.join( @rundir, 'config.yml')
@albums = {} #
@photos = {} # { key: { path_to_image, path_to_thumb}}
@parents = {}
end
def tree
@db.execute(@config['albums']['tree']['top']).each {|x|
@parents[x[0]] = children x[0]
}
parents
end
def albums
@db.execute(@config['albums']['names']).each { |primaryKey,name,parent,type|
@albums[primaryKey] = {}
@albums[primaryKey]['name'] = name
@albums[primaryKey]['type'] = type
@albums[primaryKey]['parent'] = parent
@albums[primaryKey]['photos'] = []
@db.execute(eval '"'+@config['albums']['photos']+'"').each { |photo|
@albums[primaryKey]['photos'] << photo[0]
}
}
@albums
end
def photos
photos = []
@albums.keys.each { |albumid|
photos.concat @albums[albumid]['photos']
}
photos.uniq.each { |photoKey|
@photos[photoKey] = {}
['thumb','image'].each { |l|
@photos[photoKey][l] = {}
select = eval '"' + @config['photos'][l] +'"'
@photos[photoKey][l]['path'] = @db.execute(eval '"'+@config['photos']['path'] +'"')[0][0]
@photos[photoKey][l]['dim'] = @db.execute(eval '"'+@config['photos']['dimensions'] +'"')[0][0..1]
}
}
@photos
end
def install
iPhotoLib = @options.iPhotoLib
pkg = File.join(File.expand_path(File.dirname(__FILE__)), @config['install']['zip'])
unless File.exists? pkg
puts "\n\n#{pkg} file does not exists"
puts "Maybe you need to run #{@me} --do mkzip first?\n\n"
exit 2
end
system("cd '#{iPhotoLib}'; unzip -o #{pkg}") rescue raise "Can not unzip distribution file #{pkg}: #{$!}"
File.symlink("#{iPhotoLib}/wiphoto/index.html",iPhotoLib) unless File.exists? iPhotoLib
end
def data
@parents = tree
@albums = albums
@photos = photos
iPhotoLib = @options.iPhotoLib
unless File.directory? @datadir
puts "\n\n#{@datadir} directory does not exists"
puts "Maybe you need to run #{@me} --do install first?\n\n"
exit 1
end
File.open(File.join(@datadir,"albums.js"), 'w'){|f|
f.print "tree = #{@parents.to_json};\n"
f.print "albums = #{@albums.to_json};\n"
}
File.open(File.join(@datadir,"photos.js"), "w"){|f| f.print "photos= " + @photos.to_json + ";\n" }
end
def mkzip
# p (['zip', '-r',
# @config['install']['zip'], @config['install']['files'],
# '-x', @config['install']['exclude']
# ].join(' '))
system (['zip', '-r',
@config['install']['zip'], @config['install']['files'],
'-x', @config['install']['exclude']
].join(' '))
end
private
def children(id)
out = {}
@db.execute(eval('"'+@config['albums']['tree']['nested']+'"')).each { |x|
out[x[0]] = {}
}
out.keys.each { |x| out[x] = children x }
out
end
end
end
# ----------------------------------------------------------
WIPhoto::WIPhoto.main(ARGV)