Class: TorrentRename

Inherits:
Object
  • Object
show all
Defined in:
lib/torrent-rename.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ TorrentRename

Returns a new instance of TorrentRename.



6
7
8
9
10
11
12
# File 'lib/torrent-rename.rb', line 6

def initialize(path)
  self.path=path
  file = File.open(self.path, 'rb')
  b_str = file.read.strip
  file.close
  @decode_obj=b_str.bdecode
end

Instance Attribute Details

#decode_objObject

Returns the value of attribute decode_obj.



4
5
6
# File 'lib/torrent-rename.rb', line 4

def decode_obj
  @decode_obj
end

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/torrent-rename.rb', line 4

def path
  @path
end

Instance Method Details

#hashed_file_name(name) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/torrent-rename.rb', line 41

def hashed_file_name(name)
  reg = /(^.+)(\.\w+$)/
  reg =~ name
  if $1
    $1.hash.to_s + $2
  else
    name.hash.to_s
  end
end

#replace_fileObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/torrent-rename.rb', line 14

def replace_file
  decode_obj["info"]["name"] = hashed_file_name(decode_obj["info"]["name"])
  if decode_obj["info"].has_key? "name.utf-8"
    decode_obj["info"]["name.utf-8"] = hashed_file_name(decode_obj["info"]["name.utf-8"])
  end
  if decode_obj["info"].has_key? "files"
    decode_obj["info"]["files"].each_with_index do |f,i|
        ["path","path.utf-8"].each do |p|
          if decode_obj["info"]["files"][i].has_key? p
            decode_obj["info"]["files"][i][p].each_with_index do |d,j |
              if decode_obj["info"]["files"][i][p][j]
                decode_obj["info"]["files"][i][p][j] =
                  hashed_file_name(decode_obj["info"]["files"][i][p][j] )
              end
            end
          end
        end
    end
  end
  encode_str = decode_obj.bencode
  final_name = File.join File.dirname(path),hashed_file_name(File.basename(path))
  n_file = File.new final_name , "w"
  n_file.puts encode_str
  n_file.close
  puts "็”Ÿๆˆๆ–‡ไปถ:#{final_name}"
end