lib/iphoto_backup/cli.rb in iphoto_backup-0.0.1 vs lib/iphoto_backup/cli.rb in iphoto_backup-1.0.0

- old
+ new

@@ -4,21 +4,22 @@ module IphotoBackup class CLI < Thor IPHOTO_ALBUM_PATH = "~/Pictures/iPhoto Library.photolibrary/AlbumData.xml" DEFAULT_OUTPUT_DIRECTORY = "~/Google Drive/Dropbox" + IPHOTO_EPOCH = Time.new(2001, 1, 1) desc "export iPhoto albums", "exports iPhoto albums into target directory" option :filter, aliases: '-e', default: '.*' option :output, aliases: '-o', default: DEFAULT_OUTPUT_DIRECTORY + option :config, aliases: '-c', default: IPHOTO_ALBUM_PATH + option :'date-prefix', aliases: '-d', default: false def export each_album do |folder_name, album_info| say "\n\nProcessing Roll: #{folder_name}..." - album_images = value_for_dictionary_key('KeyList', album_info).css('string').map(&:content) - album_images.each do |image_id| - image_info = info_for_image image_id + each_image(album_info) do |image_info| source_path = value_for_dictionary_key('ImagePath', image_info).content target_path = File.join(File.expand_path(options[:output]), folder_name, File.basename(source_path)) target_dir = File.dirname target_path FileUtils.mkdir_p(target_dir) unless Dir.exists?(target_dir) @@ -39,18 +40,37 @@ def each_album(&block) albums = value_for_dictionary_key("List of Rolls").children.select {|n| n.name == 'dict' } albums.each do |album_info| folder_name = value_for_dictionary_key('RollName', album_info).content + if options[:'include-date-prefix'] && folder_name !~ /^\d{4}-\d{2}-\d{2} / + album_date = nil + each_image album_info do |image_info| + next if album_date + photo_interval = value_for_dictionary_key('DateAsTimerInterval', image_info).content.to_i + album_date = (IPHOTO_EPOCH + photo_interval).to_date + end + say "Automatically adding #{album_date} prefix to folder: #{folder_name}" + folder_name = "#{album_date} #{folder_name}" + end + if folder_name.match(album_filter) yield folder_name, album_info else say "\n\n#{folder_name} does not match the filter: #{album_filter.inspect}" end end end + def each_image(album_info, &block) + album_images = value_for_dictionary_key('KeyList', album_info).css('string').map(&:content) + album_images.each do |image_id| + image_info = info_for_image image_id + yield image_info + end + end + def info_for_image(image_id) value_for_dictionary_key image_id, master_images end def value_for_dictionary_key(key, dictionary = root_dictionary) @@ -76,10 +96,11 @@ @master_images ||= value_for_dictionary_key "Master Image List" end def root_dictionary @root_dictionary ||= begin - file = File.expand_path IPHOTO_ALBUM_PATH + file = File.expand_path options[:config] + say "Loading AlbumData: #{file}" doc = Nokogiri.XML(File.read(file)) doc.child.children.find {|n| n.name == 'dict' } end end end