Skip to content

Instantly share code, notes, and snippets.

@tondol
Created January 23, 2020 04:04
Show Gist options
  • Save tondol/22ec21443e1ef56b35c2f3f5c104dd4b to your computer and use it in GitHub Desktop.
Save tondol/22ec21443e1ef56b35c2f3f5c104dd4b to your computer and use it in GitHub Desktop.
日の丸自動車学校の予約キャンセルを検知するLINE BOT
# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem "watir"
gem "selenium-webdriver", "~> 3.7.0"
gem "line-bot-api", "~> 1.13.0"
gem "mechanize"
require 'mechanize'
require 'pp'
require 'yaml'
require 'line/bot'
LOGIN_URL = 'https://www.e-license.jp/el25/mobile/m01c.action?b.schoolCd=Mudjt%2Bz0xsM%2BbrGQYS%2B1OA%3D%3D&b.processCd=R&dumy=1576910605383'
LOGIN_ID = '00000'
LOGIN_PASSWORD = '****'
DIRECTORY = File.dirname(__FILE__)
FILENAME = "#{DIRECTORY}/reserved.yaml"
LINE_CHANNEL_ID = '0000000000'
LINE_CHANNEL_SECRET = '****'
LINE_CHANNEL_TOKEN = '****'
def extract_reserved_hash(agent)
dates = agent.page.links_with(text: /\d+月\d+日\(.\)/).map {|e| e.text }[0...7]
marks = agent.page.search("form[@name='m03a']").inner_text.scan(/[-JOX ]+/)[0...7]
dates.zip(marks).first(3).to_h # first 3 days
end
def diff_hash(h1, h2)
h = {}
h2.each_pair {|key, value2|
value1 = h1[key]
next unless value1
# notify if there are cancelled hours
if value2.count("O") > value1.count("O")
h[key] = value2
end
}
h
end
agent = Mechanize.new
agent.get(LOGIN_URL)
agent.page.form_with(name: 'm01a') {|f|
f.field_with(name: 'b.studentId').value = LOGIN_ID
f.field_with(name: 'b.password').value = LOGIN_PASSWORD
f.click_button
}
reserved_hash = {}
agent.page.link_with(text: '技能予約').click
reserved_hash.merge!(extract_reserved_hash(agent))
old_reserved_hash = {}
old_reserved_hash = YAML.load_file(FILENAME) if File.exist?(FILENAME)
diff_reserved_hash = diff_hash(old_reserved_hash, reserved_hash)
client ||= Line::Bot::Client.new {|config|
config.channel_id = LINE_CHANNEL_ID
config.channel_secret = LINE_CHANNEL_SECRET
config.channel_token = LINE_CHANNEL_TOKEN
}
unless diff_reserved_hash.empty?
diff = diff_reserved_hash.each_pair.map {|e| "#{e[0]} : #{e[1]}" }.join("\n")
res = client.broadcast({
type: 'text',
text: "技能予約のキャンセルが発生したYO\n\n#{diff}\n\n#{LOGIN_URL}"
})
end
File.open(FILENAME, 'w') {|f| f.write(reserved_hash.to_yaml) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment