Module: Albmmkr

Extended by:
Albmmkr
Included in:
Albmmkr
Defined in:
lib/albmmkr.rb,
lib/albmmkr/exif.rb,
lib/albmmkr/version.rb

Defined Under Namespace

Classes: Exif

Constant Summary collapse

VERSION =
"0.0.5"

Instance Method Summary collapse

Instance Method Details

#confirm(grouped_files) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/albmmkr.rb', line 56

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) ⇒ Object



37
38
39
40
41
42
# 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.createdate || exif.filemodifydate || File::Stat.new(file).ctime
end

#find_files(path) ⇒ Object

finds files in a path using the ‘*’ glob



19
20
21
22
23
24
# File 'lib/albmmkr.rb', line 19

def find_files(path)
  entries = Dir[path + "/*"]
  puts entries.size
  entries.select! { |entry| File.file?(entry) }
  entries
end

#formatsObject



85
86
87
# File 'lib/albmmkr.rb', line 85

def formats
  { year: "%Y", month: "%Y-%m", day: "%Y-%m-%d", hour: "%Y-%m-%d %H:00" }
end

#group_by(files_with_timestamp, sym) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/albmmkr.rb', line 44

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) ⇒ Object



10
11
12
13
14
15
16
# 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) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/albmmkr.rb', line 71

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) ⇒ Object



81
82
83
# File 'lib/albmmkr.rb', line 81

def make_key(time, sym)
  time.strftime(formats[sym])
end

#sort_files(files) ⇒ Object

Sorts an array with [file, timestamp]



90
91
92
# File 'lib/albmmkr.rb', line 90

def sort_files(files)
  files.sort! { |x,y| x[1] <=> y[1] }
end

#timestamps_for_files(files) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# 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