Skip to content

Instantly share code, notes, and snippets.

@gouf
Created July 24, 2016 18:17
Show Gist options
  • Save gouf/15a59a87b7222e718175ed61e13196c8 to your computer and use it in GitHub Desktop.
Save gouf/15a59a87b7222e718175ed61e13196c8 to your computer and use it in GitHub Desktop.
Mechanize による診断メーカーの情報取得とオストンインゲーム例
require_relative 'shindan_scrape'
module Ofuton
def self.in(user_name, hide_url: true)
result = ShindanScrape.new(362_791, user_name).shindan
if hide_url
cutoff_last_url(result)
else
result
end
end
def self.cutoff_last_url(result)
result.lines.first(result.lines.size - 1).join
end
end
puts Ofuton.in('gouf')
# =>
# [____]  (:3 っ)=≡=-・∴
# goufのオストンチャレンジ!
# ↓
# [____]  (:3 っ) (失敗:-122cm)床で寝てください
# https://shindanmaker.com/362791
require 'mechanize'
class ShindanScrape
SHINDAN_URL = 'https://shindanmaker.com'.freeze
def initialize(shindan_id, user_name)
@user_name = user_name
agent = Mechanize.new
@page = agent.get("#{SHINDAN_URL}/#{shindan_id}")
end
def shindan
shindan_result(filled_form)
end
private
def filled_form
form = @page.form_with(id: 'form')
form.field_with(name: 'u') do |input|
input.value = @user_name
end
form
end
def shindan_result(filled_form)
page = filled_form.submit
page.css('#copy_text_140').text
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment