Class: Specific::JiYing

Inherits:
Object
  • Object
show all
Defined in:
lib/spider_rails/specific/ji_ying.rb

Overview

Download JiYing resources example:

@page = ::Spider.open_browser(:phantomjs, 'http://bt.ktxp.com/sort-50-1.html')
jy = ::Spider::JiYing.new(@page)
jy.full_site

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page) ⇒ JiYing

Returns a new instance of JiYing.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/spider_rails/specific/ji_ying.rb', line 10

def initialize page
  @ani = Hash.new
  @anis = Array.new
  @page = page
  @base_url = page.url.sub(/\d.html$/, '')
  if @base_url && @base_url.include?('search')
    @mode = 'search'
  else
    @mode = 'normal'
  end

end

Instance Attribute Details

#aniObject

Returns the value of attribute ani.



8
9
10
# File 'lib/spider_rails/specific/ji_ying.rb', line 8

def ani
  @ani
end

#anisObject

Returns the value of attribute anis.



8
9
10
# File 'lib/spider_rails/specific/ji_ying.rb', line 8

def anis
  @anis
end

#pageObject

Returns the value of attribute page.



8
9
10
# File 'lib/spider_rails/specific/ji_ying.rb', line 8

def page
  @page
end

Instance Method Details

#ani_countObject



87
88
89
# File 'lib/spider_rails/specific/ji_ying.rb', line 87

def ani_count
  @anis.uniq.count if @anis
end

#final_pageObject



46
47
48
49
50
51
52
53
# File 'lib/spider_rails/specific/ji_ying.rb', line 46

def final_page
  if @mode == 'search'
    fp = @page.element(css: '.title h2 a').text[/\(.+\)/].gsub!(/\(|\)/, '').to_i/100 + 1
  else
    fp = @page.element(css: 'a.pager-last').text.to_i
  end
  fp
end

#full_page(page_num) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/spider_rails/specific/ji_ying.rb', line 55

def full_page page_num
  case @mode
    when 'search'
      @page.goto "#{@base_url}#{page_num}"

    when 'normal'
      @page.goto "#{@base_url}#{page_num}.html"
  end
  html = ::Nokogiri::HTML.parse @page.html

  html.css('.ltext').each do |td|
    single(td)
  end
end

#full_siteObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/spider_rails/specific/ji_ying.rb', line 23

def full_site
  fp = final_page
  begin
    (1..fp).each do |page_num|
      full_page page_num
    end
    p_anis
  rescue Exception
    raise %Q(page isn't not exist)
  end
end

#get(element) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/spider_rails/specific/ji_ying.rb', line 75

def get(element)
  element.css('a.quick-down').each do |a|
    @ani[:torrent] = 'http://bt.ktxp.com' + a['href']
  end
  @ani[:title],
      @ani[:size],
      @ani[:finish] = get_content(element, 'a.quick-down+a', 'td.ltext+td', 'td.ltext+td+td+td+td')
  p "Get Animation: #{@ani[:title]}"

  @anis << @ani.dup
end

#get_content(element, *selectors) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/spider_rails/specific/ji_ying.rb', line 91

def get_content(element, *selectors)
  tds = Array.new
  selectors.each do |selector|
    element.css(selector).each do |td|
      tds << td.text
    end
  end
  tds
end

#multi_pages(final_page_num) ⇒ Object



35
36
37
38
39
40
# File 'lib/spider_rails/specific/ji_ying.rb', line 35

def multi_pages final_page_num
  (1..final_page_num).each do |page_num|
    full_page page_num
    p_anis
  end
end

#p_anisObject



42
43
44
# File 'lib/spider_rails/specific/ji_ying.rb', line 42

def p_anis
  p "@anis is #{@anis}"
end

#single(element) ⇒ Object



70
71
72
73
# File 'lib/spider_rails/specific/ji_ying.rb', line 70

def single element
  get(element)
  @anis
end