def agent_init auto_login=true
@agent.instance_eval do
alias raw_get get
alias raw_post post
def set_account(mail, password) @mail=mail; @password=password end
def authenticated?(page)
page.header['x-niconico-authflag'] != '0'
end
def login
raise ArgError unless (@mail && @password)
account = {'mail' => @mail, 'password' => @password }
res = raw_post('https://secure.nicovideo.jp/secure/login?site=niconico', account)
raise LoginError unless authenticated?(res)
end
end
if auto_login
@agent.instance_eval do
@wait_time = 3
def get(*args) try(:raw_get, *args) end
def post(*args) try(:raw_post, *args) end
def try(name, *args)
page = method(name).call(*args)
unless authenticated?(page)
self.login
sleep @wait_time
page = method(name).call(*args)
raise LoginError unless authenticated?(page)
end
page
end
end
end
end