-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimport.rb
82 lines (65 loc) · 2.36 KB
/
import.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
require 'json'
require "erb"
require "date"
require 'optparse'
require './src/ruby/lib/photos_scripts'
ARGV << '-h' if ARGV.empty?
CONFIG = File.open("CONFIG.json", "r") {|f| JSON.parse(f.read) }
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: bin/run import [-a] [-n COUNT] [-x]\nimport the prepared data into Photos.app; will pick up where it left off on successive runs"
opts.on("-n COUNT", "--max-photos", "Stop import after COUNT photos (so you can inspect)") do |count|
options[:max_count] = count.to_i
end
opts.on("-a", "--all", "Import all photos without stopping") do
options[:all] = true
end
opts.on("-r", "--restart", "Reset progress data (next run will start over again with the first photo)") do
options[:restart] = true
end
end.parse!
unless options.key?(:max_count) || options.key?(:all)
STDERR.puts "Error: you must specify either -n or -a; use -h for help"
exit -1
end
imported_shas_path = File.join("out", "imported_shas")
if options[:restart] && File.exists?(imported_shas_path)
File.delete(imported_shas_path)
end
imported_shas = {}
if File.exists?(imported_shas_path)
File.open(imported_shas_path, "r") do |shafile|
shafile.read.split("\n")
.filter {|sha| sha != "" }
.each {|sha| imported_shas[sha] = true }
end
end
puts "creating folder '#{CONFIG['photos_folder_name']}' in Photos.app (if needed)..."
PhotosScripts::create_folder_if_needed(CONFIG['photos_folder_name'])
shalog = File.open(imported_shas_path, "a")
import_data = File.open(File.join("out", "import_data.json"), "r") {|f| JSON.parse(f.read) }
skip_count = 0
stop_idx = options[:max_count]
import_data.each_with_index do |datum, idx|
if ! options[:all] && idx >= stop_idx
puts "stopping after #{options[:max_count]} files (use -a to import all photos)"
break
end
if imported_shas.key?(datum["sha"])
skip_count += 1
stop_idx += 1 unless options[:all]
next
end
if skip_count > 0
puts "... skipped #{skip_count} already imported photos ..."
skip_count = 0
end
print "#{idx+1}: importing photo: #{datum['selected_photo']}"
STDOUT.flush
PhotosScripts::import_photo(datum)
shalog.puts datum["sha"]
puts " [DONE]"
end
if skip_count > 0
puts "... skipped #{skip_count} already imported photos ..."
end