Class: Discuz::Robot

Inherits:
Object
  • Object
show all
Defined in:
lib/discuz_robot.rb

Instance Method Summary collapse

Constructor Details

#initialize(base_url = "http://localhost/bbs/") ⇒ Robot

Returns a new instance of Robot.



6
7
8
9
# File 'lib/discuz_robot.rb', line 6

def initialize base_url = "http://localhost/bbs/"
  @base_url  = base_url
  @agent     = CurlAgent.new
end

Instance Method Details

#login(username, password) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/discuz_robot.rb', line 11

def  username, password
  form_url       = @base_url + "logging.php?action=login"
  form_resp      = @agent.get form_url, :save_cookie => true
  form_doc       = Nokogiri::HTML(form_resp)
  formhash_input = form_doc.css("form input[name=formhash]")
  if formhash_input.first
    # Login
    formhash     = formhash_input.first['value']
        = @base_url + "logging.php?action=login&loginsubmit=yes&inajax=0"
    @agent.post , 
      "submit=true&formhash=#{formhash}&loginfield=username&username=#{username}&password=#{password}&questionid=0&answer=&cookietime=2592000", 
      :save_cookie => true
  else
    # Already logged in (this can happen if login is called multiple times in one session)
  end
end

#post(forum_id, subject, message, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/discuz_robot.rb', line 28

def post forum_id, subject, message, options = {}
  form_url  = @base_url + "post.php?action=newthread&fid=#{forum_id}"
  form_resp = @agent.get form_url
  # puts form_resp
  form_doc  = Nokogiri::HTML(form_resp)
  formhash  = form_doc.css("form input[name=formhash]").first['value']
  posttime  = form_doc.css("form input[name=posttime]").first['value']

  post_url  = @base_url + "post.php?action=newthread&fid=#{forum_id}&extra=&topicsubmit=yes"
  @agent.post post_url, "action=newthread&fid=#{forum_id}&formhash=#{formhash}&posttime=#{posttime}&subject=#{subject}&message=#{message}"
end