class ZooqleSearch::Link

Object that contains the info for a torrent file

Attributes

download_url[R]
filename[R]
leechers[R]
magnet[R]
seeders[R]
size[R]

Public Class Methods

new(filename, size, magnet, download_url, seeders, leechers) click to toggle source
# File lib/zooqle_search/link.rb, line 10
def initialize(filename, size, magnet, download_url, seeders, leechers)
  @filename = filename
  @size = size
  @magnet = magnet
  @download_url = download_url
  @seeders = seeders.tr(',', '').to_i
  @leechers = leechers.tr(',', '').to_i
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/zooqle_search/link.rb, line 19
def <=>(other)
  @seeders <=> other.seeders
end
download(path = './') click to toggle source
# File lib/zooqle_search/link.rb, line 31
def download(path = './')
  raise 'No download url provided' unless @download_url

  response = HTTParty.get(@download_url)

  raise 'Wrong content-type. Aborting.' unless response.headers['content-type'].include? 'application/x-bittorrent'

  # Get file name from the url
  filename = @filename + '.torrent'
  open(File.join(path, filename), 'w') { |f| f << response }

  # return filename
  filename
end
info_hash() click to toggle source
# File lib/zooqle_search/link.rb, line 27
def info_hash
  @info_hash ||= extract_hash
end
to_s() click to toggle source
# File lib/zooqle_search/link.rb, line 23
def to_s
  "#{@filename} (#{@size}) - [#{@seeders.to_s.green}/#{@leechers.to_s.red}]"
end

Private Instance Methods

extract_hash() click to toggle source
# File lib/zooqle_search/link.rb, line 48
def extract_hash
  # Extract magnet properties to a Hash and then parse the sha1 info hash
  raw_hash = magnet[/(xt.*?)&/, 1]  # extract the xt property
  raw_hash.split(':').last.downcase
end