Class: Torrenter::TorrentReader
- Inherits:
-
Object
- Object
- Torrenter::TorrentReader
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
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
#pieces ⇒ Object
Returns the value of attribute pieces.
8
9
10
|
# File 'lib/torrenter/torrent_reader.rb', line 8
def pieces
@pieces
end
|
#stream ⇒ Object
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_list ⇒ Object
54
55
56
|
# File 'lib/torrenter/torrent_reader.rb', line 54
def announce_list
@stream['announce-list']
end
|
#announce_url ⇒ Object
50
51
52
|
# File 'lib/torrenter/torrent_reader.rb', line 50
def announce_url
@stream['announce']
end
|
#connect_trackers ⇒ Object
102
103
104
|
# File 'lib/torrenter/torrent_reader.rb', line 102
def connect_trackers
trackers.map { |tracker| tracker.connect }
end
|
#file_list ⇒ Object
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
|
#folder ⇒ Object
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_hash ⇒ Object
22
23
24
|
# File 'lib/torrenter/torrent_reader.rb', line 22
def info_hash
Digest::SHA1.digest(@stream['info'].bencode)
end
|
#multiple_file_size ⇒ Object
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
62
63
64
|
# File 'lib/torrenter/torrent_reader.rb', line 62
def multiple_files?
file_list.size > 1
end
|
#piece_index ⇒ Object
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_length ⇒ Object
38
39
40
|
# File 'lib/torrenter/torrent_reader.rb', line 38
def piece_length
@stream['info']['piece length']
end
|
#sha_hashes ⇒ Object
26
27
28
|
# File 'lib/torrenter/torrent_reader.rb', line 26
def sha_hashes
@stream['info']['pieces']
end
|
#total_file_size ⇒ Object
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_params ⇒ Object
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
|
#trackers ⇒ Object
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_list ⇒ Object
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_paths ⇒ Object
66
67
68
|
# File 'lib/torrenter/torrent_reader.rb', line 66
def write_paths
file_list.each { |file| write_path(file) }
end
|