class Torrenter::TorrentReader

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

Attributes

pieces[R]
stream[R]

Public Class Methods

new(file) click to toggle source
# File lib/torrenter/torrent_reader.rb, line 10
def initialize(file)
  @stream = BEncode.load_file(file)
end

Public Instance Methods

announce_list() click to toggle source
# File lib/torrenter/torrent_reader.rb, line 54
def announce_list
  @stream['announce-list']
end
announce_url() click to toggle source
# File lib/torrenter/torrent_reader.rb, line 50
def announce_url
  @stream['announce']
end
connect_trackers() click to toggle source
# File lib/torrenter/torrent_reader.rb, line 102
def connect_trackers
  trackers.map { |tracker| tracker.connect }
end
file_list() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/torrenter/torrent_reader.rb, line 22
def info_hash
  Digest::SHA1.digest(@stream['info'].bencode)
end
multiple_file_size() click to toggle source
# 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?() click to toggle source
# File lib/torrenter/torrent_reader.rb, line 62
def multiple_files?
  file_list.size > 1
end
piece_index() click to toggle source
# 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() click to toggle source
# File lib/torrenter/torrent_reader.rb, line 38
def piece_length
  @stream['info']['piece length']
end
sha_hashes() click to toggle source
# File lib/torrenter/torrent_reader.rb, line 26
def sha_hashes
  @stream['info']['pieces']
end
total_file_size() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/torrenter/torrent_reader.rb, line 58
def url_list
  announce_list ? announce_list.flatten << announce_url : [announce_url]
end
write_path(file) click to toggle source
# 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() click to toggle source
# File lib/torrenter/torrent_reader.rb, line 66
def write_paths
  file_list.each { |file| write_path(file) }
end