class NogizakaBlog::Amazing

Attributes

member_size[R]
yearmonth[RW]

Public Class Methods

new(yearmonth) click to toggle source
# File lib/nogizaka_blog/amazing.rb, line 13
def initialize(yearmonth)
  @yearmonth = yearmonth
  @member_size = MEMBER.size
end

Public Instance Methods

each() { |name, sum, article| ... } click to toggle source
# File lib/nogizaka_blog/amazing.rb, line 23
def each
  MEMBER.keys.each do |name|
    max = max_page(name)
    @comment = []

    # @comment = 0 when redirected
    if max == -1
      @comment << 0
    elsif max == 0
      url = "http://blog.nogizaka46.com/#{name}/?d=#{@yearmonth}"
      comment_size(url)
    else
      (1..max).each do |n|
        url = "http://blog.nogizaka46.com/#{name}/?p=#{n}&d=#{@yearmonth}"
        comment_size(url)
      end
    end

    if max == -1
      article = 0
    else
      article = @comment.length
    end

    yield name, @comment.sum, article
  end
end
scraping(&block) click to toggle source
# File lib/nogizaka_blog/amazing.rb, line 18
def scraping(&block)
  warn "\e[31m[DEPRECATION] `scraping` is deprecated.  Please use `each` instead.\e[0m"
  each(&block)
end

Private Instance Methods

comment_size(url) click to toggle source
# File lib/nogizaka_blog/amazing.rb, line 79
def comment_size(url)
  doc = Nokogiri::HTML(open(url, 'User-Agent' => 'firefox'))
  doc.css('.entrybottom a:last').each do |link|
    link.content.scan(/\d+/) { |c| @comment << c }
  end
rescue => e
  puts "\e[31mSome failure: #{e}\e[0m"
end
max_page(name) click to toggle source
# File lib/nogizaka_blog/amazing.rb, line 53
def max_page(name)
  nbsp = Nokogiri::HTML('&nbsp;').text

  url = "http://blog.nogizaka46.com/#{name}/?d=#{@yearmonth}"
  response = Typhoeus.get(url).code

  # Be redirected if it doesn't exist @yearmonth.
  case response
  when 200
    doc = Nokogiri::HTML(open(url, 'User-Agent' => 'firefox'))
    paginate_class = doc.css('.paginate:first a:nth-last-child(2)')
    if paginate_class.empty?
      num = 0
    else
      paginate_class.each do |link|
        num = link.content
      end
      num.gsub!(nbsp, '')
    end
  when 302
    num = -1
  end

  num.to_i
end