[Ruby] [Go To Travel] I searched for a plan with Quocard in Jalan

1 minute read

using docker

docker-compose.yml


version: "3.7"
services:
  chrome:
    image: selenium/standalone-chrome
    shm_size: 256m

  backend:
    build:
      context: ./backend
    working_dir: /app
    volumes:
      -./backend:/app
    command: ash -c "tail -f README.md"

Since it is only necessary to do rails c, command is tail -f so that backend does not end even if docker-compose up is done.

Then use docker exec -it backend_1 ash command to enter.

selenium/standalone-chrome is called from the rails side

models/concerns/chromeable.rb


require "selenium-webdriver"
require "capybara"

module Chromeable
  extend ActiveSupport::Concern

  DOCKER_CHROME_SELENIUM_HOST_NAME = "chrome"
  Capybara.register_driver :chrome do |app|
    args = %w[
      --headless
      --disable-gpu
      --window-size=1280,800
      --blink-settings=imagesEnabled=false
    ]

    caps =
      Selenium::WebDriver::Remote::Capabilities.chrome(
        "goog:chromeOptions" => {
          args: args,
          prefs: {
            credentials_enable_service: false,
            profile: {password_manager_enabled: false}
          }
        }
      )

    Capybara::Selenium::Driver.new(
      app,
      browser: :remote,
      desired_capabilities: caps,
      url: "http://#{DOCKER_CHROME_SELENIUM_HOST_NAME}:4444/wd/hub"
    )
  end

  included {}

  module ClassMethod; end

  def create_session
    session = Capybara::Session.new(:chrome)
    if block_given?
      begin
        yield session
      ensure
        session&.driver&.quit
      end
    else
      session
    end
  end
end

Make a concern to call such a chrome service of docker-compose, search Jalan, and pick up the plan name that has /([\d,]+) yen/. And save it in the database. Do this to the last page.

models/Jalan.rb


class Jalan <ApplicationRecord
  class Crawler
    include Chromeable

    def ensure_session
      @session = create_session
      session.visit "https://www.jalan.net/uw/uwp2011/uww2011init.do?keyword=%83N%83I%83J%81%5B%83h&distCd=06&rootCd=7701&screenId=FWPCTOP&image1.x=11&image1.y=13 "
      session.select "1", from: "adultNum"
      session.click_on "Research"

      yield
    ensure
      session&.driver&.quit
    end

    ...
  end
end

Result

Jalan.group(:value).count
=> {3000=>201, 4000=>15, 5000=>37, 6000=>3, 7000=>2, 7700=>2, 8000=>1, 10000=>3, 30000=>2}

There is noise, but…

Great plan I found

I don’t know what you’re saying at a 50,000 yen hotel with a 30,000 yen quo card. The JCB gift card with 10,000 yen seems to be much easier to use than the Kuo card.