Skip to content

Instantly share code, notes, and snippets.

@soh335
Created September 30, 2009 02:59
Show Gist options
  • Save soh335/197704 to your computer and use it in GitHub Desktop.
Save soh335/197704 to your computer and use it in GitHub Desktop.
*Growl
growl通知をするためには、システム環境設定->Growl->ネットワークで
受信される通知を聞く、リモートアプリケーションの登録を許可をonにする必要がある。
*Ruby
gem install mechanize
gem list | grep mechanize #=> mechanize (0.9.3)
require 'yaml'
require 'rubygems'
require 'mechanize'
class Wellness
def initialize(username, password)
url = 'https://wellness.sfc.keio.ac.jp/v3/'
agent = WWW::Mechanize.new
page = agent.get(url)
form = page.forms[0]
form.password = password
form.login = username
page = agent.submit(form)
page.search('//em[@class="error"]').each do |node|
raise Exception.new(node.text) if node.text == "login名またはパスワードが異なります."
end
page.links.each do |link|
if /reserve/ =~ link.href && /mode=select/ !~ link.href
@page = link.click
break
end
end
end
def select_day(day)
@page.links.each do |link|
if /#{day}/ =~ link.href
@page = link.click
return
end
end
raise Exception.new("この日付は存在しません: #{day}")
end
def reserve(name, time)
@page.search('//table[@class="cool"]/tr').each do |node|
if node.xpath('td[3]').text == name && node.xpath('td[1]').text == time.to_s+'限'
raise Exception.new("予約空きが0です") if node.xpath('td[8]').text == '0'
value = node.xpath('td[9]/input')[0].attribute('value')
begin
self.post_reserve(value)
return
rescue Exception => e
raise e
end
end
end
raise Exception.new("この時間に科目は存在しません")
end
protected
def post_reserve(value)
@page.forms[0].radiobuttons.each do |c|
if c.value == value.to_s
c.check
break
end
end
result = @page.forms[0].submit
result.search('//p[@class="error"]').each do |node|
raise Exception.new(node.text) unless node.text =~ /予約しました./
end
end
end
class Main
def run
while(1)
self.load
begin
wellness = Wellness.new(@username, @password)
rescue Exception => e
self.trace e.to_s
exit
end
for data in @class
begin
wellness.select_day data[0]
rescue Exception => e
self.trace e.to_s
next
end
for _class in data[1]
begin
wellness.reserve(_class[1], _class[0])
rescue Exception => e
self.trace "#{e.to_s}\n#{data[0]} #{_class[0]}限 #{_class[1]}の予約に失敗しました"
else
self.trace "#{data[0]} #{_class[0]}限 #{_class[1]}を予約しました"
end
end
end
wellness = nil
GC.start
sleep @sleep*60
end
end
protected
def load
config = 'wellness.yml'
return if @mtime && @mtime == File::stat(config).mtime
begin
yaml = YAML.load_file(config)
rescue Errno::ENOENT
puts "#{config} is not found"
exit
end
if yaml['growl'] == true
require 'ruby-growl'
@growl = Growl.new('localhost', 'wellness', ['event1', 'event2'], nil, '')
end
@username = yaml['username'] || alert('username')
@password = yaml['password'] || alert('password')
@class = yaml['class']
@print = yaml['print'] || true
@sleep = yaml['sleep'] || 30
@mtime = File::stat(config).mtime
self.trace("設定ファイルを読み込みました")
end
def alert(key)
puts "#{key} の項目が空です"
exit
end
def trace(text)
if @growl then
@growl.notify('event1', 'wellness',text)
end
if @print then
puts text
end
end
end
main = Main.new
main.run
sleep: 30
username: 'username'
password: 'password'
growl: false
print: true
class:
20091001:
3: バスケットボール
20091005:
2: ベースボール
20091006:
3: クラシックバレエ
4: バスケットボール
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment