Created
April 16, 2010 17:35
-
-
Save knorrium/368706 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
#Para instalar as bibliotecas no Ubuntu, caso alguma falte: | |
# sudo apt-get install ruby1.8-dev | |
# sudo apt-get install libopenssl-ruby | |
# sudo apt-get install libxml2-dev | |
# sudo apt-get install libxslt-dev | |
# sudo gem install mechanize | |
require "rubygems" | |
require "mechanize" | |
require "uri" | |
require "net/http" | |
require "logger" | |
require "rexml/document" | |
def prepare_vote(how, who) | |
begin | |
@vote_action = how | |
$url = "http://colirios.capricho.abril.com.br/batalha.php?idColirio=" + who | |
@mech = Mechanize.new { |a| | |
#a.log = Logger.new("mech.log") | |
a.user_agent_alias = "Linux Firefox" | |
a.read_timeout = 360 | |
} | |
@mech.pre_connect_hooks << lambda { |p| p[:request]['X-Requested-With'] = 'XMLHttpRequest' } | |
@page = @mech.get($url) | |
@options = @page.search("//a[contains(@title, 'Vote')]").map { |link| link['href'] } | |
@left = @options[0].scan(/\d+/)[0] | |
@right = @options[1].scan(/\d+/)[0] | |
if (how == "win") | |
@winner = @left | |
@loser = @right | |
else | |
@winner = @right | |
@loser = @left | |
end | |
#$voteUrl = "http://colirios.capricho.abril.com.br/batalha_controle.php?idVencedor=" + @winner.to_s + "&idPerdedor=" + @loser.to_s | |
vote(@winner, @loser) | |
rescue => e | |
puts "unknown #{e.to_s}" | |
prepare_vote(how, who) | |
rescue Net::HTTPBadResponse => e | |
puts "net exception"+ e.to_s | |
rescue Mechanize::ResponseCodeError => ex | |
puts "mechanize error: "+ex.response_code | |
rescue Timeout::Error => e | |
puts "timeout: " + e.to_s | |
end | |
end | |
def vote(winner, loser) | |
begin | |
puts winner + " vs " + loser | |
puts "WINNER: " + winner | |
puts "LOSER: " + loser | |
$voteUrl = "http://colirios.capricho.abril.com.br/batalha_controle.php?idVencedor=" + winner.to_s + "&idPerdedor=" + loser.to_s | |
puts $voteUrl | |
puts "=============================" | |
@page = @mech.post($voteUrl, {'Referer' => $url}) | |
$xml = REXML::Document.new @page.body | |
puts "Next match: " + $xml.root.elements["colirio/nome"].text + " (" + $xml.root.elements["colirio/id"].text + ")" | |
if (@vote_action == "win") | |
@winner = winner | |
@loser = $xml.root.elements["colirio/id"].text | |
else | |
@winner = $xml.root.elements["colirio/id"].text | |
@loser = loser | |
end | |
vote(@winner, @loser) | |
end | |
end | |
#Lista de IDs | |
#LordEternal - 33125302527857541563 | |
#prepare_vote("win", "33125302527857541563") | |
#F. R. S. - 3885406527813388284 | |
#prepare_vote("lose", "3885406527813388284") | |
#V. B. - 755539852789223063 | |
prepare_vote("lose", "755539852789223063") | |
#T. B. - 42924209 | |
#prepare_vote("lose", "42924209") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment