Class: Unpack
- Inherits:
-
Object
- Object
- Unpack
- Defined in:
- lib/unpack.rb
Instance Attribute Summary collapse
-
#files ⇒ Object
Returns the value of attribute files.
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #clean! ⇒ Object
- #diff ⇒ Object
- #find_file_type(file_type) ⇒ Object
-
#initialize(args) ⇒ Unpack
constructor
A new instance of Unpack.
- #prepare! ⇒ Object
- #unpack! ⇒ Object
- #unrar(args) ⇒ Object
- #unzip(args) ⇒ Object
-
#wipe! ⇒ Object
Removes the old rar and zip files.
Constructor Details
#initialize(args) ⇒ Unpack
Returns a new instance of Unpack.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/unpack.rb', line 8 def initialize(args) args.keys.each { |name| instance_variable_set "@" + name.to_s, args[name] } @options = { :min_files => 5, :depth => 2, :debugger => false, :force_remove => false, :remove => false, :absolute_path_to_unrar => "#{File.dirname(__FILE__)}/../bin/unrar" } @removeable = {} @options.merge!(args[:options]) if args[:options] # If the path is relative @directory = File.(@directory) unless @directory.match(/^\//) # Makes shure that every directory structure looks the same @directory = Dir.new(@directory).path rescue nil raise Exception.new("You need to specify a valid path") if @directory.nil? or not Dir.exist?(@directory) raise Exception.new("You need unzip to keep going") if %x{whereis unzip}.empty? end |
Instance Attribute Details
#files ⇒ Object
Returns the value of attribute files.
6 7 8 |
# File 'lib/unpack.rb', line 6 def files @files end |
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/unpack.rb', line 6 def @options end |
Class Method Details
.runner!(directory = '.', options = {}) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/unpack.rb', line 34 def self.runner!(directory = '.', = {}) unpack = Unpack.new(directory: directory, options: ) rescue nil # If the initializer raised any excetions return [] if unpack.nil? unpack.prepare! unpack.clean! unpack.unpack! unpack.wipe! if [:remove] unpack.diff end |
Instance Method Details
#clean! ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/unpack.rb', line 57 def clean! # Removing all folders that have less then {@options[:lim]} files in them # Those folders are offen subtitle folders folders = @files.map {|file| File.dirname(file)}.uniq.reject {|folder| Dir.entries(folder).count <= (@options[:min_files] + 2)} @files.reject!{|file| not folders.include?(File.dirname(file))} results = [] # Finding one rar file for every folder @files.group_by{|file| File.dirname(file) }.each_pair{|_,file| results << file.sort.first } @files = results end |
#diff ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/unpack.rb', line 120 def diff # The code below this line can only be called once return @removeable if @removeable.first.class == Container # Removing some non welcome data @removeable.reject!{|item| @removeable[item][:diff].nil? or @removeable[item][:diff].empty?} @removeable = @removeable.map do |value| Container.new(files: value.last[:diff], directory: value.first) end # Never return the hash @removeable.empty? ? [] : @removeable end |
#find_file_type(file_type) ⇒ Object
143 144 145 |
# File 'lib/unpack.rb', line 143 def find_file_type(file_type) %x{cd '#{@directory}' && find '#{@directory}' -type f -maxdepth '#{(@options[:depth])}' -name \"*.#{file_type}\"}.split(/\n/) end |
#prepare! ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/unpack.rb', line 46 def prepare! @directory.gsub!(/\s+/, '\ ') @files = [] ['zip', 'rar'].each do |type| @files << find_file_type(type) end @files.flatten!.map! {|file| File.absolute_path(file)} end |
#unpack! ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/unpack.rb', line 69 def unpack! @files.each do |file| type = Mimer.identify(file) path = File.dirname(file) before = Dir.new(path).entries if type.zip? @removeable.merge!(path => {:file_type => 'zip'}) self.unzip(path: path, file: file) elsif type.rar? @removeable.merge!(path => {:file_type => 'rar'}) self.unrar(path: path, file: file) else puts "Something went wrong, the mime type does not match zip or rar" if @options[:debugger] end # What files/folders where unpacked? diff = Dir.new(path).entries - before @removeable[path] ? @removeable[path].merge!(:diff => diff) : @removeable.delete(path) # Some debug info if @options[:debugger] and diff.any? and @removeable[path] puts "#{diff.count} where unpacked" puts "The archive was of type #{@removeable[path][:file_type]}" puts "The name of the file(s) are #{diff.join(', ')}" puts "The path is #{path}" STDOUT.flush end end end |
#unrar(args) ⇒ Object
135 136 137 |
# File 'lib/unpack.rb', line 135 def unrar(args) %x(cd '#{args[:path]}' && '#{@options[:absolute_path_to_unrar]}' e -y -o- '#{args[:file]}') end |
#unzip(args) ⇒ Object
139 140 141 |
# File 'lib/unpack.rb', line 139 def unzip(args) %x(unzip -n '#{args[:file]}' -d '#{args[:path]}') end |
#wipe! ⇒ Object
Removes the old rar and zip files
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/unpack.rb', line 102 def wipe! @removeable.each do |value| path = value.first type = value.last[:file_type] # Does not remove anything if nothing where unpacked next if value.last[:diff].empty? and not @options[:force_remove] puts "Removing files in #{path}" if @options[:debugger] # Finding every file in this directory Dir.glob(path + '/*').each do |file| # Is the found file as the same type as the one that got unpacked? FileUtils.rm(file) if Mimer.identify(file).send(:"#{type}?") end end end |