#!/usr/bin/ruby # Author: Daniel Zhang (caesarz) #(http://www.zhangdi.name) # Date: 2008/9/15 # Version: 0.1 require 'net/http' require 'json' require 'logger' $excepts = [] # The exception list $log = Logger.new(STDOUT) # Log to the STDOUT, could be any file name $log.level = Logger::DEBUG # Log level, could be INFO, WARN, ERROR, etc. def car_parking res = Net::HTTP.new('www.kaixin001.com', 80).start do |http| # Visit the index page get = Net::HTTP::Get.new('/index.php') response = http.request(get) # Find the SERVERID int the set-cookie field in the header sid = response.response['set-cookie'].split('SERVERID=')[1].split(';')[0] $log.debug "The sid: #{sid}" $log.info "Logging in..." # Post the login information to the login.php post = Net::HTTP::Post.new('/login/login.php') post.set_form_data({ 'url' => '/', 'email' => 'your_email', 'password' => 'your_password' }) # The SERVERID is required in the cookie when logging in" post['Cookie'] = "SERVERID=#{sid}" response = http.request(post) # Forge the entire cookie. (After this step, we can act as a logged in user) cookie = "SERVERID=#{sid};#{response.response['set-cookie']}" $log.debug "Cookie: #{cookie}" $log.info "Logged in." $log.info "Visit the car parking app page." # Visit the parking app index page get = Net::HTTP::Get.new('/app/app.php?aid=1040') get['Cookie'] = cookie response = http.request(get) $log.info "Write the page into temp.html" open('temp.html', 'w') { |f| f << response.body } $log.info "Finding the data" # Find the useful data data = find_data $log.debug "The data: #{data.inspect}" response = nil $log.info "Start the car parkings loop..." data[:parking].each do |parking| # if UID is 0, means the parking position is empty. next if parking[:uid] == 0 || $excepts.include?(parking[:uid]) next if parking[:profit] < 6000 # Make the car posted if its profit higher than 6000 $log.info "Sending the marking request for #{parking[:uid]}" post = Net::HTTP::Post.new('/parking/post.php') post.set_form_data({ 'verify' => data[:verify_flag], 'parkid' => parking[:parkid], '_' => '' }) post['Cookie'] = cookie response = http.request(post) end $log.info "Finished." if response == nil # Didn't make any car posted, so we find the highest profit of the cars pl = data[:parking].select { |c| c[:uid] != 0 } pl = pl.collect { |c| c[:profit] } hp = pl.sort.reverse[0] {:highest_profit => hp, :marked => false} else {:marked => true, :response => response} end end end def find_data data, verify_flag = nil, nil open('temp.html').each do |line| data = line if line =~ /var v_userdata/ verify_flag = line if line =~ /var g_verify/ break if data != nil && verify_flag != nil end data = data.split("=")[1].strip.chop verify_flag = verify_flag.split("=")[1].strip.chop.delete("\"") parsed = JSON.parse(data) { :parking => parsed['parking'].map { |c| {:profit => c['car_profit'], :parkid => c['parkid'], :uid => c['car_uid']} }, :verify_flag => verify_flag } end if __FILE__ == $0 while true begin result = car_parking if result[:marked] $log.debug result[:response].body else hp = result[:highest_profit] $log.info "Sleeping...#{6*(6000-hp)}s" sleep(6*(6000 - hp)) end rescue $log.error "Error occured. #{$!}" end end end