Class: LoyalSpider::Clients::Lengxiaohua::ArticleEntityLister

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

Instance Method Summary collapse

Methods included from EntityListerAble

included

Instance Method Details

#after_fetch_fail(result) ⇒ Object



99
100
101
# File 'lib/loyal_spider/clients/lengxiaohua/article_entity_lister.rb', line 99

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
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/loyal_spider/clients/lengxiaohua/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('.joke_wrap li.joke_li').each do |entity_doc|
    _entity_attr = {}

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

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

    _joke_id = entity_doc.css('.para_info .para_tool a').first.attr('jokeid')

    _entity_attr[:url] = "#{_base_url}/joke/#{_joke_id}"

    _entity_attr[:title] = ''

    _text_content = entity_doc.css('.para_can pre').first.inner_html

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

    if _img_box = entity_doc.css('.default_load_imgbox').first

      _image_content = _img_box.css('img').map do |_img|
        "<img src='#{_img.attr('data-original').to_s.gsub('!water', '')}'/>"
      end

      _content = _content + "<p>#{_image_content.join('')}</p>" if _image_content.any?
    end

    _entity_attr[:content] = _content

    _entity_attr[:tags] = entity_doc.css('.tag_box a').map do |_tag_doc|
      {
        :text => _tag_doc.text,
        :href => "#{self.base_url}#{_tag_doc.attr('href')}"
      }
    end

    # debugger

    _author_doc = entity_doc.css('.para_info .user_info a').first

    if _author_doc
      _entity_attr[:authors] = [
        {
          :text => _author_doc.text.to_s.strip,
          :href => "#{self.base_url}#{_author_doc.attr('href').to_s.strip}"
        }
      ]
    else
      _entity_attr[:authors] = []
    end

    _tool_doc = entity_doc.css('.para_tool')

    _entity_attr[:up_rating]      = _tool_doc.css('a[report=like_joke] span').last.text.gsub(/\W/, '').to_i
    _entity_attr[:down_rating]    = _tool_doc.css('a[report=unlike_joke] span').last.text.gsub(/\W/, '').to_i
    _entity_attr[:comments_count] = _tool_doc.css("#show_comment_count_#{_joke_id}").text.gsub(/\W/, '').to_i

    _entity = self.new_entity(_entity_attr)

    if _entity.valid?
      self.add_entity _entity
    end
  end

end

#before_fetch(options = {}) ⇒ Object

TODO:



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

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