Module: Torrent

Defined in:
lib/torrent.rb

Overview

Created by me on 2007-12-28.

Copyright (c) 2007. All pwnage reserved.

Class Method Summary collapse

Class Method Details

.cleanObject



86
87
# File 'lib/torrent.rb', line 86

def clean
end

.fetchObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/torrent.rb', line 40

def fetch
  puts "[Log] Starting peer..." if @opts[:verbose]
  bt = RubyTorrent::BitTorrent.new(mi, package, :http_proxy => proxy)

  if @opts[:verbose]
    bt.on_event(self, :added_peer) { |s, p| puts "[Log] Connected to peer #{p}" }
    bt.on_event(self, :received_block) { |s, b, peer| puts "<-- got block #{b} from peer #{peer}, now #{package.pieces[b.pindex].percent_done.f}% done and #{package.pieces[b.pindex].percent_claimed.f}% claimed" }
    bt.on_event(self, :sent_block) { |s, b, peer| puts "--> Sent block #{b} to peer #{peer}" }
    bt.on_event(self, :discarded_piece) { |s, p| puts "[Log] Checksum error on piece #{p}, discarded" }
    bt.on_event(self, :tracker_connected) { |s, url| puts "--> Connected to tracker #{url}" }
    bt.on_event(self, :tracker_lost) { |s, url| puts "--> Couldn't connect to tracker #{url}" }
  end

  # Always register this
  bt.on_event(self, :complete) do
    @url.length = bt.dlamt
    puts "--> Download complete!"
  end

  puts "[Log] Listening on #{bt.ip}:#{bt.port}" if @opts[:verbose]


  thread = Thread.new do
    until bt.complete?
      previous = ''

      if bt.tracker

        current = '%s: %dk of %dk (%.2f%% complete)' % [Time.now,
                                                        bt.bytes_completed / 1024,
                                                        bt.total_bytes / 1024,
                                                        bt.percent_completed]
        print "\b" * previous.size
        print curent
        previous = ''

        sleep(5)
      else
        sleep(5)
      end
    end
  end

  thread.join
end

.prepObject



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
# File 'lib/torrent.rb', line 14

def prep
  begin
    mi = RubyTorrent::MetaInfo.from_location(File.join(@url.host, @url.path), ENV["http_proxy"])
  rescue RubyTorrent::MetaInfoFormatError, RubyTorrent::BEncodingError => e
    abort "[Error] Can't parse metainfo file '#{torrent}' -- maybe not a .torrent?"
  rescue RubyTorrent::TypedStructError => e
    raise e
  rescue IOError, SystemCallError => e
    abort "[Error] Can't read file '#{torrent}': #{e.message}"
  end


  if FileTest.directory?(@opts[:out]) && mi.info.single?
    dest = File.join(@opts[:out], mi.info.name)
  elsif FileTest.file?(@opts[:out]) && mi.info.multiple?
    abort "[Error] .torrent contains multiple files, but '#{dest}' is a single file (must be a directory)"
  end
  dest ||= @opts[:out]

  print "[LOG] Checking file status: " if @opts[:verbose]
  package = RubyTorrent::Package.new(mi, dest) do |piece|
    print(piece.complete? && piece.valid? ? "#" : ".") if @opts[:verbose]
  end
  puts " done" if @opts[:verbose]
end

.setup(opts, uri) ⇒ Object



9
10
11
12
# File 'lib/torrent.rb', line 9

def setup(opts, uri)
  @opts = opts
  @url = uri
end