Class: AstroSubframeOrganizer::Utils::ExifRenamer

Inherits:
Object
  • Object
show all
Includes:
Logging, ExposureFormat
Defined in:
lib/astro_subframe_organizer/utils/exif_renamer.rb

Overview

Renames CR2 files based on EXIF data to match the standard subframe naming convention: TYPE_TARGET_EXPTIME_BIN_CAMERA_ISO_DATETIME_TEMP_SEQ.CR2

Constant Summary collapse

RAW_NAME_PATTERN =
/^(IMG_|DSC_|_DSC|DSCN)/
EXIF_DT_FORMAT =
'%Y:%m:%d %H:%M:%S%z'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ExposureFormat

#format_exposure

Methods included from Logging

#logger

Constructor Details

#initialize(path = Dir.pwd) ⇒ ExifRenamer

Returns a new instance of ExifRenamer.



19
20
21
# File 'lib/astro_subframe_organizer/utils/exif_renamer.rb', line 19

def initialize(path = Dir.pwd)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



17
18
19
# File 'lib/astro_subframe_organizer/utils/exif_renamer.rb', line 17

def path
  @path
end

Instance Method Details

#already_named?(files) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/astro_subframe_organizer/utils/exif_renamer.rb', line 45

def already_named?(files)
  files.none? { |file| File.basename(file).match?(RAW_NAME_PATTERN) }
end

#find_cr2_filesObject



49
50
51
52
53
54
# File 'lib/astro_subframe_organizer/utils/exif_renamer.rb', line 49

def find_cr2_files
  exts = Config.raw_extensions.flat_map { |ext| [ext.downcase, ext.upcase] }
  Dir.glob(exts.map { |e| "**/*#{e}" }, base: path)
     .map { |f| File.expand_path(File.join(path, f)) }
     .uniq
end

#rename(type:, target: nil, dry_run: false) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/astro_subframe_organizer/utils/exif_renamer.rb', line 23

def rename(type:, target: nil, dry_run: false)
  Exiftool.command = 'exiftool.exe' if Gem.win_platform?

  cr2_files = find_cr2_files

  if cr2_files.empty?
    logger.warn 'No CR2 files found.'
    return
  end

  e = Exiftool.new(cr2_files)
  bar = TTY::ProgressBar.new(
    'Renameing files from EXIF data [:bar] :current/:total (:percent) :eta',
    total: e.files_with_results.size,
  )

  e.files_with_results.each do |cr2|
    exif = e.result_for(cr2)
    rename_file(cr2, exif, type: type, target: target, dry_run: dry_run, bar: bar)
  end
end

#revert(dry_run: false) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/astro_subframe_organizer/utils/exif_renamer.rb', line 56

def revert(dry_run: false)
  cr2_files = find_cr2_files # recursive search

  if cr2_files.empty?
    logger.warn 'No CR2 files found.'
    return
  end

  cr2_files.each_with_index do |file, index|
    idx         = derive_sequence_number_from_filename(file) || (index + 1)
    filename    = "IMG_#{idx.to_s.rjust(4, '0')}.CR2"
    target_file = File.join(File.dirname(file), filename)

    logger.info "Renaming #{File.basename(file)} to #{filename}"
    FileUtils.move(file, target_file, verbose: dry_run, noop: dry_run) unless File.exist?(target_file)
  end
end