public
Description: A collection of scripts (some mine, some shamelessly nabbed from elsewhere) that live in my ~/local/bin folder.
Clone URL: git://github.com/nickstenning/bin.git
bin / photos_import
100755 37 lines (29 sloc) 0.795 kb
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
#!/usr/bin/env ruby
 
require 'pathname'
require 'fileutils'
 
PHOTOS = Pathname.new(ENV["HOME"] + "/Photos")
#PHOTOS = Pathname.new("./temp_photos")
cwd = Pathname.getwd
 
module Enumerable
  def partition_by #:yield:
    result = {}
    self.each do |e|
      value = yield e
      (result[value] ||= []) << e
    end
    result
  end
end
 
print "-- Import *.{JPG,jpg} from the current directory (#{cwd})? [yN] "
exit unless gets =~ /[Yy](es)?/
 
files = Pathname.glob("*.{JPG,jpg}").partition_by { |x| x.ctime.strftime("%Y/%m/%d") }
 
files.each_key do |dir|
  outdir = PHOTOS + dir
  if outdir.directory?
    puts "-- #{outdir} exists, adding photos:"
  else
    outdir.mkpath
    puts "-- #{outdir} created, adding photos:"
  end
  files[dir].each { |f| puts f; FileUtils.cp(f, outdir) }
end