Class: Taifu::App

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

Constant Summary collapse

REQUIRED_APPS =
['youtube-dl', 'ffmpeg']

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ App

Returns a new instance of App.



8
9
10
11
12
13
14
15
16
17
# File 'lib/taifu/app.rb', line 8

def initialize(url)
  return unless satisfied?

  init_working_dir

  save_wav_with(url)
  add_track

  logging "Done. Type 'taifu' on your iTunes", false
end

Instance Method Details

#add_trackObject



30
31
32
33
34
35
36
37
38
# File 'lib/taifu/app.rb', line 30

def add_track
  logging 'Add wav file to iTunes'

  script = create_script
  `osascript #{script}`
  delete_script

  delete_wav
end

#create_scriptObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/taifu/app.rb', line 48

def create_script
  file_path = MacTypes::FileURL.path(File.expand_path("#{working_dir}/taifu.wav")).hfs_path
  script_path = "#{working_dir}/track.scpt"

  script = script_base.gsub('$file_path', file_path)
  open(script_path, 'w') do |f|
    f.write script
  end
  script_path
end

#delete_scriptObject



44
45
46
# File 'lib/taifu/app.rb', line 44

def delete_script
  FileUtils.rm "#{working_dir}/track.scpt"
end

#delete_wavObject



40
41
42
# File 'lib/taifu/app.rb', line 40

def delete_wav
  FileUtils.rm "#{working_dir}/taifu.wav"
end

#init_working_dirObject



19
20
21
22
23
# File 'lib/taifu/app.rb', line 19

def init_working_dir
  unless File.exist?(working_dir)
    FileUtils.mkdir(working_dir)
  end
end

#logging(message, caption = true) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/taifu/app.rb', line 87

def logging(message, caption=true)
  if caption
    puts "[#{message}]..." if ENV['LOGGING']
  else
    puts message
  end
end

#satisfied?Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/taifu/app.rb', line 72

def satisfied?
  REQUIRED_APPS.each do |app|
    if `which #{app}`.empty?
      messages = []
      messages << "'#{app}' is not installed."
      messages << "Try this command to install '#{app}'."
      messages << ""
      messages << "   brew install #{app}"
      messages << ""
      raise RuntimeError, messages.join("\n")
    end
  end
  true
end

#save_wav_with(youtube_url) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/taifu/app.rb', line 59

def save_wav_with(youtube_url)
  url = youtube_url.split('&').first
  wav_file = 'taifu.wav'
  wav_path = "#{working_dir}/#{wav_file}"

  logging 'Download data'
  system "youtube-dl -q #{url} -o #{working_dir}/taifu.flv"

  logging 'Save wav file'
  system "ffmpeg -i #{working_dir}/taifu.flv #{wav_path} 2>/dev/null"
  system "rm -f #{working_dir}/taifu.flv"
end

#script_baseObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/taifu/app.rb', line 95

def script_base
<<-SCRIPT
tell application "iTunes"
  set added_track to add "$file_path"
  set loc to (get location of added_track)
  convert added_track
  delete added_track

  tell application "Finder"
delete loc
  end tell
end tell
SCRIPT
end

#working_dirObject



25
26
27
28
# File 'lib/taifu/app.rb', line 25

def working_dir
  home_dir = File.expand_path('~')
  File.join(home_dir, '.taifu')
end