From 9f7869c1c4d67ffba4f4277b21b710ed51f49fda Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Wed, 24 Sep 2014 10:04:15 -0700 Subject: [PATCH] extract archives in encode-and-add-to-itunes.rb --- encode-and-add-to-itunes.rb | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/encode-and-add-to-itunes.rb b/encode-and-add-to-itunes.rb index c531e72..8056bb3 100755 --- a/encode-and-add-to-itunes.rb +++ b/encode-and-add-to-itunes.rb @@ -5,9 +5,18 @@ require 'fileutils' ADD_TO_ITUNES_DIR = File.expand_path('~/Music/iTunes/iTunes Media/Automatically Add to iTunes.localized') def main - root_dir = ENV['TR_TORRENT_DIR'] || ARGV.shift + root_dir = + if ENV['TR_TORRENT_DIR'] + File.join(ENV['TR_TORRENT_DIR'], ENV['TR_TORRENT_NAME']) + else + ARGV.shift + end + if File.exists?(root_dir) - puts "* Encoding files in #{root_dir}" + puts "* Looking for archives in #{root_dir}..." + extract_archives(root_dir) + + puts "* Encoding files in #{root_dir}..." encode_and_add_to_itunes(root_dir) elsif root_dir puts "file not found: #{root_dir}" @@ -18,6 +27,22 @@ def main end end +def extract_archives(dir) + Dir.foreach(dir) do |filename| + next if filename == '.' || filename == '..' + path = File.join(dir, filename) + if File.directory?(path) + extract_archives(path) + elsif filename =~ /\.rar$/ + pwd = Dir.pwd + Dir.chdir(dir) + puts "* Extracting #{filename}..." + `unrar x '#{filename}'` + Dir.chdir(pwd) + end + end +end + def encode_and_add_to_itunes(dir) Dir.foreach(dir) do |filename| next if filename == '.' || filename == '..'