class Mediasort

Attributes

options[RW]

Public Class Methods

new(options) click to toggle source
# File lib/mediasort.rb, line 7
def initialize(options)
  @options = options
end

Public Instance Methods

check_options() click to toggle source
# File lib/mediasort.rb, line 20
def check_options
  # Optional Arguments
  options[:verbose]  = false if options[:verbose].nil?
  options[:progress] = true  if options[:progress].nil?

  # Required Arguments
  if !options[:mimetype]
    puts "Missing mimetype argument!"
  end

  if !options[:input]
    puts "Missing input argument!"
  end

  if !options[:output]
    puts "Missing output argument!"
  end
end
clean_paths() click to toggle source
# File lib/mediasort.rb, line 11
def clean_paths
  options[:input]  = options[:input].gsub(/^\ /, "\\ ")
  options[:output] = options[:output].gsub(/^\ /, "\\ ")
  options[:input]  = options[:input].gsub(/~/, ENV["HOME"])
  options[:output] = options[:output].gsub(/~/, ENV["HOME"])
  options[:input]  = options[:input].gsub(/$\//, "")
  options[:output] = options[:output].gsub(/$\//, "")
end
parse_json(d) click to toggle source
# File lib/mediasort.rb, line 39
def parse_json(d)
  JSON.parse(`exiftool -json "#{d}"`).first
end
work() click to toggle source
# File lib/mediasort.rb, line 43
def work
  check_options
  clean_paths

  entries = Dir["#{options[:input]}/*"]
  entries = entries.length == 0  ? [] : entries
  entries.each do |d|
    begin
      if File.file?(d)
        file = parse_json(d)
        if file && !file["MIMEType"].nil? && file["MIMEType"][options[:mimetype]]
          date = file["CreateDate"]
          if date
            date  = date.split(' ')
            date  = date[0].gsub(':', '-') + " " + date[1]
            date  = DateTime.parse(date)
            year  = date.year
            month = date.month
            day   = date.day

            month = format('%02d', month)
            day = format('%02d', day)
            path = "#{year}/#{month}/#{year}-#{month}-#{day}"

            basedir = `dirname #{d}`
            mkdir = "mkdir -p #{options[:output]}/#{path}"
            mv    = "mv -n #{d} #{options[:output]}/#{path}/"

            puts mkdir if options[:verbose]
            puts mv if options[:verbose]

            %x[#{mkdir}]
            %x[#{mv}]

            print '⋅' if options[:progress]
          end
        end
      end
    rescue JSON::ParserError
      puts JSON::ParserError
    end

  end
end