Class: ExifTagger::TagWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/phtools/exif_tagger/tag_writer.rb

Overview

batch EXIF tags setter

Constant Summary collapse

DEFAULT_OPTIONS =
%w(-v0 -FileModifyDate<DateTimeOriginal -overwrite_original -ignoreMinorErrors).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(script_name: 'exif_tagger.txt', memo: 'Generated by phtools', output: STDOUT, err: STDERR) ⇒ TagWriter

Returns a new instance of TagWriter.



15
16
17
18
19
20
21
22
23
24
# File 'lib/phtools/exif_tagger/tag_writer.rb', line 15

def initialize(script_name: 'exif_tagger.txt',
               memo: 'Generated by phtools',
               output: STDOUT, err: STDERR)
  @script_name = script_name
  create_script(memo)
  @added_files_count = 0
  @output = output
  @err = err
  @output.puts "*** Preparing exiftool script '#{@script_name}' ..."
end

Instance Attribute Details

#added_files_countObject (readonly)

Returns the value of attribute added_files_count.



13
14
15
# File 'lib/phtools/exif_tagger/tag_writer.rb', line 13

def added_files_count
  @added_files_count
end

#script_nameObject (readonly)

Returns the value of attribute script_name.



13
14
15
# File 'lib/phtools/exif_tagger/tag_writer.rb', line 13

def script_name
  @script_name
end

Instance Method Details

#add_to_script(filename: '', tags: {}, options: DEFAULT_OPTIONS) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/phtools/exif_tagger/tag_writer.rb', line 26

def add_to_script(filename: '', tags: {}, options: DEFAULT_OPTIONS)
  @script.puts "# **(#{@added_files_count + 1})** Processing file: #{filename} *****"
  # tags
  tags.each do |k|
    @script.puts tags.item(k).to_write_script
  end
  # file to be altered
  @script.puts filename
  # General options
  options.each { |o| @script.puts o }
  @script.puts %(-execute)
  @script.puts
  @added_files_count += 1

rescue => e
  raise WriteTag, "adding item to exiftool script - #{e.message}"
end

#close_scriptObject



44
45
46
47
48
49
# File 'lib/phtools/exif_tagger/tag_writer.rb', line 44

def close_script
  @script.close
  @output.puts "*** Finished preparation of the script '#{script_name}'"
rescue => e
  raise WriteTag, "closing exiftool script - #{e.message}"
end

#commandObject



51
52
53
# File 'lib/phtools/exif_tagger/tag_writer.rb', line 51

def command
  "exiftool -@ #{@script_name}"
end

#run!Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/phtools/exif_tagger/tag_writer.rb', line 55

def run!
  close_script
  if @added_files_count.positive?
    @output.puts "*** Running #{command} ..."
    ok = system(command, out: @output, err: @err)
    fail if ok.nil?
    @output.puts "*** Finished #{command}"
  else
    @output.puts "*** Nothing to update, skip running #{command} ..."
  end
rescue
  raise WriteTag, "running #{command}"
end