Class: TorrentkittyClient::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/torrentkitty_client/worker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, host) ⇒ Worker

Returns a new instance of Worker.



12
13
14
15
16
17
# File 'lib/torrentkitty_client/worker.rb', line 12

def initialize(id, host)
  uri = URI.parse(host)

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

Instance Attribute Details

#hostObject

Returns the value of attribute host.



10
11
12
# File 'lib/torrentkitty_client/worker.rb', line 10

def host
  @host
end

#idObject

Returns the value of attribute id.



10
11
12
# File 'lib/torrentkitty_client/worker.rb', line 10

def id
  @id
end

Instance Method Details

#executeObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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_listObject



41
42
43
44
45
46
47
48
# 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_pathObject



89
90
91
# File 'lib/torrentkitty_client/worker.rb', line 89

def index_path
  "/search/#{URI.escape(id)}/"
end

#parse_list_page(html_doc) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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