Skip to content

Instantly share code, notes, and snippets.

@zakx
Forked from trehn/damazon.py
Last active August 30, 2020 00:01
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save zakx/72738a3da30d0bb09f2e to your computer and use it in GitHub Desktop.
Save zakx/72738a3da30d0bb09f2e to your computer and use it in GitHub Desktop.
#!/usr/bin/python2
# setup: pip install requests beautifulsoup4
from decimal import Decimal
import requests
from bs4 import BeautifulSoup
import sys
# Session setup
session = requests.Session()
session.headers['User-Agent'] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.3 Safari/537.36"
# I've become too lazy to implement the new login, so I removed the login code and crowdsourced the login part to you! please fill this:
session.headers['Cookie'] = """please navigate to https://www.amazon.de/gp/your-account/order-history, log in, solve 2 factor auth, then insert your browser's cookies here"""
# Request order history
hist_r = session.get("https://www.amazon.de/gp/css/order-history/")
soup = BeautifulSoup(hist_r.content)
filters = [options.attrs['value'] for options in soup.find('select', attrs={'name': 'orderFilter'}).findChildren()[1:]]
print "[+] Found %d filters, processing..." % len(filters)
total = Decimal("0.00")
total_orders = 0
for scope in filters:
scope_r = session.get("https://www.amazon.de/gp/css/order-history/?ie=UTF8&orderFilter=%s&startIndex=0" % scope)
scope_soup = BeautifulSoup(scope_r.content)
try:
length = int(scope_soup.find('span', 'num-orders').text.split(" ", 1)[0])
except ValueError:
length = 0
sys.stdout.write("[+] Processing %s \t(%s orders)... " % (scope.rjust(9), str(length).rjust(4)))
sys.stdout.flush()
page = 10
scope_sum = Decimal("0.00")
for x in filter(lambda x: x.text.lstrip().startswith("EUR "), scope_soup.find_all('span', 'a-color-secondary value')):
scope_sum += Decimal(x.text.lstrip()[4:].replace(".","").replace(",","."))
while page <= length:
scope_page_r = session.get("https://www.amazon.de/gp/css/order-history/?ie=UTF8&orderFilter=%s&startIndex=%d" % (scope, page))
for y in filter(lambda x: x.text.lstrip().startswith("EUR "), BeautifulSoup(scope_page_r.content).find_all('span', 'a-color-secondary value')):
scope_sum += Decimal(y.text.lstrip()[4:].replace(".","").replace(",","."))
page += 10
print "\t%s EUR" % (str(scope_sum).rjust(10))
if scope.startswith("year"):
total += scope_sum
total_orders += length
print "[+] Grand total (years only) \t(%s orders)... \t%s EUR" % (str(total_orders).rjust(4), str(total).rjust(10))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment