class CheckOne

Attributes

session[RW]

Public Class Methods

new(username, passwd) click to toggle source
# File lib/getscore/check_one.rb, line 18
def initialize(username, passwd)
  @username = username
  @passwd   = passwd
  @port     = 80
  @session  = nil
  #@user_agent = {
  #      'User-Agent' => "score-bot, just for test"
  #}

  check_internet
  @http = Net::HTTP.new(@host, @port)
end

Public Instance Methods

check_internet() click to toggle source
# File lib/getscore/check_one.rb, line 31
def check_internet
  hosts = ["60.219.165.24", "192.168.11.239"]
  threads = []
  hosts.each do |host|
    threads << Thread.new do
      uri = URI("http://#{host}/loginAction.do")
      response = Net::HTTP.post_form(uri, 'zjh' => @username, 'mm' => @passwd)
      return if "200" != response.code
      threads.each { |thread| Thread.kill(thread) if thread != Thread.current }
      @host = host
      @session = response["set-cookie"]
    end
  end
  threads.each(&:join)
end
different?() click to toggle source
# File lib/getscore/check_one.rb, line 68
def different?
  puts "diff?"
  last = File.exist?("last_digest.tmp") ? File.read("last_digest.tmp") : nil
  now = Digest::SHA2.hexdigest(@body)

  if last == now
    return false
  else
    puts "It's different now!"
    File.open("last_digest.tmp", "w") { |file| file.print now }
    return true
  end
end
get_score() click to toggle source
# File lib/getscore/check_one.rb, line 57
def get_score
  puts "get my score"
  raise "get_session first OR no session" if @session == nil
  request = Net::HTTP::Get.new('/bxqcjcxAction.do')
  request['Cookie'] = @session
  response = @http.request(request)
  @body = response.body.force_encoding('gbk').encode('utf-8').gsub(/\s/,"")

  self
end
get_session() click to toggle source
# File lib/getscore/check_one.rb, line 47
def get_session
  puts "force get session, |this method should not run|"
  request = Net::HTTP::Post.new('/loginAction.do')
  request.set_form_data({ 'zjh' => @username, 'mm' => @passwd })
  response = @http.request(request)

  raise "can't get session"  if !response.code == '200'
  @session = response["set-cookie"]
end
parse_html() click to toggle source
# File lib/getscore/check_one.rb, line 82
def parse_html
  puts "parse my score"
  @body =~ /thead(.*)<\/TABLE/
  arr = $1.scan(/>(.{0,15})<\/td/).map(&:first)
  max_ulen = arr.map(&:ulen).max
  puts "---------------------------------------------------"
  arr.each_slice(7) do |row|
    printf("%-9s %-4s %-s %s %-5s %-4s %-4s\n",row[0],row[1],row[2],(" "*(max_ulen - row[2].ulen)),row[4],row[5],row[6])
  end
end