Skip to content

Instantly share code, notes, and snippets.

@beccasaurus
Last active February 21, 2016 21:50
Show Gist options
  • Save beccasaurus/4d161095b954ff945d92 to your computer and use it in GitHub Desktop.
Save beccasaurus/4d161095b954ff945d92 to your computer and use it in GitHub Desktop.
Nordy Orders
source "https://rubygems.org"
gem "sequel"
gem "sqlite3"
gem "poltergeist"
# Scrape all or your Norstrom orders
# require "sinatra" # TODO
require "sequel"
require "capybara/dsl"
require "capybara/poltergeist"
DB = Sequel.sqlite "orders.db"
unless DB.tables.include? :products
DB.create_table :products do
String :title
BigDecimal :price
String :size
String :color
end
end
unless DB.tables.include? :orders
DB.create_table :orders do
primary_key :id
Fixnum :number, index: true
Date :date
end
end
Capybara.default_driver = :poltergeist
Capybara.default_max_wait_time = 10
include Capybara::DSL
username, password = File.read(".username-and-password").split("\n")
begin
visit "https://secure.nordstrom.com/SignIn.aspx"
within "[id*='loginPanel']" do
find("input[type=text]").set username
find("input[type=password]").set password
find("[alt='Sign In']").click
end
has_link? "Order History" # wait for login to finish
click_link "Order History"
has_css? ".ccm-order-history-list tbody tr" # wait ... for order list to load
order_numbers = []
loop do
puts "Processing orders"
all(".ccm-order-history-list tbody tr").each do |order_info|
date = order_info.find(".orderHistoryLeft").text
number = order_info.find(".orderHistoryCenter").text
status = order_info.find(".orderHistoryRight").text
order_numbers.push number
break if order_numbers.length > 4 # don't need any more for testing
puts "#{date}\t#{number}\t#{status}"
end
break if order_numbers.length > 4 # don't need any more for testing
break unless has_link? "Next >"
click_link "Next >"
# Wait for next page of orders
while order_numbers.include? all(".ccm-order-history-list tbody tr .orderHistoryCenter").first.text
puts "Waiting for next page of orders"
sleep 0.1
end
end
puts "Order numbers:"
order_numbers.each do |num|
puts num
end
# just process the first 2 ...
order_numbers[0..1].each do |num|
#if DB[:orders].where(number: num).first
# puts "Already processed order: #{num}"
#else
puts "Processing order: #{num}"
visit "https://secure.nordstrom.com/myaccount/OrderStatus.aspx?ordernum=#{num}"
puts "Number on the page: #{find("[id*=orderNumberLabel]").text}"
all(".ostatIRBox").each do |item|
text = item.find(".ostatItemRowTd3").text
puts "-------"
puts text.inspect
puts "-------"
# title = text.match(/[^\n]+/)[0]
# puts "Title: #{title}"
# price_match = text.match(/Unit Price:\w*\n\w*([^\n]+)/)
# puts price_match
# puts "price: #{price_match[1].strip}" if price_match
### title = item.all("span>span").first.text
### puts "Title: #{title}"
### unit_price = item.find("[id*=unitPrice]").text
### puts "Price: #{unit_price}"
### item.all("[id*=displaySchemaCaption1]").each do |details|
###
### end
#end
end
end
rescue Exception => ex
puts "BOOM!"
save_screenshot "error.png"
puts "saved screenshot to error.png"
raise
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment