Guest User

Untitled

a guest
Aug 15th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. # - Recursively extracts multi-part RAR archives. This tool is useful when downloading a compressed torrent.
  2. # - Example usage: ruby clean_torrent.rb "C:\Entertainment\TV Shows\" or in uTorrent, ruby D:\Dropbox\Development\clean_torrent.rb "%D"
  3.  
  4. require 'logger'
  5.  
  6. # Monkey patch to enable easy logging of exceptions, system calls and deletions
  7. class Exception
  8. alias r_initialize initialize
  9. def initialize(*args)
  10. r_initialize *args
  11. $LOG.fatal *args
  12. end
  13. end
  14.  
  15. module Kernel
  16. alias r_system system
  17. def system(*args)
  18. r_system *args
  19. $LOG.debug "Running " + args.to_s
  20. end
  21. end
  22.  
  23. class << File
  24. alias r_delete delete
  25. def delete(*args)
  26. r_delete *args
  27. $LOG.debug "Deleting " + args.to_s
  28. end
  29. end
  30.  
  31. $LOG = Logger.new('clean_torrent.log', 'monthly')
  32.  
  33. raise ArgumentError, "Incorrect number of arguments" if ARGV.length != 1
  34.  
  35. class Cleaner
  36. def initialize(directory, del=false, unrar="\"C:\\Program Files\\WinRar\\WinRAR.exe\" x -IBCK -o+")
  37. # Directory to process
  38. @directory = directory.dup
  39. # Sanitize input
  40. @directory.gsub!("\\", "/")
  41. @directory.chomp!("\"")
  42. @directory.pop if @directory.split('').last == "/"
  43.  
  44. # Delete RAR files after extracting
  45. @delete = del
  46.  
  47. # System UnRAR command
  48. @unrarpath = unrar
  49.  
  50. $LOG.debug "Directory: #{@directory}, Delete: #{@delete}, Command: #{@unrarpath}"
  51. end
  52.  
  53. def r00
  54. Dir[@directory + "/**/*.r00"].each do |f|
  55. f.chomp!('00') << 'ar'
  56. system "#{@unrarpath} \"#{f}\" \"" + File.dirname(f) + "\""
  57. if @delete
  58. Dir[f.chomp('ar') << '[0-9][0-9]'].each { |g| File.delete(g) }
  59. File.delete(f)
  60. end
  61. end
  62. end
  63.  
  64. def part01
  65. Dir[@directory + "/**/*.part01.rar"].each do |f|
  66. system "#{@unrarpath} \"#{f}\" \"" + File.dirname(f) + "\""
  67. Dir[File.dirname(f) + "/*.rar"].each { |g| File.delete(g) } if @delete
  68. end
  69. end
  70. end
  71.  
  72. clean = Cleaner.new(ARGV[0], true)
  73. clean.r00
  74. clean.part01
Add Comment
Please, Sign In to add comment