-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiphoto.rb
executable file
·71 lines (56 loc) · 1.36 KB
/
iphoto.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
#!/usr/bin/env ruby
require 'fileutils'
require 'set'
class Image
attr_reader :date, :path
def initialize(date, path)
@date = date.to_i
@path = path
end
def <=>(x)
x.date <=> @date
end
def fname()
file = /[\w\.\s_\-\,\'\"]+$/.match(@path)
"#{@date} #{file}"
end
end
media = /
<key>DateAsTimerInterval<\/key>
<real>(.*)<\/real>
<key>ModDateAsTimerInterval<\/key>
<real>.*<\/real>
<key>MetaModDateAsTimerInterval<\/key>
<real>.*<\/real>
<key>ImagePath<\/key>
<string>(.*)<\/string>
/
destination = "."
if ARGV.length > 0
destination = ARGV[0]
abort "destination is invalid" unless File.directory?(destination)
end
libraryfile = File.expand_path("~") + "/Pictures/iPhoto\ Library/AlbumData.xml"
abort "unable to find iPhoto library" unless File.exists?(libraryfile)
library = File.open(libraryfile, "r").read
set = SortedSet.new()
library.scan(media).each do |record|
set.add(Image.new(record[0], record[1]))
end
puts "library scanned, %i files found" % set.length
count = 0
set.each do |item|
destfile = "#{destination}/#{item.fname}"
unless File.exists?(destfile)
begin
print "copying #{item.fname}... "
FileUtils.cp(item.path, destfile)
puts "done"
count += 1
rescue Errno::ENOSPC
FileUtils.rm(destfile)
puts "failed, out of space\ncopied #{count} files"
break
end
end
end