Class: LoyalSpider::Clients::Haha365::ArticleEntityLister

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

Instance Method Summary collapse

Methods included from EntityListerAble

included

Instance Method Details

#after_fetch_fail(result) ⇒ Object



86
87
88
# File 'lib/loyal_spider/clients/haha365/article_entity_lister.rb', line 86

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
76
77
78
79
80
81
82
83
84
# File 'lib/loyal_spider/clients/haha365/article_entity_lister.rb', line 22

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

  # _basic_doc = html_doc.css('html #main .content .left .r_c .cat_llb')
  _basic_doc = html_doc.css('html #main .content .left .r_c')

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

  (0...(_basic_doc.css('.cat_llb .fl a').size)).each do |_index|

    _title_doc    = _basic_doc.css('.cat_llb h3 a')[_index]
    _content_doc  = _basic_doc.css('.cat_llb #endtext')[_index]
    _category_doc = _basic_doc.css('.cat_llb .fl a')[_index]

    if _title_doc.nil? || _content_doc.nil? || _category_doc.nil?
      next
    end

    _entity_attr = {}

    _text_content = _content_doc.try :inner_html

    _content = _text_content.to_s.split("<br>\r\n").map do |_cnt|
      "<p>#{(Sanitize.clean _cnt).to_s.strip}</p>"
    end.join('')

    _entity_attr[:content] = _content

    _entity_attr[:url] = "#{self.base_url}#{_title_doc.attr('href')}"
    _entity_attr[:title] = "#{_title_doc.text}"

    if _category_doc
      _entity_attr[:tags] = [
        {
          :text => _category_doc.text,
          :href => "#{self.base_url}#{_category_doc.attr('href')}"
        }
      ]
    else
      _entity_attr[:tags] = []
    end

    _entity_attr[:authors] = []

    _entity_attr[:up_rating]      = -1
    _entity_attr[:down_rating]    = -1
    _entity_attr[:comments_count] = -1

    _entity = self.new_entity(_entity_attr)

    if _entity.valid?
      self.add_entity _entity
    end
  end

  # debugger
end

#before_fetch(options = {}) ⇒ Object

TODO:



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

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