class TorrentkittyClient::Worker

Attributes

host[RW]
id[RW]

Public Class Methods

new(id, host) click to toggle source
# File lib/torrentkitty_client/worker.rb, line 12
def initialize(id, host)
  uri = URI.parse(host)

  @id = id
  @host = "#{uri.scheme}://#{uri.host}"
end

Public Instance Methods

execute() click to toggle source
# File lib/torrentkitty_client/worker.rb, line 19
def execute
  list = fetch_list
  content_page_infos = []

  list.each do |result|
    content_page_infos << Thread.new do
      fetch_content_page_info(result.detail_page_path)
    end
  end

  content_page_infos = content_page_infos.map(&:value)

  list.map.with_index do |result, index|
    content_page_info = content_page_infos[index]

    result.magnet_link = content_page_info.magnet_link
    result.content_size = content_page_info.content_size

    result
  end
end
fetch_content_page_info(detail_page_path) click to toggle source
# File lib/torrentkitty_client/worker.rb, line 71
def fetch_content_page_info(detail_page_path)
  conn = Faraday.new(url: host)
  response = conn.get detail_page_path

  html_doc = Nokogiri::HTML(response.body)

  content_size = html_doc.css("#torrentDetail")
                         .first.children[3]
                         .children.last
                         .text

  magnet_link = html_doc.css(
    "#main > div.wrapper > div.ta-holder.center > textarea"
  ).first.text

  ContentPageInfo.new(content_size, magnet_link)
end
fetch_list() click to toggle source
# File lib/torrentkitty_client/worker.rb, line 41
def fetch_list
  conn = Faraday.new(url: host)
  response = conn.get index_path

  html_doc = Nokogiri::HTML(response.body)

  parse_list_page(html_doc)
end
index_path() click to toggle source
# File lib/torrentkitty_client/worker.rb, line 89
def index_path
  "/search/#{URI.escape(id)}/"
end
parse_list_page(html_doc) click to toggle source
# File lib/torrentkitty_client/worker.rb, line 50
def parse_list_page(html_doc)
  list = []

  html_doc.css("#archiveResult").first.css("tr").each_with_index do |tr_element, index|
    next if index.zero?
    tds = tr_element.children

    return [] if tds[0].text =~ /No result/

    result = Result.new
    result.name = tds[0].text
    result.torrent_size = tds[1].text
    result.upload_date = tds[2].text
    result.detail_page_path = tds[3].children[0][:href]

    list << result
  end

  list
end