Class: LoyalSpider::Clients::KuaileMahua::ArticleEntityLister

Inherits:
Object
  • Object
show all
Includes:
EntityListerAble
Defined in:
lib/loyal_spider/clients/kuaile_mahua/article_entity_lister.rb

Instance Method Summary collapse

Methods included from EntityListerAble

included

Instance Method Details

#after_fetch_fail(result) ⇒ Object



77
78
79
# File 'lib/loyal_spider/clients/kuaile_mahua/article_entity_lister.rb', line 77

def after_fetch_fail result
  puts "after_fetch fail: #{result}"
end

#after_fetch_success(result) ⇒ Object

TODO



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/loyal_spider/clients/kuaile_mahua/article_entity_lister.rb', line 22

def after_fetch_success result
  # puts "after_fetch success: #{result}"
  html_doc = result.response_html_doc

  html_doc.css('.main .main-left .xiaohua').each do |entity_doc|
    _entity_attr = {}

    _fetch_options = result.fetch_options
    _base_url      = _fetch_options.base_url.to_s.strip

    _title_link = entity_doc.css('h3 a').first

    # :content         # 正文
    # :tags            # 标签
    # :up_rating       # 好评数
    # :down_rating     # 差评数目
    # :comments_count  # 评论数目
    # :authors         # 抓取的作者信息

    _entity_attr[:url]   = "#{_base_url}#{_title_link.attr('href').to_s.strip}"

    _entity_attr[:title] = _title_link.text
    _entity_attr[:content] = entity_doc.css('.content').inner_html
    _entity_attr[:tags] = entity_doc.css('.link .tags h4 a').map do |_tag_doc|
      {
        :text => _tag_doc.text.to_s.strip,
        :href => "#{_base_url}#{_tag_doc.attr('href').to_s.strip}"
      }
    end

    _entity_attr[:authors] = entity_doc.css('.link .tags .pusher a').map do |_author_doc|
      {
        :text => _author_doc.text.to_s.strip,
        :href => "#{_author_doc.attr('href').to_s.strip}"
      }
    end

    _entity_attr[:up_rating]      = entity_doc.css('.tools li a.good').text.to_i
    _entity_attr[:down_rating]    = entity_doc.css('.tools li a.bad').text.to_i
    _entity_attr[:comments_count] = entity_doc.css('.tools li s').first.text.to_i

    _entity = self.new_entity(_entity_attr)

    if entity_doc.css('.content .more a').any?
      _entity.fetch
    end

    if _entity.valid?
      self.add_entity _entity
    end
  end

  # debugger
end

#before_fetch(options = {}) ⇒ Object

TODO:



17
18
19
# File 'lib/loyal_spider/clients/kuaile_mahua/article_entity_lister.rb', line 17

def before_fetch options={}
  puts "before_fetch: #{options}"
end