Class: Radikocopy::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/radikocopy.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts, config) ⇒ Command

Returns a new instance of Command.



76
77
78
79
# File 'lib/radikocopy.rb', line 76

def initialize(opts, config)
  @opts = opts
  @config = config
end

Class Method Details

.run(argv) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/radikocopy.rb', line 46

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

Instance Method Details

#copy_file(filename) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/radikocopy.rb', line 115

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 #{@config.remote_host}:\"'#{filename}'\" \"#{@config.local_dir}\""
  runcmd_and_exit(copy_command)
  true
end

#copy_filesObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/radikocopy.rb', line 94

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_filesObject



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/radikocopy.rb', line 156

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) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/radikocopy.rb', line 147

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) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/radikocopy.rb', line 130

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

#runObject



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/radikocopy.rb', line 81

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) ⇒ Object



176
177
178
179
# File 'lib/radikocopy.rb', line 176

def runcmd(cmd)
  puts cmd
  system(cmd)
end

#runcmd_and_exit(cmd) ⇒ Object



169
170
171
172
173
174
# File 'lib/radikocopy.rb', line 169

def runcmd_and_exit(cmd)
  unless runcmd(cmd)
    puts "system error"
    exit(1)        
  end
end