Class: Torrenter::TorrentReader

Inherits:
Object
  • Object
show all
Defined in:
lib/torrenter/torrent_reader.rb,
lib/torrenter/torrent_reader/piece.rb,
lib/torrenter/torrent_reader/piece_index.rb,
lib/torrenter/torrent_reader/piece_constructor.rb

Overview

what is being used or accessed by the torrent reader, and what is being used by the trackers?

Defined Under Namespace

Classes: Piece, PieceConstructor, PieceIndex

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ TorrentReader

Returns a new instance of TorrentReader.



10
11
12
# File 'lib/torrenter/torrent_reader.rb', line 10

def initialize(file)
  @stream = BEncode.load_file(file)
end

Instance Attribute Details

#piecesObject (readonly)

Returns the value of attribute pieces.



8
9
10
# File 'lib/torrenter/torrent_reader.rb', line 8

def pieces
  @pieces
end

#streamObject (readonly)

Returns the value of attribute stream.



8
9
10
# File 'lib/torrenter/torrent_reader.rb', line 8

def stream
  @stream
end

Instance Method Details

#announce_listObject



54
55
56
# File 'lib/torrenter/torrent_reader.rb', line 54

def announce_list
  @stream['announce-list']
end

#announce_urlObject



50
51
52
# File 'lib/torrenter/torrent_reader.rb', line 50

def announce_url
  @stream['announce']
end

#connect_trackersObject



102
103
104
# File 'lib/torrenter/torrent_reader.rb', line 102

def connect_trackers
  trackers.map { |tracker| tracker.connect }
end

#file_listObject



42
43
44
45
46
47
48
# File 'lib/torrenter/torrent_reader.rb', line 42

def file_list
  if @stream['info']['files'].nil?
    [{ 'path' => [@stream['info']['name']], 'length' => @stream['info']['length'] }]
  else
    @stream['info']['files']
  end
end

#folderObject



14
15
16
17
18
19
20
# File 'lib/torrenter/torrent_reader.rb', line 14

def folder
  if multiple_files?
    @stream['info']['name']
  else
    @stream['info']['name'].gsub(/\.\w+$/, '')
  end
end

#info_hashObject



22
23
24
# File 'lib/torrenter/torrent_reader.rb', line 22

def info_hash
  Digest::SHA1.digest(@stream['info'].bencode)
end

#multiple_file_sizeObject



34
35
36
# File 'lib/torrenter/torrent_reader.rb', line 34

def multiple_file_size
  file_list.map { |file| file['length'] }.inject { |x,y| x + y }
end

#multiple_files?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/torrenter/torrent_reader.rb', line 62

def multiple_files?
  file_list.size > 1
end

#piece_indexObject



78
79
80
81
# File 'lib/torrenter/torrent_reader.rb', line 78

def piece_index
  constructor = PieceConstructor.new(file_list, folder, sha_hashes, piece_length)
  constructor.make_index
end

#piece_lengthObject



38
39
40
# File 'lib/torrenter/torrent_reader.rb', line 38

def piece_length
  @stream['info']['piece length']
end

#sha_hashesObject



26
27
28
# File 'lib/torrenter/torrent_reader.rb', line 26

def sha_hashes
  @stream['info']['pieces']
end

#total_file_sizeObject



30
31
32
# File 'lib/torrenter/torrent_reader.rb', line 30

def total_file_size
  file_list.is_a?(Array) ? multiple_file_size : file_list['length']
end

#tracker_paramsObject



83
84
85
86
87
88
89
90
# File 'lib/torrenter/torrent_reader.rb', line 83

def tracker_params
  {
    :info_hash => info_hash,
    :peer_id   => PEER_ID,
    :left      => piece_length,
    :pieces    => file_list
  }
end

#trackersObject



92
93
94
95
96
97
98
99
100
# File 'lib/torrenter/torrent_reader.rb', line 92

def trackers
  url_list.compact.map do |url|
    if url.include?('http://')
      Tracker::HTTPTracker.new(url, tracker_params)
    else
      Tracker::UDPTracker.new(url, tracker_params)
    end
  end
end

#url_listObject



58
59
60
# File 'lib/torrenter/torrent_reader.rb', line 58

def url_list
  announce_list ? announce_list.flatten << announce_url : [announce_url]
end

#write_path(file) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/torrenter/torrent_reader.rb', line 70

def write_path(file)
  file = "#{folder}/#{file['path'].join('/')}"
  if !Dir.exist?(File.dirname(file))
    FileUtils.mkdir_p(File.dirname(file))
  end
  IO.write(file, '', 0)
end

#write_pathsObject



66
67
68
# File 'lib/torrenter/torrent_reader.rb', line 66

def write_paths
  file_list.each { |file| write_path(file) }
end