Skip to content

Instantly share code, notes, and snippets.

@matallo
Created July 5, 2015 22:54
Show Gist options
  • Save matallo/f4395b0c8c6f3882e07a to your computer and use it in GitHub Desktop.
Save matallo/f4395b0c8c6f3882e07a to your computer and use it in GitHub Desktop.
require 'nokogiri'
require 'typhoeus'
BEDCA_URL = "http://www.bedca.net/bdpub/procquery.php"
data_list = "<?xml version=\"1.0\" encoding=\"utf-8\"?><foodquery><type level=\"1\"/><selection><atribute name=\"f_id\"/><atribute name=\"f_ori_name\"/><atribute name=\"langual\"/><atribute name=\"f_eng_name\"/><atribute name=\"f_origen\"/><atribute name=\"edible_portion\"/></selection><condition><cond1><atribute1 name=\"f_origen\"/></cond1><relation type=\"EQUAL\"/><cond3>BEDCA</cond3></condition><order ordtype=\"ASC\"><atribute3 name=\"f_id\"/></order></foodquery>"
req_list = Typhoeus::Request.new(
BEDCA_URL,
method: :post,
headers: {
"Content-Type" => "text/xml"
},
body: data_list
)
req_list.on_complete do |res_list|
if res_list.success?
foodresponse = Nokogiri::XML(res_list.body)
foodresponse.search('food').each do |food|
f_id = food.at("f_id").text
data_food = "<?xml version=\"1.0\" encoding=\"utf-8\"?><foodquery><type level=\"2\"/><selection><atribute name=\"f_id\"/><atribute name=\"f_ori_name\"/><atribute name=\"f_eng_name\"/><atribute name=\"sci_name\"/><atribute name=\"langual\"/><atribute name=\"foodexcode\"/><atribute name=\"mainlevelcode\"/><atribute name=\"codlevel1\"/><atribute name=\"namelevel1\"/><atribute name=\"codsublevel\"/><atribute name=\"codlevel2\"/><atribute name=\"namelevel2\"/><atribute name=\"f_des_esp\"/><atribute name=\"f_des_ing\"/><atribute name=\"photo\"/><atribute name=\"edible_portion\"/><atribute name=\"f_origen\"/><atribute name=\"c_id\"/><atribute name=\"c_ori_name\"/><atribute name=\"c_eng_name\"/><atribute name=\"eur_name\"/><atribute name=\"componentgroup_id\"/><atribute name=\"glos_esp\"/><atribute name=\"glos_ing\"/><atribute name=\"cg_descripcion\"/><atribute name=\"cg_description\"/><atribute name=\"best_location\"/><atribute name=\"v_unit\"/><atribute name=\"moex\"/><atribute name=\"stdv\"/><atribute name=\"min\"/><atribute name=\"max\"/><atribute name=\"v_n\"/><atribute name=\"u_id\"/><atribute name=\"u_descripcion\"/><atribute name=\"u_description\"/><atribute name=\"value_type\"/><atribute name=\"vt_descripcion\"/><atribute name=\"vt_description\"/><atribute name=\"mu_id\"/><atribute name=\"mu_descripcion\"/><atribute name=\"mu_description\"/><atribute name=\"ref_id\"/><atribute name=\"citation\"/><atribute name=\"at_descripcion\"/><atribute name=\"at_description\"/><atribute name=\"pt_descripcion\"/><atribute name=\"pt_description\"/><atribute name=\"method_id\"/><atribute name=\"mt_descripcion\"/><atribute name=\"mt_description\"/><atribute name=\"m_descripcion\"/><atribute name=\"m_description\"/><atribute name=\"m_nom_esp\"/><atribute name=\"m_nom_ing\"/><atribute name=\"mhd_descripcion\"/><atribute name=\"mhd_description\"/></selection><condition><cond1><atribute1 name=\"f_id\"/></cond1><relation type=\"EQUAL\"/><cond3>#{f_id}</cond3></condition><condition><cond1><atribute1 name=\"publico\"/></cond1><relation type=\"EQUAL\"/><cond3>1</cond3></condition><order ordtype=\"ASC\"><atribute3 name=\"componentgroup_id\"/></order></foodquery>"
req_food = Typhoeus::Request.new(
BEDCA_URL,
method: :post,
headers: {
"Content-Type" => "text/xml"
},
body: data_food
)
req_food.on_complete do |res_food|
if res_food.success?
puts "-- Saving #{f_id}"
open("#{f_id}.xml", "w") do |f|
f.puts res_food.body
end
elsif res_food.timed_out?
# aw hell no
puts "got a time out"
elsif res_food.code == 0
puts res_food.return_message
else
puts "HTTP request failed: #{res_food.code.to_s}"
end
end
res_food = req_food.run
end
elsif res_list.timed_out?
# aw hell no
puts "got a time out"
elsif res_list.code == 0
puts res_list.return_message
else
puts "HTTP request failed: #{res_list.code.to_s}"
end
end
res_list = req_list.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment