Hacking 开心网(虚拟朋友) with Mechanize

因为本人作息时间严重不规律, 导致开心网(kaixin001)里养的宠物mm快饿死了。。。  于是想写一个外挂,来帮mm自动按时喂食, 本想把此脚本偷偷放服务器的crontab里:) ,用ruby写很简单,完成的代码如下:

配置文件: pp.yml
logins:
    url: [url]http://www.kaixin001.com/login/login.php[/url]
    email: [email protected]
    password: xxxxxx

trueman:
    url: [url]http://www.kaixin001.com/app/app.php?aid=1068[/url]

主文件: kaixin_trueman.rb
require 'rubygems'
require 'mechanize'
require 'yaml'

class KaixinTrueMan < WWW::Mechanize

    if not File.exists?('pp.yaml')
        puts "Please setup the pp.yaml for configuration first"
        exit
    end

    conf        = YAML::load_file('pp.yaml')
    logins    = conf["logins"]
    trueman = conf["trueman"]

    KAIXIN_EMAIL = logins["email"]
    KAIXIN_PWD     = logins["password"]
    LOGIN_URL        = logins["url"]
    TRUEMAN_URL    = trueman["url"]

    attr_accessor :logged_in

    def initialize
        super
    end

    def spider_to(url=TRUEMAN_URL)
        login or return false unless self.logged_in
        sleep(3)
        page = post(url)
        if page.uri.to_s ==  TRUEMAN_URL
            return page
        end
    end

    def feeding
        page = spider_to
        sleep(3)
        # get flash
        page.search("//div[@id='mainplayer']")
    
        #    TODO
        #    ... ...
    end

    private
    def login(email = KAIXIN_EMAIL,password = KAIXIN_PWD)
        @logged_in = begin
            page = post(LOGIN_URL,{"email" => email,"password" => password})
            if page.uri.to_s == 'http://www.kaixin001.com/home/'
                return true
            else
                history.clear
                return false
            end
        end
    end
end


:(   现在可以让这个spider爬到mm身边了, 可是想要下手帮mm喂食,却困难了。。。因为碰到个flash文件 。。。  研究了两天没有结果。。。暂时告一段落吧。




你可能感兴趣的:(Ruby,mechanize,开心网,kaixin001,虚拟朋友)