Class: TorrentRename
- Inherits:
-
Object
- Object
- TorrentRename
- Defined in:
- lib/torrent-rename.rb
Instance Attribute Summary collapse
-
#decode_obj ⇒ Object
Returns the value of attribute decode_obj.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
- #hashed_file_name(name) ⇒ Object
-
#initialize(path) ⇒ TorrentRename
constructor
A new instance of TorrentRename.
- #replace_file ⇒ Object
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_obj ⇒ Object
Returns the value of attribute decode_obj.
4 5 6 |
# File 'lib/torrent-rename.rb', line 4 def decode_obj @decode_obj end |
#path ⇒ Object
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_file ⇒ Object
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 |