Class: Firecracker::TCPScraper

Inherits:
Base
  • Object
show all
Defined in:
lib/firecracker/tcp_scraper.rb

Instance Method Summary collapse

Methods inherited from Base

#debug?, #hashes, #initialize, #timeout, #valid?

Constructor Details

This class inherits a constructor from Firecracker::Base

Instance Method Details

#process!Object

Returns Hash Example: {

c2cff4acc8f5b49fd6b93b88fc0423467fbb08b0: {
  downloads: 123,
  complete: 456,
  incomplete: 789
}

}.

Returns:

  • Hash Example: {

    c2cff4acc8f5b49fd6b93b88fc0423467fbb08b0: {
      downloads: 123,
      complete: 456,
      incomplete: 789
    }
    

    }



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/firecracker/tcp_scraper.rb', line 17

def process!
  raise "both #tracker and #hashes/#hash must be set" unless valid?
  
  keys    = ["downloaded", "complete", "incomplete"]
  map     = {:complete => :seeders, :incomplete => :leechers, :downloaded => :downloads} 
  results = Hash.new { |h,k| h[k] = 0 }
  
  raise %q{
    Someting went wrong.
    You've passed multiply hashes, but the 
    tracker only responed with one result.
  } if files.empty? and not @options[:hashes].one?
  
  if files.empty?
    keys.each do |key|
      replace = map[key.to_sym] ? map[key.to_sym] : key
      results[replace.to_sym] += raw_hash[key].to_i
    end
    
    return {
      hashes.first => results
    }
  end
  
  files.keys.each do |key|
    file = files[key]
    results.merge!({
      key => {
        downloads: file["downloaded"],
        seeders: file["complete"],
        leechers: file["incomplete"]
      }
    })
  end
  
  return results
end