vadviktor
5/2/2015 - 10:33 PM

Process the btsynced photos and videos from our phones

Process the btsynced photos and videos from our phones

require 'mini_magick'
require 'active_support/core_ext/object/blank'
require 'fileutils'

SOURCE_DIR      = 'C:/store/sync'
DESTINATION_DIR = 'C:/Users/ikon/megasync'

Dir.glob "#{SOURCE_DIR}/**/*.{jpg,mp4}" do |filepath|

  50.times { print '=' }
  puts ''
  puts "Processing [#{filepath}]"

  case filepath.slice -4, 4
    when '.jpg'
      image = MiniMagick::Image.new filepath
      exif  = image.exif
      if exif.present? and exif['DateTimeOriginal'].present?
        puts 'Using exif date'

        exif_date = exif['DateTimeOriginal']
        date      = Date.parse exif_date.slice(0, 10).gsub(/:/, '-')
        filename  = Time.parse(exif_date.gsub(/(\d{4}):(\d{2}):(.+)/) { "#{$1}-#{$2}-#{$3}" }).strftime('%Y-%m-%d %H.%M.%S') + '.jpg'
      else
        puts 'Using file modification time'

        # use file modification date
        date     = File.mtime(filepath).to_date
        filename = File.mtime(filepath).strftime('%Y-%m-%d %H.%M.%S') + '.jpg'
      end
    when '.mp4'
      date     = File.mtime(filepath).to_date
      filename = File.mtime(filepath).strftime('%Y-%m-%d %H.%M.%S') + '.mp4'
    else
      puts "Where did this come from: #{filepath} ?"
      next # skip to the next file
  end

  # process file

  # change this if a different directory name is needed
  dir      = "#{DESTINATION_DIR}/Mark - #{date.year} #{Date::MONTHNAMES[date.month]}"
  FileUtils.mkdir_p dir

  # add a microsec suffix if another file with same name already exists
  filename = filename.insert -5, " - #{Time.now.usec}" if File.exist? "#{dir}/#{filename}"

  puts "Final destination: #{dir}/#{filename}"
  FileUtils.copy_file filepath, "#{dir}/#{filename}"
end
source 'https://rubygems.org'

gem 'mini_magick'
gem 'activesupport'