module Albmmkr

Constants

VERSION

Public Instance Methods

confirm(grouped_files) click to toggle source
# File lib/albmmkr.rb, line 55
  def confirm(grouped_files)
    puts """Here are the albums the image sort came up with:

#{grouped_files.columnize(colsep: '  |  ', ljust: false) }

Do you want me to move your files into that structure?
"""
    choice = nil
    while choice != 'yes' && choice != 'no'
      puts "Yes/No > "
      choice = $stdin.gets.chomp
    end
    choice == 'yes'
  end
file_creation_time(file) click to toggle source
# File lib/albmmkr.rb, line 37
def file_creation_time(file)
  exif = Albmmkr::Exif.new(file)
  return File::Stat.new(file).ctime unless exif.photo
  exif.creationdate || exif.createdate || exif.filemodifydate || File::Stat.new(file).ctime
end
find_files(path) click to toggle source

finds files in a path using the ‘*’ glob

# File lib/albmmkr.rb, line 19
def find_files(path)
  entries = Dir[path + "/*"]
  puts entries.size
  entries.select! { |entry| File.file?(entry) }
  entries
end
formats() click to toggle source
# File lib/albmmkr.rb, line 84
def formats
  { year: "%Y", month: "%Y-%m", day: "%Y-%m-%d", hour: "%Y-%m-%d %H:00" }
end
group_by(files_with_timestamp, sym) click to toggle source
# File lib/albmmkr.rb, line 43
def group_by(files_with_timestamp, sym)
  grouped_files = {}
  pbar = ProgressBar.new("Grouping files", files_with_timestamp.size)
  files_with_timestamp.each do |file, time|
    key = make_key(time, sym)
    grouped_files[key] = (grouped_files[key] || []) << file
    pbar.inc
  end
  pbar.finish
  grouped_files
end
index(path, group, destination) click to toggle source
# File lib/albmmkr.rb, line 10
def index(path, group, destination)
  files = find_files(path)
  files_with_timestamp = timestamps_for_files(files)
  grouped_files = group_by(files_with_timestamp, group)

  make_albums(grouped_files, destination) if confirm(grouped_files.keys)
end
make_albums(grouped_files, destination) click to toggle source
# File lib/albmmkr.rb, line 70
def make_albums(grouped_files, destination)
  pbar = ProgressBar.new("Moving files into albums", grouped_files.keys.size)
  grouped_files.each do |album, files|
    dir = FileUtils.mkdir_p("#{destination}/#{album}")
    files.each { |file| FileUtils.mv file, dir.first }
    pbar.inc
  end
  pbar.finish
end
make_key(time, sym) click to toggle source
# File lib/albmmkr.rb, line 80
def make_key(time, sym)
  time.strftime(formats[sym])
end
sort_files(files) click to toggle source

Sorts an array with [file, timestamp]

# File lib/albmmkr.rb, line 89
def sort_files(files)
  files.sort! { |x,y| x[1] <=> y[1] }
end
timestamps_for_files(files) click to toggle source
# File lib/albmmkr.rb, line 26
def timestamps_for_files(files)
  timestamps = []
  pbar = ProgressBar.new("Timestamps", files.size)
  files.each do |file|
    timestamps << [file, file_creation_time(file)]
    pbar.inc
  end
  pbar.finish
  timestamps
end