From 887ccdbf5560d1459c87fb78ace111d87c7893ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Mon, 21 Apr 2014 15:03:44 -0700 Subject: [PATCH] make it ruby1.9 compatible, allow redirection, use common mouser site --- hardware/Rakefile | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/hardware/Rakefile b/hardware/Rakefile index 1179c6b..8f59770 100644 --- a/hardware/Rakefile +++ b/hardware/Rakefile @@ -6,6 +6,7 @@ Rakefile to manage gEDA hardware projects require 'rake/clean' require 'csv' # to export BOM and costs require 'open-uri' # to parse URLs +require 'open_uri_redirections' # allow redirections require 'nokogiri' # to scrape sites require 'net/http' # to ask octopart require 'json' # to parse octopart reponses @@ -242,7 +243,7 @@ def scrape_farnell(sku) # get stock stock_doc = doc.xpath('//td[@class="prodDetailAvailability"]')[0] if stock_doc then - to_return[:stock] = stock_doc.text.lines[-1].to_i + to_return[:stock] = stock_doc.text.lines.to_a[-1].to_i else # when several stocks are available stock_doc = doc.xpath('//div[@class="stockDetail"]')[0] to_return[:stock] = stock_doc.text.gsub(".","").scan(/\d+/)[-1].to_i # the last match should be for EU @@ -262,8 +263,8 @@ end def scrape_mouser(sku) to_return = {stock: nil, currency: "EUR", prices: []} # get page - url = "http://de.mouser.com/Search/ProductDetail.aspx?R=0virtualkey0virtualkey#{sku}" - doc = Nokogiri::HTML(open(URI.escape(url))) + url = "http://www.mouser.com/Search/ProductDetail.aspx?R=0virtualkey0virtualkey#{sku}" + doc = Nokogiri::HTML(open(URI.escape(url),:allow_redirections => :all)) # get stock stock_doc = doc.xpath('//table[contains(@id,"availability")]/tr/td')[0] to_return[:stock] = stock_doc.text.gsub(".","").to_i