Last active
August 29, 2015 14:08
-
-
Save jamesottaway/806be64342146b91d272 to your computer and use it in GitHub Desktop.
This file contains 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
module ArrayRefinements | |
refine Array do | |
def head | |
self.first | |
end | |
def tail | |
self[1..-1] | |
end | |
end | |
end |
This file contains 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 'photo' | |
require 'array_refinements' | |
using ArrayRefinements | |
dir = '/Volumes/HFS+' | |
extension = 'CR2' | |
Dir.glob(File.join(dir, '**', "*.#{extension}")) | |
.map { |path| Photo.new(path) }.group_by(&:name) | |
.select { |name, group| group.count > 1 } | |
.values.each { |group| | |
group.head.keep! | |
}.each { |group| | |
group.tail.reject { |photo| photo == group.head }.map(&:keep!) | |
}.flatten.map(&:to_s) | |
.tap { |commands| STDOUT.puts commands } |
This file contains 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 'shellwords' | |
require 'openssl' | |
class Photo < Struct.new :path | |
def name | |
File.split(path).last | |
end | |
def fingerprint | |
@fingerprint ||= OpenSSL::Digest::SHA256.new(data) | |
end | |
def ==(other) | |
self.fingerprint == other.fingerprint | |
end | |
def keep! | |
@keep = true | |
end | |
def keep? | |
@keep | |
end | |
def to_s | |
"#{action} #{Shellwords.escape(path)}" | |
end | |
private | |
def data | |
File.read(path) | |
end | |
def action | |
keep? ? 'touch' : ' rm' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment