Class: Taifu::App
- Inherits:
-
Object
- Object
- Taifu::App
- Defined in:
- lib/taifu/app.rb
Constant Summary collapse
- REQUIRED_APPS =
['youtube-dl', 'ffmpeg']
Instance Method Summary collapse
- #add_track ⇒ Object
- #create_script ⇒ Object
- #delete_script ⇒ Object
- #delete_wav ⇒ Object
- #init_working_dir ⇒ Object
-
#initialize(url) ⇒ App
constructor
A new instance of App.
- #logging(message, caption = true) ⇒ Object
- #satisfied? ⇒ Boolean
- #save_wav_with(youtube_url) ⇒ Object
- #script_base ⇒ Object
- #working_dir ⇒ Object
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_track ⇒ Object
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_script ⇒ Object
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.("#{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_script ⇒ Object
44 45 46 |
# File 'lib/taifu/app.rb', line 44 def delete_script FileUtils.rm "#{working_dir}/track.scpt" end |
#delete_wav ⇒ Object
40 41 42 |
# File 'lib/taifu/app.rb', line 40 def delete_wav FileUtils.rm "#{working_dir}/taifu.wav" end |
#init_working_dir ⇒ Object
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(, =true) if puts "[#{}]..." if ENV['LOGGING'] else puts end end |
#satisfied? ⇒ 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? = [] << "'#{app}' is not installed." << "Try this command to install '#{app}'." << "" << " brew install #{app}" << "" raise RuntimeError, .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_base ⇒ Object
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_dir ⇒ Object
25 26 27 28 |
# File 'lib/taifu/app.rb', line 25 def working_dir home_dir = File.('~') File.join(home_dir, '.taifu') end |