class Ruboty::Bokete::Client

Constants

BOKETE_URL

Public Class Methods

new(max_results: 30) click to toggle source
# File lib/ruboty/bokete/client.rb, line 8
def initialize(max_results: 30)
  @max_results = max_results
  @agent = Mechanize.new
end

Public Instance Methods

get(mode) click to toggle source
# File lib/ruboty/bokete/client.rb, line 13
def get(mode)
  results = []

  @agent.get("#{BOKETE_URL}/boke/#{mode}") do |page|
    target_page = page

    while results.size < @max_results
      images = scraping_from_page(target_page)
      p images
      results.concat(images)

      target_page = get_next_page(target_page)
      break if target_page.nil?
    end
  end

  results
end
get_next_page(page) click to toggle source
# File lib/ruboty/bokete/client.rb, line 46
def get_next_page(page)
  page.links_with(href: /\?page/)[-1].click rescue nil
end
scraping_from_page(page) click to toggle source
# File lib/ruboty/bokete/client.rb, line 32
def scraping_from_page(page)
  page.search('.boke').map {|node|
    node.children.each_with_object({}) do |child, hash|
      case child.name
      when 'div'
        img = child.children.children.children.detect {|o| o.name == 'img' }
        hash[:url] ||= img.attribute('src').value rescue nil
      when 'h3'
        hash[:boke] = child.text
      end
    end
  }.reject(&:empty?)
end