Skip to content

Instantly share code, notes, and snippets.

Created May 8, 2015 15:03
Show Gist options
  • Save anonymous/4f760b691008b9ceed9e to your computer and use it in GitHub Desktop.
Save anonymous/4f760b691008b9ceed9e to your computer and use it in GitHub Desktop.
#!/bin/env ruby
# encoding: utf-8
require 'open-uri'
require 'nokogiri'
require 'sequel'
require 'mysql2'
### FIX ... puxa-me 10 vezes a mesma oferta, em vez de 10 ofertas diferentes
##URL + Parse
url_wone = "http://wone.pt/descontos"
dat_wone = Nokogiri::HTML(open(url_wone))
#DB connect + Dataset
DB = Sequel.mysql2('patinhas', :user => 'patinhas', :password => 'xxx@2015', :host => 'localhost')
dataset = DB[:ofertas]
wone = dat_wone.css('.item')
wone.each do | dat_wone |
id_ext = wone.at_css('.itemContent a')['onclick'][20..32].to_s.delete('^0-9')
site = "wone.pt"
camp = wone.at_css('.itemText').text.strip
imagem = wone.at_css('.itemImg')['src'][16..-33]
#link1 = "http://www.wone.pt"
#link2 = wone.at_css('a')['href']
link = wone.at_css('a')['onclick'][46..4444][/[^?]+/]
pvr = wone.at_css('.priceBox .price div').to_s.delete('^0-9.').gsub(',','.').to_f
antes = wone.at_css('.priceBox .price .poupanca').text.to_s.delete('^0-9.').gsub(',','.').to_f
pvp = pvr - antes
percent = (100 - pvp / pvr * 100).to_i
portes = 0
datetime= Time.now.strftime("%F %T")
dataset.insert(:id_ext => id_ext, :site => site, :camp => camp, :link => link, :img => imagem, :pvp => pvp, :pvr => pvr, :percent => percent, :portes => portes, :data => datetime)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment