Last active
March 7, 2024 14:58
-
-
Save celadevra/96001b28dd955b9c68b480fed503debe to your computer and use it in GitHub Desktop.
Import photos to NAS, and put them into YYYY/MM folders
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'date' | |
require 'exif' # in turn requires libexif | |
require 'fileutils' | |
require 'json' | |
SOURCE = '/Volumes/home/Drive/GDrive/Takeout/Google Photos' | |
DEST = '/Volumes/home/Photos/PhotoLibrary' | |
def main | |
traverse(SOURCE) | |
end | |
def traverse(dir_or_file) | |
Dir.each_child(dir_or_file) do |x| | |
loc = dir_or_file + "/" + x | |
if File.directory?(loc) | |
traverse(loc) | |
puts "Going into dir #{loc}" | |
else | |
puts "Processing file #{loc}" | |
process_file(loc) | |
end | |
end | |
end | |
def process_file(file) | |
if File.extname(file) == ".json" | |
puts "Ignoring non-image file #{file}" | |
return | |
end #if | |
# Use EXIF data whenever possible | |
begin | |
date = Exif::Data.new(File.open(file)).date_time.split(" ")[0].split(":") | |
target_subdir = DEST + "/" + date[0,2].join("/") | |
unless Dir.exist?(target_subdir) | |
FileUtils.mkdir_p(target_subdir) | |
end | |
puts "EXIF found. Moving #{file} to #{target_subdir}" | |
FileUtils.mv(file, target_subdir) | |
return | |
rescue NoMethodError, Exif::NotReadable | |
puts "File has no readable EXIF data" | |
# Read data from json as the first fallback | |
if (File.file?(File.basename(file, ".*") + ".json")) | |
json_data = JSON.parse(File.read(File.basename(file, ".*") + ".json")) | |
begin | |
date = Time.at(json_data["photoTakenTime"]["timestamp"].to_i).to_s.split(" ")[0].split("-") | |
target_subdir = DEST + "/" + date[0,2].join("/") | |
unless Dir.exist?(target_subdir) | |
FileUtils.mkdir_p(target_subdir) | |
end | |
puts "JSON found. Moving #{file} to #{target_subdir}" | |
FileUtils.mv(file, target_subdir) | |
return | |
rescue | |
puts "photoTakenTime unknown" | |
end #begin | |
end #if | |
# Fall back to file name match | |
if (file.match?(/wx_camera_[0-9]{10}/) || file.match?(/mmexport[0-9]{10}/)) # 微信 | |
timestamp = file.match(/[0-9]{10}/).to_s.to_i | |
date = Time.at(timestamp).to_s.split(" ")[0].split("-") | |
target_subdir = DEST + "/" + date[0,2].join("/") | |
unless Dir.exist?(target_subdir) | |
FileUtils.mkdir_p(target_subdir) | |
end | |
puts "Time stamp match. Moving #{file} to #{target_subdir}" | |
FileUtils.mv(file, target_subdir) | |
return | |
end #if | |
if (file.match?(/20[0-9]{2}-[0-1][0-9]-[0-3][0-9]/)) | |
date = file.match(/20[0-9]{2}-[0-1][0-9]-[0-3][0-9]/).to_s.split("-") | |
target_subdir = DEST + "/" + date[0,2].join("/") | |
unless Dir.exist?(target_subdir) | |
FileUtils.mkdir_p(target_subdir) | |
end | |
puts "File name match. Moving #{file} to #{target_subdir}" | |
FileUtils.mv(file, target_subdir) | |
return | |
end #if | |
if (file.match?(/20[0-9]{6}/)) | |
dat = file.match(/20[0-9]{6}/).to_s # "20181231" | |
date = [dat[0,4], dat[4,2], dat[6,2]] | |
target_subdir = DEST + "/" + date[0,2].join("/") | |
unless Dir.exist?(target_subdir) | |
FileUtils.mkdir_p(target_subdir) | |
end | |
puts "File name match. Moving #{file} to #{target_subdir}" | |
FileUtils.mv(file, target_subdir) | |
return | |
end #if | |
end #begin | |
end | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment