Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # - Recursively extracts multi-part RAR archives. This tool is useful when downloading a compressed torrent.
- # - Example usage: ruby clean_torrent.rb "C:\Entertainment\TV Shows\" or in uTorrent, ruby D:\Dropbox\Development\clean_torrent.rb "%D"
- require 'logger'
- # Monkey patch to enable easy logging of exceptions, system calls and deletions
- class Exception
- alias r_initialize initialize
- def initialize(*args)
- r_initialize *args
- $LOG.fatal *args
- end
- end
- module Kernel
- alias r_system system
- def system(*args)
- r_system *args
- $LOG.debug "Running " + args.to_s
- end
- end
- class << File
- alias r_delete delete
- def delete(*args)
- r_delete *args
- $LOG.debug "Deleting " + args.to_s
- end
- end
- $LOG = Logger.new('clean_torrent.log', 'monthly')
- raise ArgumentError, "Incorrect number of arguments" if ARGV.length != 1
- class Cleaner
- def initialize(directory, del=false, unrar="\"C:\\Program Files\\WinRar\\WinRAR.exe\" x -IBCK -o+")
- # Directory to process
- @directory = directory.dup
- # Sanitize input
- @directory.gsub!("\\", "/")
- @directory.chomp!("\"")
- @directory.pop if @directory.split('').last == "/"
- # Delete RAR files after extracting
- @delete = del
- # System UnRAR command
- @unrarpath = unrar
- $LOG.debug "Directory: #{@directory}, Delete: #{@delete}, Command: #{@unrarpath}"
- end
- def r00
- Dir[@directory + "/**/*.r00"].each do |f|
- f.chomp!('00') << 'ar'
- system "#{@unrarpath} \"#{f}\" \"" + File.dirname(f) + "\""
- if @delete
- Dir[f.chomp('ar') << '[0-9][0-9]'].each { |g| File.delete(g) }
- File.delete(f)
- end
- end
- end
- def part01
- Dir[@directory + "/**/*.part01.rar"].each do |f|
- system "#{@unrarpath} \"#{f}\" \"" + File.dirname(f) + "\""
- Dir[File.dirname(f) + "/*.rar"].each { |g| File.delete(g) } if @delete
- end
- end
- end
- clean = Cleaner.new(ARGV[0], true)
- clean.r00
- clean.part01
Add Comment
Please, Sign In to add comment