#!/usr/bin/ruby require 'yaml' require 'fileutils' require 'shellwords' # require 'apple_tv_converter' def debug(message) puts "[#{Process.pid.to_s.ljust(6, ' ')} @ #{Time.now.strftime('%Y/%m/%d %H:%I:%S')}] #{message}" STDOUT.flush end def check_for_file_complete(file) debug "Check for file complete: #{file}" same_size_counter = 0 previous_size = 0 while true new_size = File.size(file) debug "size: #{previous_size} -> #{new_size}" if previous_size < new_size same_size_counter = 0 else same_size_counter += 1 end return true if same_size_counter == 3 previous_size = new_size sleep 1 end end debug "Starting" EXTENSIONS = ["avi", "mov", "mpg", "mpeg", "wmv", "mp4", "mkv", "ogg", "m2ts"] plist_file = File.read(File.expand_path(File.join('~', 'Library', 'LaunchAgents', 'org.gokuu.apple-tv-converter.plist'))) paths = plist_file.match(/WatchPaths.*?(.*?.*?<\/string>.*?<\/array>)/m)[1].gsub(/<\/?.*?>/, "\n").strip.gsub(/\s{2,}/, "\n").split("\n") existing_files = File.expand_path(File.join('~', 'Library', 'Application Support', 'apple-tv-converter', 'files.yml')) FileUtils.mkdir_p File.dirname(existing_files) files = YAML.load_file(existing_files) if File.exists?(existing_files) files ||= {} paths.each do |path| if File.directory?(path) old_existing = files[path] || [] new_existing = Dir[File.join(path, '**', '*')].select { |f| !File.directory?(f) } to_process = new_existing.select { |new_file| !old_existing.include?(new_file) && EXTENSIONS.include?(File.extname(new_file).gsub(/^\./, '').downcase) } if to_process.any? command_line = "/usr/bin/apple-tv-converter --ffmpeg /usr/local/bin/ffmpeg --itunes -l eng,por --imdb --os" to_process.each do |f| if check_for_file_complete(f) command_line << %Q[ "#{f}"] end end debug command_line debug `#{command_line}` end files[path] = new_existing end end File.open(existing_files, 'w') { |file| file.write files.to_yaml } debug "Complete"