class Radikocopy::Command

Public Class Methods

new(opts, config) click to toggle source
# File lib/radikocopy.rb, line 80
def initialize(opts, config)
  @opts = opts
  @config = config
end
run(argv) click to toggle source
# File lib/radikocopy.rb, line 50
def self.run(argv)
  STDOUT.sync = true
  opts = {}
  opt = OptionParser.new(argv)
  opt.banner = "Usage: #{opt.program_name} [-h|--help] [config.yml]"
  opt.version = Radikocopy::VERSION
  opt.separator('')
  opt.separator "Options:"
  opt.on_head('-h', '--help', 'Show this message') do |v|
    puts opt.help
    exit
  end
  opt.on('-v', '--verbose', 'Verbose message') {|v| opts[:v] = v}
  opt.on('-n', '--dry-run', 'Message only') {|v| opts[:n] = v}
  opt.on('-f', '--force-import', 'Force import') {|v| opts[:f] = v} 
  opt.parse!(argv)

  # 最後の引数は設定ファイルのパス
  config_file = argv.empty? ? "~/.radikocopyrc" : argv[0]
  config_file = File.expand_path(config_file)
  unless FileTest.file?(config_file)
    puts opt.help
    exit
  end
  config = Config.new(YAML.load_file(config_file))
  puts config
  radikocopy = Command.new(opts, config)
  radikocopy.run
end

Public Instance Methods

copy_file(filename) click to toggle source
# File lib/radikocopy.rb, line 119
    def copy_file(filename)
      basename = File.basename(filename)
      local_file = File.join(@config.local_dir, basename)
#      puts  "##### local_file #{local_file} #####"
      if FileTest.file?(local_file)
# TODO -v option
#        puts "exists local_file #{local_file}"
        return false
      end
#      puts "not exists"
      copy_command = "scp -p -T #{@config.remote_host}:\"'#{filename}'\" \"#{@config.local_dir}\""
      runcmd_and_exit(copy_command)
      true
    end
copy_files() click to toggle source
# File lib/radikocopy.rb, line 98
    def copy_files
      # リモートホスト内の録音ファイルをローカルにコピー
      list_command = "ssh #{@config.remote_host} 'find \"#{@config.remote_dir}\"'"
      puts list_command
      result = `#{list_command}`
      files = []
      result.each_line do |line|
#        puts line
        line.chomp!
        if line =~ /mp3$/ || line =~ /m4a$/
          if copy_file(line)
            basename = File.basename(line)
            local_file = File.join(@config.local_dir, basename)
            puts "local_file: #{local_file}"
            files << local_file
          end
        end
      end
      files
    end
expire_local_files() click to toggle source
# File lib/radikocopy.rb, line 160
def expire_local_files
  #ローカル保存フォルダ内の古いファイルを削除する
  filenames = Dir.glob("#{@config.local_dir}/*.{mp3,m4a}").sort_by {|f| File.mtime(f) }.reverse
  filenames.each_with_index do |filename, index|
    if index < @config.keep
      puts "Keep: #{filename}"
    else
      puts "Delete: #{filename}"
      File.unlink(filename)
    end
  end
end
import_file(file, i) click to toggle source
# File lib/radikocopy.rb, line 151
def import_file(file, i)
  cmd = "osascript #{@config.import_scpt} \"#{file}\""
  unless runcmd(cmd)
    puts "import error[#{i}] #{file}"
    return false
  end
  true
end
import_files(files) click to toggle source
# File lib/radikocopy.rb, line 134
def import_files(files)
  files.each do |file|
    import_ok = false
    3.times do |i|
      if import_file(file, i)
        import_ok = true
        break
      end
      sleep(1)
    end
    unless import_ok
      puts "import failed"
      File.unlink(file)
    end
  end
end
run() click to toggle source
# File lib/radikocopy.rb, line 85
def run
  puts "##### start radikocopy #####"
  filenames = []
  if @opts[:f] || @config.local_only?
    filenames = Dir.glob("#{@config.local_dir}/*.{mp3,m4a}")
  else
    filenames = copy_files
  end
  import_files(filenames)
  expire_local_files
  puts "##### end radikocopy #####" 
end
runcmd(cmd) click to toggle source
# File lib/radikocopy.rb, line 180
def runcmd(cmd)
  puts cmd
  system(cmd)
end
runcmd_and_exit(cmd) click to toggle source
# File lib/radikocopy.rb, line 173
def runcmd_and_exit(cmd)
  unless runcmd(cmd)
    puts "system error"
    exit(1)        
  end
end