rogerleite-anvisa-bot 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile ADDED
@@ -0,0 +1,64 @@
1
+ h1. anvisa-bot
2
+
3
+ anvisa-bot é uma API Ruby para consultar Produtos para Saúde Registrados.
4
+
5
+ Até o momento, somente a busca por registro anvisa foi implementada.
6
+ Esta API faz requests e parse no HTML do site da anvisa para fornecer informações sobre produtos.
7
+
8
+ Site da Anvisa:
9
+
10
+ http://www.anvisa.gov.br/scriptsweb/correlato/correlato.htm
11
+
12
+ h2. Instalação
13
+
14
+ @sudo gem install rogerleite-anvisa-bot -s http://gems.github.com@
15
+
16
+ h2. Usando como executável
17
+
18
+ $ anvisa-bot "numero_registro_anvisa" <br>
19
+ Por exemplo:
20
+ @anvisa-bot 10008530225@
21
+
22
+ h2. Usando a API Ruby
23
+
24
+ <pre>
25
+ require "rubygems"
26
+ require "anvisa_bot"
27
+
28
+ produto = AnvisaBot.consulta_produto_por_registro(10247530027)
29
+
30
+ puts produto.empresa
31
+ puts produto.cnpj
32
+ puts produto.autorizacao
33
+ puts produto.nome
34
+ puts produto.modelo
35
+ puts produto.registro
36
+ puts produto.processo
37
+ puts produto.origem
38
+ puts produto.vencimento_registro
39
+ </pre>
40
+
41
+ h2. LICENSE:
42
+
43
+ (The MIT License)
44
+
45
+ Copyright (c) 2008 FIX
46
+
47
+ Permission is hereby granted, free of charge, to any person obtaining
48
+ a copy of this software and associated documentation files (the
49
+ 'Software'), to deal in the Software without restriction, including
50
+ without limitation the rights to use, copy, modify, merge, publish,
51
+ distribute, sublicense, and/or sell copies of the Software, and to
52
+ permit persons to whom the Software is furnished to do so, subject to
53
+ the following conditions:
54
+
55
+ The above copyright notice and this permission notice shall be
56
+ included in all copies or substantial portions of the Software.
57
+
58
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
59
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
60
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
61
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
62
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
63
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
64
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rubygems/specification'
3
+ require 'rake'
4
+ require 'rake/gempackagetask'
5
+ require 'spec/rake/spectask'
6
+
7
+ GEM = "anvisa-bot"
8
+ GEM_VERSION = "0.2.1"
9
+ SUMMARY = "Pesquisa de produtos para saúde registrados"
10
+ AUTHOR = "Roger Leite"
11
+ EMAIL = "roger.barreto@gmail.com"
12
+ HOMEPAGE = "http://github.com/rogerleite/anvisa-bot"
13
+
14
+
15
+ spec = Gem::Specification.new do |s|
16
+ s.name = GEM
17
+ s.version = GEM_VERSION
18
+ s.platform = Gem::Platform::RUBY
19
+ s.summary = SUMMARY
20
+ s.require_paths = ['bin', 'lib']
21
+ s.files = FileList['bin/*.rb', 'lib/**/*.rb', '[A-Z]*', '[a-z]*'].to_a
22
+ s.executables = ["anvisa-bot"]
23
+
24
+ s.author = AUTHOR
25
+ s.email = EMAIL
26
+ s.homepage = HOMEPAGE
27
+
28
+ s.rubyforge_project = GEM # GitHub bug, gem isn't being build when this miss
29
+
30
+ s.add_dependency(%q<hpricot>, [">= 0.8.1"])
31
+ end
32
+
33
+ Spec::Rake::SpecTask.new do |t|
34
+ t.spec_files = FileList['spec/**/*_spec.rb']
35
+ t.spec_opts = %w(-fs --color)
36
+ end
37
+
38
+ Rake::GemPackageTask.new(spec) do |pkg|
39
+ pkg.gem_spec = spec
40
+ end
41
+
42
+ desc "Install the gem locally"
43
+ task :install => [:package] do
44
+ sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
45
+ end
46
+
47
+ desc "Create a gemspec file"
48
+ task :make_spec do
49
+ File.open("#{GEM}.gemspec", "w") do |file|
50
+ file.puts spec.to_ruby
51
+ end
52
+ end
53
+
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{anvisa-bot}
5
+ s.version = "0.2.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Roger Leite"]
9
+ s.date = %q{2009-08-10}
10
+ s.default_executable = %q{anvisa-bot}
11
+ s.email = %q{roger.barreto@gmail.com}
12
+ s.executables = ["anvisa-bot"]
13
+ s.files = ["lib/anvisa_bot.rb", "lib/anvisa_browser.rb", "lib/anvisa_parser.rb", "README.textile", "Rakefile", "spec", "fluxo_site_anvisa.txt", "anvisa-bot.gemspec", "bin", "lib", "bin/anvisa-bot"]
14
+ s.homepage = %q{http://github.com/rogerleite/anvisa-bot}
15
+ s.require_paths = ["bin", "lib"]
16
+ s.rubyforge_project = %q{anvisa-bot}
17
+ s.rubygems_version = %q{1.3.1}
18
+ s.summary = %q{Pesquisa de produtos para saúde registrados}
19
+
20
+ if s.respond_to? :specification_version then
21
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
22
+ s.specification_version = 2
23
+
24
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
25
+ s.add_runtime_dependency(%q<hpricot>, [">= 0.8.1"])
26
+ else
27
+ s.add_dependency(%q<hpricot>, [">= 0.8.1"])
28
+ end
29
+ else
30
+ s.add_dependency(%q<hpricot>, [">= 0.8.1"])
31
+ end
32
+ end
data/bin/anvisa-bot ADDED
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Exemplo de comando:
4
+ # ruby bin/anvisa-bot.rb 10008530225
5
+ # exemplos de registros anvisa:
6
+ # - 10008530225
7
+ # - 10247530027
8
+ # - 80175510005
9
+
10
+ $LOAD_PATH << File.expand_path(File.dirname(__FILE__) + "/../lib")
11
+
12
+ require "anvisa_bot"
13
+
14
+ args = ARGV.dup
15
+ ARGV.clear
16
+
17
+ puts "usage: anvisa-bot <registro_anvisa>"
18
+
19
+ numero_registro = args[0]
20
+ produto = AnvisaBot.consulta_produto_por_registro(numero_registro)
21
+
22
+ puts produto.to_yaml
23
+
24
+ =begin
25
+ puts <<HTML
26
+ <html>
27
+ <body>
28
+ <div>
29
+ <p>empresa: #{produto.empresa}</p><br>
30
+ <p>modelo: #{produto.modelo}</p>
31
+ </div>
32
+ </body>
33
+ </html>
34
+ HTML
35
+ =end
@@ -0,0 +1,38 @@
1
+ ===Fluxo para pesquisa por num. de registro no site da Anvisa===
2
+
3
+ POST rconsulta_produto_internet.asp
4
+
5
+ CO_TIPO_PRODUTO=8
6
+ Area=Cosmético
7
+ Processo=
8
+ Produto=
9
+ Registro=
10
+ CNPJ=
11
+ NO_EMPRESA=
12
+
13
+ -recebo o html de produtos.
14
+ -extrair os links e executar:
15
+
16
+ POST rconsulta_produto_detalhe.asp
17
+
18
+ CO_PRODUTO=var_codigo
19
+ NO_EMPRESA=var_empresa
20
+ NU_CNPJ=var_cnpj
21
+ REGISTRO=var_registro
22
+
23
+ NU_PROCESSO=
24
+ NO_PRODUTO=
25
+ NU_REGISTRO=
26
+ NU_AUTORIZACAO=
27
+
28
+ CO_TIPO_PRODUTO=8
29
+ hdnpgAtual=1
30
+ hdnmodo=
31
+ PROCESSO=
32
+ PRODUTO=
33
+ EMPRESA=
34
+ CNPJ=
35
+
36
+ -recebe o html de detalhe do produto
37
+ -extrair as informacoes e retornar
38
+
data/lib/anvisa_bot.rb ADDED
@@ -0,0 +1,20 @@
1
+ require "anvisa_browser"
2
+ require "anvisa_parser"
3
+
4
+ class AnvisaBot
5
+
6
+ #veja o fluxo de "navegacao" do bot no arquivo fluxo_site_anvisa.txt
7
+ def self.consulta_produto_por_registro(numero_registro)
8
+ anvisa_browser = AnvisaBrowser.new
9
+ plain_html = anvisa_browser.consulta_produto_por_registro(numero_registro)
10
+
11
+ #puts "ZZZ ... ~10 segs"
12
+ sleep( rand(10) ) #para não chamar atencao do server
13
+
14
+ links = AnvisaParser.find_links_produtos(plain_html)
15
+ plain_html = anvisa_browser.consulta_produto_detalhe(numero_registro, links.first)
16
+ AnvisaParser.extract_produto(plain_html)
17
+ end
18
+
19
+ end
20
+
@@ -0,0 +1,101 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+
4
+ class AnvisaBrowser
5
+
6
+ ANVISA_URL = 'http://www7.anvisa.gov.br/datavisa/Consulta_Produto_correlato/'
7
+ USER_AGENT = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.13) Gecko/2009080315 Ubuntu/9.04 (jaunty) Firefox/3.0.13"
8
+ DEBUG_REQ = false
9
+
10
+ class << self
11
+ attr_accessor :default_headers
12
+ end
13
+ self.default_headers = {
14
+ 'User-Agent' => USER_AGENT,
15
+ 'Accept' => 'text/html',
16
+ 'Accept-Charset' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'
17
+ }
18
+
19
+ def consulta_produto_por_registro(numero_registro)
20
+ plain_url = "#{ANVISA_URL}rconsulta_produto_internet.asp"
21
+ params = {
22
+ :CO_TIPO_PRODUTO => '8', :Area => 'Cosmético', :Processo => '',
23
+ :Produto => '', :CNPJ => '', :NO_EMPRESA => '',
24
+ :Registro => numero_registro
25
+ }
26
+
27
+ user_headers = {
28
+ 'Referer' => 'http://www7.anvisa.gov.br/datavisa/Consulta_Produto_correlato/consulta_correlato.asp'
29
+ }
30
+
31
+ simple_post(plain_url, params, user_headers)
32
+ end
33
+
34
+ def consulta_produto_detalhe(numero_registro, link_produto)
35
+ plain_url = "#{ANVISA_URL}rconsulta_produto_detalhe.asp"
36
+ params = {
37
+ :CO_PRODUTO => link_produto.codigo,
38
+ :NO_EMPRESA => link_produto.empresa,
39
+ :NU_CNPJ => link_produto.cnpj,
40
+ :REGISTRO => numero_registro,
41
+ :NU_PROCESSO => '',
42
+ :NO_PRODUTO => '',
43
+ :NU_REGISTRO => '',
44
+ :NU_AUTORIZACAO => '',
45
+ :CO_TIPO_PRODUTO => '8',
46
+ :hdnpgAtual => '1',
47
+ :hdnmodo => '',
48
+ :PROCESSO => '',
49
+ :PRODUTO => '',
50
+ :EMPRESA => '',
51
+ :CNPJ => ''
52
+ }
53
+
54
+ user_headers = {
55
+ 'Referer' => 'http://www7.anvisa.gov.br/datavisa/Consulta_Produto_correlato/consulta_correlato.asp'
56
+ }
57
+
58
+ simple_post(plain_url, params, user_headers)
59
+ end
60
+
61
+ private
62
+ # Faz um post simples e retorna o "plain html" da pagina.
63
+ # fonte: http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html
64
+ def simple_post(plain_url, params, user_headers)
65
+ url = URI.parse(plain_url)
66
+
67
+ headers = self.class.default_headers.merge(user_headers)
68
+
69
+ req = Net::HTTP::Post.new(url.path, headers)
70
+ req.set_form_data(params)
71
+
72
+ if DEBUG_REQ
73
+ puts "=== Debug Http Request"
74
+ puts "url => #{url}"
75
+ puts "body => #{req.body}"
76
+ puts "=== Fim Debug Http Request"
77
+ end
78
+
79
+ res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
80
+ case res
81
+ when Net::HTTPSuccess, Net::HTTPRedirection
82
+ # OK
83
+ else
84
+ res.error!
85
+ end
86
+
87
+ plain_html = res.body
88
+ if DEBUG_REQ
89
+ puts "=== Debug Http Response Body"
90
+ puts plain_html
91
+ puts "=== Fim Debug Http Response Body"
92
+ end
93
+ plain_html
94
+ end
95
+
96
+ def make_headers(user_headers)
97
+ self.class.default_headers.merge(user_headers)
98
+ end
99
+
100
+ end
101
+
@@ -0,0 +1,80 @@
1
+ require "rubygems"
2
+ require "hpricot"
3
+
4
+ class AnvisaParser
5
+
6
+ DEBUG = false
7
+
8
+ def self.find_links_produtos(plain_html)
9
+ html = Hpricot(plain_html)
10
+ links = []
11
+ html.search('a[@href*="ver_detalhes"]').each do |link|
12
+ #link['href'] returns:
13
+ #javascript:ver_detalhes('302629','LABORAT%C3%93RIOS%20B.%20BRAUN%20S/A','31673254000102');
14
+ params = link['href'].scan(/'([^']*)'/) #ER, pega os valores que estão dentro das ''
15
+ links << LinkProduto.new(params[0].to_s, params[1].to_s, params[2].to_s)
16
+ end
17
+ links
18
+ end
19
+
20
+ def self.extract_produto(plain_html)
21
+ html = Hpricot(plain_html)
22
+
23
+ table = html.at("table.formulario")
24
+ #devido a html errado da anvisa (ln 106 e 116), subo dois niveis e ignoro o primeiro td
25
+ table = table.parent.parent.parent
26
+
27
+ values = []
28
+ ignore_first = true
29
+ table.search("td").each do |td|
30
+ if ignore_first
31
+ ignore_first = false
32
+ next
33
+ end
34
+ if DEBUG
35
+ puts '=== td.inner_html ==='
36
+ puts td.inner_html
37
+ end
38
+ value = td.inner_html
39
+ value.gsub!(/&nbsp;/, ' ') #substitui o espaco do html por espaco
40
+ value.strip! #remove espacos adicionais
41
+ value.gsub!(/(<br>|<br\s\/>)/, "\n")
42
+ value.gsub!(/<[^>]*>/, '') #remove qualquer tag restante
43
+ values << value
44
+ end
45
+
46
+
47
+ Produto.new(:empresa => values[0], :cnpj => values[1], :autorizacao => values[2],
48
+ :nome => values[3], :modelo => values[4], :registro => values[5],
49
+ :processo => values[6], :origem => values[7], :vencimento_registro => values[8])
50
+ end
51
+
52
+ end
53
+
54
+ class LinkProduto
55
+
56
+ attr_accessor :codigo, :empresa, :cnpj
57
+
58
+ def initialize(codigo, empresa, cnpj)
59
+ @codigo = codigo
60
+ @empresa = empresa
61
+ @cnpj = cnpj
62
+ end
63
+
64
+ end
65
+
66
+ class Produto
67
+
68
+ attr_accessor :empresa, :cnpj, :autorizacao,
69
+ :nome, :modelo, :registro,
70
+ :processo,:origem, :vencimento_registro
71
+
72
+ def initialize(args)
73
+ raise ArgumentError("Args have to be a Hash with attributes") unless args.is_a?(Hash)
74
+ args.each do |att, value|
75
+ self.send("#{att}=", value)
76
+ end
77
+ end
78
+
79
+ end
80
+
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rogerleite-anvisa-bot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Roger Leite
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-10 00:00:00 -07:00
13
+ default_executable: anvisa-bot
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hpricot
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.8.1
24
+ version:
25
+ description:
26
+ email: roger.barreto@gmail.com
27
+ executables:
28
+ - anvisa-bot
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - lib/anvisa_bot.rb
35
+ - lib/anvisa_browser.rb
36
+ - lib/anvisa_parser.rb
37
+ - README.textile
38
+ - Rakefile
39
+ - spec
40
+ - fluxo_site_anvisa.txt
41
+ - anvisa-bot.gemspec
42
+ - bin
43
+ - lib
44
+ - bin/anvisa-bot
45
+ has_rdoc: false
46
+ homepage: http://github.com/rogerleite/anvisa-bot
47
+ licenses:
48
+ post_install_message:
49
+ rdoc_options: []
50
+
51
+ require_paths:
52
+ - bin
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project: anvisa-bot
69
+ rubygems_version: 1.3.5
70
+ signing_key:
71
+ specification_version: 2
72
+ summary: "Pesquisa de produtos para sa\xC3\xBAde registrados"
73
+ test_files: []
74
+