Class: BTScraper::HTTPScrape

Inherits:
Object
  • Object
show all
Defined in:
lib/btscraper/httpscrape.rb

Overview

This class permits you to scrape an HTTP torrent tracker according to the BEP 48

See Also:

Author:

  • sherkix

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tracker, info_hash) ⇒ HTTPScrape

Create a new HTTPScrape object

Examples:

Default usage

scrape_object = BTScraper::HTTPScrape.new('https://example.com:443/scrape', ['c22b5f9178342609428d6f51b2c5af4c0bde6a42'], ['aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d'])
scrape_object.scrape

Parameters:

  • tracker (String)

    Bittorrent HTTP tracker server

  • info_hash (Array<String>, String)

    Array of infohashes or single infohash

Raises:

  • (TypeError)

    if wrong type of argument is provided



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/btscraper/httpscrape.rb', line 29

def initialize(tracker, info_hash)
  unless tracker.instance_of? String
    raise TypeError, "String excpected, got #{tracker.class}"
  end
  unless info_hash.instance_of? String or info_hash.instance_of? Array
    raise TypeError, "String or Array excpected, got #{info_hash.class}"
  end

  if info_hash.instance_of? String
    info_hash.downcase!
    BTScraper.check_info_hash Array(info_hash)
  else
    info_hash.map(&:downcase!)
    BTScraper.check_info_hash info_hash
  end
  @tracker = tracker
  @info_hash = Array(info_hash)
end

Instance Attribute Details

#info_hashArray<String> (readonly)

Returns array of infohashes

Returns:

  • (Array<String>)

    returns array of infohashes



29
30
31
# File 'lib/btscraper/httpscrape.rb', line 29

def info_hash
  @info_hash
end

#trackerString (readonly)

Returns tracker full url

Returns:

  • (String)

    returns tracker full url



29
30
31
# File 'lib/btscraper/httpscrape.rb', line 29

def tracker
  @tracker
end

Instance Method Details

#scrapeHash

Returns The method returns a hash with the scraped data.

Examples:

Response example

{"files" => {"xxxxxxxxxxxxxxxxxxxxxxxxxx" => {"complete" => 8, "downloaded" => 9, "incomplete" => 4}, "yyyyyyyyyyyyyyyyyyyyyyyyyy" => {"complete" => 81, "downloaded" => 204, "incomplete" => 23}, "zzzzzzzzzzzzzzzzzzzzzzzzzz" => {"complete" => 3, "downloaded" => 26, "incomplete" => 1}}}

Returns:

  • (Hash)

    The method returns a hash with the scraped data



50
51
52
53
54
55
56
57
58
# File 'lib/btscraper/httpscrape.rb', line 50

def scrape
  unhex_info_hash = @info_hash.map{|x| Binascii.a2b_hex(x)}
  params = unhex_info_hash.map{|h| "info_hash=#{CGI.escape(h.to_s)}"}.join('&')
  begin
    HTTParty.get(@tracker, :query => params, :headers => {'User-Agent' => "btscraper #{VERSION}"}, :timeout => 10).body.bdecode
  rescue HTTParty::Error => e
    raise BTScraperError, e
  end
end