- #!/usr/bin/ruby
- # Licença: GNU GPLv2
- # Copyright: Renato Silva
- require 'rubygems'
- require 'hpricot'
- require 'open-uri'
- require 'iconv'
- UTF8Option = "--utf8"
- ProxyOption = "--proxy"
- URL = "http://www.trf2.jus.br/transparencia/concursos/Concursos/Servidores/Convoca%C3%A7%C3%A3o%20de%20Candidatos/2007/candidatos_rio_2007.aspx"
- PROXY_REGEX = /\A#{ProxyOption}=([^:]+:\d+)\Z/
- ProxyArg = ARGV.find { |a| a =~ PROXY_REGEX }
- Proxy = ProxyArg && "http://" << ProxyArg[PROXY_REGEX, 1]
- Analista = "tr[13]/td[%d]"
- Tecnico = "tr[20]/td[%d]"
- Arquivo = File.dirname($0) << '/trf.txt'
- def say(arg)
- msg = (arg.is_a? Exception)? "Erro#{" de conexão" if arg.is_a? SocketError}: " : arg.to_s
- msg = Iconv.conv('cp850', 'utf-8', msg) unless ARGV.include? UTF8Option
- msg << arg.message if arg.is_a? Exception
- puts msg
- end
- def finish(cause, delay=nil)
- say cause
- sleep delay rescue begin
- print "\nPressione ENTER para fechar..."
- STDIN.gets
- end
- exit
- end
- def pegar(*itens)
- resultado = ""
- itens.each do |item|
- prefixo = case item
- when Analista: "Analista de"
- when Tecnico: "Técnico de"
- else "Algum cargo de"
- end
- begin
- doc = Hpricot(open(URL, :proxy => Proxy))
- qtde = doc.search(item % 3).first.inner_html
- area = doc.search(item % 1).first.inner_html.sub(/ - .*/, '')
- pnes = doc.search(item % 2).first.inner_html.gsub(/-/, '0').gsub(/\n+/, '').gsub(/\s+/, ' ')
- rescue Exception => e
- finish e
- end
- resultado << "#{prefixo} #{area}: #{qtde} nomeado, deficientes: #{pnes}\n"
- end
- resultado.chop!
- end
- if ARGV.any? and not ARGV.all? { |a| a == UTF8Option or a =~ PROXY_REGEX }
- finish "
- Notificador do concurso do TRF, v2010.10.19\n
- Modo de usar: #{File.basename($0)} [#{ProxyOption}=host:porta] [#{UTF8Option}]
- ", 0
- end
- say "Checando convocações no TRF...\n\n"
- say "Usando proxy #{Proxy}...\n\n" if Proxy
- Atual = pegar(Analista, Tecnico)
- Anterior = IO.read(Arquivo) rescue "Não disponível"
- finish "Sem novas convocações.", 3 if Atual == Anterior
- File.new(Arquivo, 'w').write(Atual)
- finish "ATENÇÃO: aparentemente houveram mais convocações no TRF:\n
- Verificação anterior:\n#{Anterior}\n
- Verificação atual:\n#{Atual}"