Created
June 5, 2009 23:28
-
-
Save luisparravicini/124573 to your computer and use it in GitHub Desktop.
Inscripcion automatica al torneo diario de elbruto.es
This file contains 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
#!/usr/bin/ruby | |
# | |
# Inscribe a un jugador de elbruto.es a los torneos. | |
# | |
# El nombre del jugador y la clave se definen en conf.yaml | |
# | |
# escrito por Luis Parravicini ([email protected]) | |
require 'yaml' | |
require 'rubygems' | |
require 'mechanize' | |
CONF = 'conf.yaml' | |
#TODO mostrar un mensaje cuando el sitio da error | |
#TODO no hay chequeo explicito de si la clave es incorrecta, directament da error de que no encontro un elemento de la pagina. | |
$agent = WWW::Mechanize.new | |
$agent.user_agent = 'Windows IE 7' | |
def find(node, search) | |
node.at(search).tap do |elem| | |
raise 'no encontre el elemento' if elem.nil? | |
end | |
end | |
def do_login(uri) | |
page = $agent.get(uri) | |
form = page.forms.first | |
form.pass = $conf[:passw] | |
form.submit | |
end | |
def read_conf | |
unless File.exist?(CONF) | |
raise "no existe el archivo de configuracion (#{CONF})" | |
end | |
File.open('conf.yaml') { |f| YAML::load(f) }.tap do |conf| | |
raise 'falta setear el usuario' if conf[:user].nil? | |
raise 'falta setear la clave' if conf[:passw].nil? | |
end | |
end | |
$conf = read_conf | |
uri = "http://#{$conf[:user]}.elbruto.es/cellule" | |
page = $agent.get(uri) | |
div = find(page, '.caracs') | |
login = div.at('.center/a') | |
unless login.nil? | |
puts "ingresando" | |
page = do_login login['href'] | |
end | |
div = find(page, '.caracs') | |
tournament = find(div, '#tournament') | |
inscribe = tournament.at('div/a') | |
unless inscribe.nil? | |
puts 'registrando al torneo' | |
page = $agent.get inscribe['href'] | |
div = find(page, '.caracs') | |
t = find(div, '#tournament/div').inner_text | |
raise "error inscribiendo (#{t})" unless t =~ /registrada con .+xito/ | |
else | |
puts 'ya inscripto' | |
puts 'hay resultados del torneo' if tournament.at('div').nil? | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment