Skip to content

Instantly share code, notes, and snippets.

@andrast0th
Last active April 6, 2024 12:23
Show Gist options
  • Save andrast0th/6ad6641b53a089eeb97918f2f1b829a5 to your computer and use it in GitHub Desktop.
Save andrast0th/6ad6641b53a089eeb97918f2f1b829a5 to your computer and use it in GitHub Desktop.
emag.ro API, calculate total money spent
// use nodejs to run this :)
// login to emag with a browser, find a req, copy the value of the cookie header here
const COOKIE_VAL = "FILL_ME_BABY"
const USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36";
const API_URL = "https://www.emag.ro/history/shopping/{pageNo}?source=front&time=older";
fetchAllOrders().then(allOrders => {
let total = 0;
console.log(`Calc total spent...`);
allOrders.forEach(order => {
const orderTotal = order?.general_data?.order_total;
if(orderTotal) {
total += order.general_data.order_total;
} else {
console.warn("0 order total", `https://www.emag.ro${order.actions.view_details_url}`);
}
})
console.log(`No of orders: ${allOrders.length}, Total spent: ${total}`);
});
async function fetchAllOrders() {
const orders = [];
let currentPageNo = 1;
const page = await fetchPage(currentPageNo);
orders.push(...page.pageVars.orders)
const totalPageNo = page.pageVars.pagination.total;
while(currentPageNo < totalPageNo) {
currentPageNo++;
const page = await fetchPage(currentPageNo);
orders.push(...page.pageVars.orders);
}
return orders;
}
async function fetchPage(pageNo) {
try {
const url = API_URL.replace('{pageNo}', pageNo + '');
console.log('Fetching: ', url);
const res = await fetch(url, {
method: 'GET',
headers: {
'Cookie': COOKIE_VAL,
'User-Agent': USER_AGENT,
'Accept': 'application/json',
},
});
console.log('Resp Status:', res.status);
return await res.json();
} catch (err) {
console.log(err.message); //can be console.error
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment