Last active
March 2, 2022 08:10
-
-
Save meet30997/6fe33ddcdb91edf0a6165205ccdff50c to your computer and use it in GitHub Desktop.
Amazon | Scrap the total amount you’ve ever spent on Amazon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Steps
let yearToStartTheSearchFrom = 2015;
let yearToEndTheSearch = 2022;