Skip to content

Instantly share code, notes, and snippets.

@bjeanes
Created June 11, 2012 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjeanes/2908270 to your computer and use it in GitHub Desktop.
Save bjeanes/2908270 to your computer and use it in GitHub Desktop.
How much have I spent on amazon.com?
require 'rubygems'
require 'mechanize'
require 'date'
email = ARGV.shift
password = ARGV.shift || print('Password: ') || gets.chomp
# TODO:
# * record average product price
# * histogram of purchase volume
class Object
def try(method)
send(method) unless nil?
end
end
agent = Mechanize.new { |a| a.user_agent_alias = 'Mac Safari' }
orders_page = agent.get("https://www.amazon.com/gp/css/history/orders/view.html")
orders_page = orders_page.form_with(:id => "ap_signin_form") do |log_in|
log_in.email = email
log_in.password = password
end.submit
orders_page.form_with(:id => 'order-dropdown-form') do |filter|
filter.field_with(:id => 'orderFilter') do |select|
years = select.options.select { |o| o.text =~ /placed in \d{4}/ }.map(&:value).sort
orders = years.map do |year|
select.value = year
page = filter.submit
pages = [page]
pages << page while page = page.link_with(text: /Next /).try(:click)
pages.map { |page| page.search('#cs-orders .action-box') }
end
total = orders.flatten.map do |order|
order.search(".price").text.strip.sub('$', '').to_f
end.reduce(&:+)
puts "Total: #{'$%.02f' % total}"
end
end
# Download report from https://www.amazon.com/gp/b2b/reports and run this script with the CSV as the only argument.
cat $* | cut -d, -f14 | grep -v Sub | xargs | tr ' ' '+' | bc
@anathematic
Copy link

Can has end numbers? :D

@bjeanes
Copy link
Author

bjeanes commented Jun 11, 2012

Hahah... for me it was $11k-ish over the last 18 months. Not as high as I expected, honestly...

@bjeanes
Copy link
Author

bjeanes commented Jun 11, 2012

I added a shell script version that does the same thing but in one line. Much nicer.

@stevie86
Copy link

Hi! Bjeanes how can i find your bash script? @bjeanes thanks!

@bjeanes
Copy link
Author

bjeanes commented May 12, 2022

It's the second file in this gist. But this is over 10 years old so I doubt it still works because Amazon probably changed the format of their CSVs. Not to mention, there are far better CSV processing tools in the command line than what I came up with a decade ago!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment