Skip to content

Instantly share code, notes, and snippets.

@pierot
Created January 23, 2011 14:00
Show Gist options
  • Save pierot/792096 to your computer and use it in GitHub Desktop.
Save pierot/792096 to your computer and use it in GitHub Desktop.
Makro Resto + Fuel scraping
require 'sinatra'
require 'builder'
require 'nokogiri'
require 'uri'
require 'open-uri'
$: << File.join(File.dirname(__FILE__), '..', 'app')
configure do
set :root, File.join(File.dirname(__FILE__), '..')
set :locations, { "deurne" => 0,
"machelen" => 1,
"alleur" => 2,
"eke" => 3,
"sint-pieters-leeuw" => 4}
set :target_fuel, 'http://www.makro.be/Content/assortiment/benzinestation/benzineprijzen/1/index.jsp?stat='
set :targets_resto, { 'nl-BE' => 'http://www.makro.be/Content/assortiment/restaurant/1/index.html',
'fr-BE' => 'http://www.makro.be/Content/assortiment/restaurant/2/index.html'}
end
error do
'Sorry there was a nasty error - ' + request.env['sinatra.error'].name
end
not_found do
'Sorry - Not Found'
end
get '/' do
'<script src="https://gist.github.com/792096.js?file=makro_nokogiri.rb"></script>'
end
get '/resto/:lang' do
redirect '/' unless ['fr-BE', 'nl-BE'].include? params[:lang]
uri = URI.parse(settings.targets_resto[params[:lang]])
html = open(uri)
doc = Nokogiri::HTML(html.read.gsub('<!-- *** div header end *** //-->', ''))
img = doc.css('.boxContent p img').first
builder do |xml|
xml.instruct! :xml, :version => '1.0'
xml.makro do
xml.error(img.nil? ? '1' : '0')
xml.img "#{uri.scheme}://#{uri.host}/#{img['src']}" unless img.nil?
end
end
end
get '/location/:loc' do
redirect '/' if settings.locations[params[:loc]].nil?
doc = Nokogiri::HTML(open("#{settings.target_fuel}#{settings.locations[params[:loc]]}"))
fuels_prices = []
doc.css('div.boxContent table[width="337"] tr:first-child').each do |tr|
fuels_prices << { :officieel => tr.next.css('td')[0].content,
:korting => tr.next.css('td')[1].content,
:makro => tr.next.css('td')[2].content}
end
p fuels_prices
builder do |xml|
xml.instruct! :xml, :version => '1.0'
xml.makro do
xml.error(fuels_prices.empty? ? '1' : '0')
unless fuels_prices.empty?
xml.fuels do
['diesel', 'eurosuper', 'superplus'].each_with_index do |fuel, i|
# xml[fuel] do
xml.tag! fuel do
xml.officieel fuels_prices[i][:officieel]
xml.korting fuels_prices[i][:korting]
xml.makro fuels_prices[i][:makro]
end
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment