module Pirata

Public Class Methods

config() click to toggle source
# File lib/pirata.rb, line 26
def self.config
  @config
end
configure(opts = nil) click to toggle source
# File lib/pirata.rb, line 16
def self.configure(opts = nil)
  return if opts.nil?

  if opts.class == Hash
    opts.each do |k, v|
      @config[k.to_sym] = v if self.valid_key?(k)
    end
  end
end
parse_search_page(html, search_object = nil) click to toggle source

From a results table, collect and build all Torrents into an array

# File lib/pirata/search.rb, line 77
def self.parse_search_page(html, search_object = nil)
  results = []

  begin
    search_object.pages = html.css('#content div a')[-2].text.to_i
  rescue
  end

  html.css('#searchResult tr').each do |row|
    title = row.search('.detLink').text
    next if title == ''
    h = {}

    begin
      h[:title]       = title
      h[:category]    = row.search('td a')[0].text
      url             = Pirata.config[:base_url] + row.search('.detLink').attribute('href').to_s
      h[:url]         = url.gsub("[", "%5B").gsub("]", "%5D")
      h[:id]          = h[:url].split('/')[4].to_i
      h[:magnet]      = row.search('td a')[3]['href']
      h[:seeders]     = row.search('td')[2].text.to_i
      h[:leechers]    = row.search('td')[3].text.to_i
      h[:uploader]    = Pirata::User.new(row.search('.detDesc a')[0].text)
    rescue
      #puts "not found"
    end
    results << Pirata::Torrent.new(h)
  end
  results
end
parse_torrent_page(html) click to toggle source
# File lib/pirata/torrent.rb, line 60
def self.parse_torrent_page(html)
  if html.css("#err").text.include?("404")
    raise "No torrent for supplied ID found"
  else
    row = html.css('#detailsframe').first
    h = {
      :title     => row.search('#title')[0].text.strip,
      :files     => row.at_css('dt:contains("Files:")').next_element().text.to_i,
      :size      => row.at_css('dt:contains("Size:")').next_element().child.text,
      :date      => Time.parse(row.at_css('dt:contains("Uploaded:")').next_element().text),
      :uploader  => Pirata::User.new(row.at_css('dt:contains("By:")').next_element().text.strip),
      :seeders   => row.at_css('dt:contains("Seeders:")').next_element().text.to_i,
      :leechers  => row.at_css('dt:contains("Leechers:")').next_element().text.to_i,
      :comments  => row.at_css('dt:contains("Comments")').next_element().text.to_i,
      :hash      => row.search('dl').text.split.last.strip,
      :magnet    => row.search('.download a')[0]['href']
    }
    return h
  end
  nil
end
valid_key?(key) click to toggle source
# File lib/pirata.rb, line 12
def self.valid_key?(key)
  @config.keys.include?(key.to_sym)
end

Public Instance Methods

build_getters() click to toggle source

Initialize getters for +1 request variables, fetching them if we need them

# File lib/pirata/torrent.rb, line 51
def build_getters
  [:seeders, :leechers, :uploader, :files, :size, :date, :comments, :hash].each do |m|
    self.class.send(:define_method, m) {
      update_params! unless @params[m]
      @params[m]
    }
  end
end
parse_html(url) click to toggle source

Parse HTML body of a supplied URL

# File lib/pirata/search.rb, line 70
def parse_html(url)
  response = open(url, :allow_redirections => Pirata.config[:redirect])
  Nokogiri::HTML(response)
end