Created
May 7, 2013 23:09
-
-
Save rafapolo/5536918 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
| #encoding: utf-8 | |
| namespace :minc do | |
| desc "Crawleia Minc" | |
| task :grab => :environment do | |
| puts | |
| puts "="*70 | |
| puts "\t\t\t- Crawleando MinC -" | |
| puts "\t\t\t"+Time.new.strftime("%Y-%m-%d %H:%M:%S") | |
| puts "="*70 | |
| # do primeiro ao último | |
| 80000.downto(1) do |id| | |
| #(49414..48160).each do |id| | |
| get_projeto(to_count_str(id)) | |
| end | |
| puts | |
| puts "="*70 | |
| puts "\t\t\t"+Time.new.strftime("%Y-%m-%d %H:%M:%S") | |
| puts "="*70 | |
| end | |
| #ajusta encoding | |
| def self.utf8(text) | |
| Iconv.conv("UTF8", "LATIN1", text) if text | |
| end | |
| # 4 => 000004 | |
| def to_count_str(num) | |
| "0"*(6-(num.to_s.size))+num.to_s | |
| end | |
| # "20.000,00" => 20,000.00 | |
| def self.to_float(string) | |
| string.gsub(".", "").gsub(",", ".").to_f | |
| end | |
| # converto texto em data | |
| def self.to_date(string) | |
| return !string || string.to_s.empty? ? nil : Date.strptime(string, '%d/%m/%Y') | |
| end | |
| # limpar cnpj e cpf | |
| def self.clean(cpfcnpj) | |
| cpfcnpj.gsub(/[-.\/]/, "") | |
| end | |
| # se texto é vazio deve ir pro banco como nulo | |
| def self.empty(string) | |
| return string == nil || string == " " ? nil : string | |
| end | |
| # expressões regulares no HTML - sacada de mestre. | |
| def self.get_field(field) | |
| data = @page.scan(/>#{field}<br \/><\/FONT>.*?serif">(.*?)<\/FONT>/m) | |
| d = data[0] && data[0][0] ? data[0][0].to_s[0..65536] : nil | |
| puts d | |
| utf8 d | |
| end | |
| def self.get_from_link(field) | |
| data = @page.scan(/>#{field}<br \/><\/FONT>.*?#007ba3">(.*?)<\/font>/m) | |
| return data[0] && data[0][0] ? data[0][0].to_s : nil | |
| end | |
| def self.get_value(field) | |
| @page.scan(/>#{field} R\$<br \/><\/FONT>.*?serif">(.*?)<\/FONT>/m)[0][0] | |
| end | |
| def self.get_liberado_at | |
| data = @page.scan(/movimentar conta bancária em (.*?)<\/FONT>/) | |
| return data[0] && data[0][0] ? to_date(data[0][0].to_s) : nil | |
| end | |
| def self.get_sintese | |
| data = @page.scan(/Síntese do Projeto<\/FONT>.*?serif">(.*?)<\/FONT>/m) | |
| return data[0] && data[0][0] ? data[0][0].to_s : nil | |
| end | |
| # pego página com POST | |
| def self.get_page(path, post_data) | |
| url = 'sistemas.cultura.gov.br' | |
| http = Net::HTTP.new(url, 80) | |
| resp = http.get(path) | |
| headers = { | |
| 'Cookie' => resp.response['set-cookie'], | |
| 'Referer' => url+path, | |
| 'Content-Type' => 'application/x-www-form-urlencoded' | |
| } | |
| resp = http.post(path, post_data, headers) | |
| if resp.code.to_i==200 | |
| page = utf8 resp.body | |
| else | |
| puts "Error getting page: #{resp.code}" | |
| end | |
| page | |
| end | |
| def self.get_projeto(id) | |
| projeto = Projeto.find_by_numero(id) | |
| if (!projeto) | |
| puts "==> pegando projeto #{id}..." | |
| @page = get_page("/salicnet/conDadosBasicosProjeto/conDadosBasicosProjeto.php", "nrprojeto=#{id}") | |
| if @page =~ /Não há registros a exibir/ | |
| puts "Nada" | |
| return | |
| end | |
| # identificação | |
| #num = get_field("Nº Projeto") | |
| nome = get_field("Nome do Projeto") | |
| cnpjcpf = get_from_link("CNPJ / CPF") | |
| #nome_proponente = get_from_link("Proponente") | |
| # info | |
| uf = get_field("UF do Projeto") | |
| area = get_field("Área Cultural") | |
| segmento = get_field("Segmento") | |
| processo = get_field("Processo") | |
| mecanismo = get_field("Mecanismo") | |
| enquadramento = empty(get_field("Enquadramento")) | |
| # situação | |
| situacao_at = to_date(empty(get_field("Dt.Situação"))) | |
| situacao = get_field("Situação") | |
| providencia = get_field("Providência Tomada") | |
| sintese = get_sintese | |
| # valores | |
| solicitado = to_float(get_field('Solicitado R\$')) | |
| aprovado = to_float(get_from_link('Aprovado R\$')) | |
| apoiado = to_float(get_from_link('Apoiado R\$')) | |
| liberado_at = get_liberado_at | |
| # pega proponente | |
| proponente = get_proponente(clean(cnpjcpf)) | |
| projeto = Projeto.create(:proponente_id=>proponente.id, :nome=>nome, :numero=>num, :uf=>uf, :area=>area, :mecanismo=>mecanismo, :enquadramento=>enquadramento, | |
| :segmento=>segmento, :processo=>processo, :situacao_at=>situacao_at, :situacao=>situacao, :providencia=>providencia, :sintese=>sintese, | |
| :solicitado=>solicitado, :aprovado=>aprovado, :apoiado=>apoiado, :liberado_at=>liberado_at) | |
| puts "Projeto: #{projeto.nome}" | |
| if (apoiado>0) | |
| get_incentivadores(id) | |
| end | |
| end | |
| projeto | |
| end | |
| def self.get_proponente(cnpjcpf) | |
| proponente = Proponente.find_by_cnpjcpf(cnpjcpf) | |
| if (!proponente) | |
| puts "==> pegando proponente #{cnpjcpf}..." | |
| @page = get_page("/salicnet/conDadosCadastraisProponente/conDadosCadastraisProponente.php", "nmgp_parms=nmgp_lig_edit_lapis?#?S?@?cgccpf?#?#{cnpjcpf}?@?") | |
| nome = get_field("Nome") | |
| responsavel = empty(get_field("Responsável")) | |
| logradouro = get_field("Logradouro") | |
| uf = get_field("UF do Proponente") | |
| cidade = get_field("Cidade") | |
| cep = get_field("CEP") | |
| email = get_from_link("Email") | |
| tel_res = empty(get_field("Residencial")) | |
| tel_com = empty(get_field("Comercial")) | |
| tel_cel = empty(get_field("Celular")) | |
| tel_fax = empty(get_field("Fax")) | |
| proponente = Proponente.create(:cnpjcpf=>cnpjcpf, :nome=>nome, :responsavel=>responsavel, :logradouro=>logradouro, :uf=>uf, :cidade=>cidade, | |
| :cep=>cep, :email=>email, :tel_res=>tel_res, :tel_com=>tel_com, :tel_fax=>tel_fax, :tel_cel=>tel_cel) | |
| puts "Proponente: #{proponente.nome}" | |
| end | |
| proponente | |
| end | |
| def self.get_incentivadores(id_projeto) | |
| puts "==> pegando incentivadores #{id_projeto}" | |
| @page = get_page("/salicnet/conProjetoESeusIncentivadores/conProjetoESeusIncentivadores.php", "nmgp_parms=nmgp_lig_edit_lapis?#?S?@?g_nrprojeto?#?#{id_projeto}?@?") | |
| incentivadores = @page.scan(/007ba3">(.*?)<\/font>.*?serif">(.*?)<\/FONT>.*?false;">(.*?)<\/a>/m) | |
| incentivadores.each do |info| | |
| proponente = get_proponente(clean(info[0])) | |
| Incentivo.create(:proponente_id=>proponente.id, :projeto_id=>id_projeto, :valor=>to_float(info[2])) | |
| puts "#{info[1]} => #{info[2]}" | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment