Class: Unpack
- Inherits:
-
Object
- Object
- Unpack
- Defined in:
- lib/unpack.rb
Instance Attribute Summary collapse
-
#directory ⇒ Object
Returns the value of attribute directory.
-
#files ⇒ Object
Returns the value of attribute files.
-
#options ⇒ Object
Returns the value of attribute options.
-
#removeable ⇒ Object
Returns the value of attribute removeable.
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 33 34 |
# File 'lib/unpack.rb', line 8 def initialize(args) args.keys.each { |name| instance_variable_set "@" + name.to_s, args[name] } = { :min_files => 5, :depth => 2, :debugger => false, :force_remove => false, :remove => false, :absolute_path_to_unrar => "#{File.dirname(__FILE__)}/../bin/unrar" } @removeable = {} .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? @files = [] end |
Instance Attribute Details
#directory ⇒ Object
Returns the value of attribute directory.
6 7 8 |
# File 'lib/unpack.rb', line 6 def directory @directory end |
#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 end |
#removeable ⇒ Object
Returns the value of attribute removeable.
6 7 8 |
# File 'lib/unpack.rb', line 6 def removeable @removeable end |
Class Method Details
.it!(args) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/unpack.rb', line 36 def self.it!(args) # If no to argument is given, file will be unpacked in the same dir args[:to] = args[:to].nil? ? File.dirname(args[:file]) : args[:to] # Adding the options that is being passed to {it!} directly to {Unpack} this = self.new(directory: args[:to], options: {min_files: 0}.merge(args)) # Is the file path absolute ? good, do nothing : get the absolute path file = args[:file].match(/^\//) ? args[:file] : File.(args[:file]) this.files << file this.unpack! this.wipe! if this.[:remove] # Only one files has been unpacked, that is why we select the first object in the list this.diff.first end |
.runner!(directory = '.', options = {}) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/unpack.rb', line 54 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
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/unpack.rb', line 76 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 <= ([: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
142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/unpack.rb', line 142 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
165 166 167 |
# File 'lib/unpack.rb', line 165 def find_file_type(file_type) %x{cd '#{@directory}' && find '#{@directory}' -type f -maxdepth '#{(@options[:depth])}' -name '*.#{file_type}'}.split(/\n/) end |
#prepare! ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/unpack.rb', line 66 def prepare! @files = [] ['zip', 'rar'].each do |type| @files << find_file_type(type) end @files.flatten!.map! {|file| File.absolute_path(file)} end |
#unpack! ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/unpack.rb', line 88 def unpack! @files.each do |file| type = Mimer.identify(file) # To what directory want we to unpack the file ? The same as the file : The one that where specified in {initialize} path = @directory == File.dirname(file) ? File.dirname(file) : @directory 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 [: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 [: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
157 158 159 |
# File 'lib/unpack.rb', line 157 def unrar(args) %x(cd '#{args[:path]}' && '#{@options[:absolute_path_to_unrar]}' e -y -o- '#{args[:file]}') end |
#unzip(args) ⇒ Object
161 162 163 |
# File 'lib/unpack.rb', line 161 def unzip(args) %x(unzip -n '#{args[:file]}' -d '#{args[:path]}') end |
#wipe! ⇒ Object
Removes the old rar and zip files
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/unpack.rb', line 124 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 [:force_remove] puts "Removing files in #{path}" if [: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 |