Skip to content

Instantly share code, notes, and snippets.

@meet30997
Last active March 2, 2022 08:10
Show Gist options
  • Save meet30997/6fe33ddcdb91edf0a6165205ccdff50c to your computer and use it in GitHub Desktop.
Save meet30997/6fe33ddcdb91edf0a6165205ccdff50c to your computer and use it in GitHub Desktop.
Amazon | Scrap the total amount you’ve ever spent on Amazon
let yearToStartTheSearchFrom = 2015;
let yearToEndTheSearch = 2022;
let startingIndex = 0;
let totalSpend = 0;
let globalSpend = 0;
for (let currentYear = yearToStartTheSearchFrom; currentYear <= yearToEndTheSearch; currentYear++) {
let isFetchCompleted = false;
while (!isFetchCompleted) {
const req = await fetch('https://www.amazon.in/gp/your-account/order-history/ref=ppx_yo_dt_b_pagination_3_1?ie=UTF8&orderFilter=year-' + currentYear + '&search=&startIndex=' + startingIndex, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
});
const res = await req.text();
const parser = new DOMParser();
const doc = parser.parseFromString(res, 'text/html');
const data = doc.querySelectorAll(".order-info");
if (data.length == 0) {
isFetchCompleted = true;
startingIndex = 0;
console.log("Fetch Completed For Year => " + currentYear);
console.log("Total Spend For Year " + currentYear + " is => " + totalSpend);
globalSpend += totalSpend;
totalSpend = 0;
} else {
for (let child of data) {
const spanContainerPrice = child.firstChild.childNodes[1].firstChild.childNodes[1].childNodes[1].childNodes[3]
.childNodes[3].childNodes[1];
if (spanContainerPrice.childNodes.length > 1) {
const priceText = spanContainerPrice.childNodes[1].childNodes[2].textContent.trim();
const amount = priceText.replace(",", "").replace('"', "");
try {
const price = parseFloat(amount);
// console.log(price);
totalSpend += price;
} catch (error) {
console.log(error);
}
}
}
startingIndex += 10
}
}
}
console.log("Global Spend For All The Years is => " + globalSpend);
@meet30997
Copy link
Author

meet30997 commented Mar 1, 2022

Steps

  • Login to Amazon Website
  • Paste the code in the developer console
  • Change the year range for the search
    let yearToStartTheSearchFrom = 2015;
    let yearToEndTheSearch = 2022;
  • Hit Enter!

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