Created
October 12, 2012 13:17
-
-
Save yyyyyyuanfei/3879154 to your computer and use it in GitHub Desktop.
虾米自动签到
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'mechanize' | |
| require "logger" | |
| require 'json' | |
| def login_url | |
| "http://www.xiami.com/member/login" | |
| end | |
| def checkin_url | |
| "http://www.xiami.com/task/signin" | |
| end | |
| def server | |
| "www.xiami.com" | |
| end | |
| def is_osx? | |
| !!(RUBY_PLATFORM =~ /darwin/) | |
| end | |
| def account_filename | |
| "account.yml" | |
| end | |
| def gen_account_from_keychain account | |
| password = %x[security find-internet-password -a #{account} -ws #{server}].chomp | |
| File.open(account_filename, "w") do |file| | |
| file.puts({email: account, password: password}.to_json) | |
| end | |
| end | |
| if is_osx? | |
| gen_account_from_keychain "[email protected]" | |
| end | |
| agent = Mechanize.new | |
| # agent.log = Logger.new $stderr | |
| # agent.agent.http.debug_output = $stderr | |
| login_page = agent.get login_url | |
| login_form = login_page.forms.first | |
| begin | |
| File.open(account_filename, "r") do |file| | |
| account = JSON.parse file.read | |
| login_form.email = account['email'] | |
| login_form.password = account['password'] | |
| end | |
| login_form.submit login_form.buttons.first | |
| agent.post checkin_url | |
| rescue | |
| ensure | |
| File.delete(account_filename) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment