Skip to content

Instantly share code, notes, and snippets.

@wilik16
Created June 19, 2023 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wilik16/ce6f3e96b90fd459da661c4e22be3c91 to your computer and use it in GitHub Desktop.
Save wilik16/ce6f3e96b90fd459da661c4e22be3c91 to your computer and use it in GitHub Desktop.
Ruby script to change modified date of iCloud Photos downloaded from icloud.com so that it's easier to be sorted.
# frozen_string_literal: true
require 'csv'
require 'time'
require 'fileutils'
# find all folders starting with "iCloud Photos"
folders = Dir.foreach('.').select do |e|
/^iCloud Photos/.match(e)
end
# create Folders
FileUtils.mkdir_p 'Photos'
FileUtils.mkdir_p 'Favorites'
FileUtils.mkdir_p 'Deleted'
FileUtils.mkdir_p 'Albums'
FileUtils.mkdir_p 'Memories'
def iterate_folder(folder_name)
# find all csv files inside "Photos" folder
csv_file_names = Dir.new("#{folder_name}/Photos").entries.select do |e|
/^Photo Details(?:-[0-9])?\.csv$/.match(e)
end
csv_file_names.each do |csv_file_name|
iterate_photos_csv(folder_name, csv_file_name)
end
move_other_csv(folder_name)
end
def iterate_photos_csv(folder_name, csv_file_name)
csv = CSV.parse(File.read("#{folder_name}/Photos/#{csv_file_name}"), headers: true)
csv.each do |row|
image_name = row['imgName']
original_creation_date = row['originalCreationDate']
favorite = row['favorite']
deleted = row['deleted']
media_file_path = if deleted == 'yes'
"#{folder_name}/Recently Deleted/#{image_name}"
else
"#{folder_name}/Photos/#{image_name}"
end
unless File.exist?(media_file_path)
File.write('skipped_not_exist.txt', "#{media_file_path}\n", mode: 'a+')
puts "skipped, not exist : #{image_name}"
next
end
# change creation and modified time
new_date = Time.strptime(original_creation_date, '%A %B %e,%Y %l:%M %p %z')
File.utime(new_date, new_date, media_file_path)
# organize files into its own folder
if favorite == 'yes'
FileUtils.mv(media_file_path, "Favorites/#{image_name}")
elsif deleted == 'yes'
FileUtils.mv(media_file_path, "Deleted/#{image_name}")
else
FileUtils.mv(media_file_path, "Photos/#{image_name}")
end
end
end
def move_other_csv(folder_name)
if File.exist?("#{folder_name}/Albums")
albums_csv = Dir.new("#{folder_name}/Albums").entries.select do |e|
/^.*\.csv$/.match(e)
end
albums_csv.each do |csv|
FileUtils.mv("#{folder_name}/Albums/#{csv}", "Albums/#{csv}")
end
end
if File.exist?("#{folder_name}/Memories")
albums_csv = Dir.new("#{folder_name}/Memories").entries.select do |e|
/^.*\.csv$/.match(e)
end
albums_csv.each do |csv|
FileUtils.mv("#{folder_name}/Memories/#{csv}", "Memories/#{csv}")
end
end
end
folders.each do |folder_name|
iterate_folder(folder_name)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment