Skip to content

Instantly share code, notes, and snippets.

@SamadiPour
Last active January 8, 2024 09:37
Show Gist options
  • Save SamadiPour/a18b1e186deac76c4a95fa108d02a6ea to your computer and use it in GitHub Desktop.
Save SamadiPour/a18b1e186deac76c4a95fa108d02a6ea to your computer and use it in GitHub Desktop.
Snappfood Spent money
cookies = Object.fromEntries(document.cookie.split('; ').map(c => c.split('=')));
let UDID = cookies.UDID;
let jwt = cookies[['jwt-access_token']] ?? JSON.parse(window.localStorage.JWT ?? '{}').access_token;
var myHeaders = new Headers();
myHeaders.append("authority", "snappfood.ir");
myHeaders.append("accept", "application/json, text/plain, */*");
myHeaders.append("accept-language", "en-US,en;q=0.9,fa;q=0.8");
myHeaders.append("authorization", "Bearer " + jwt);
myHeaders.append("content-type", "application/x-www-form-urlencoded");
myHeaders.append("referer", "https://snappfood.ir/profile/orders");
myHeaders.append("sec-ch-ua", "\".Not/A)Brand\";v=\"99\", \"Google Chrome\";v=\"103\", \"Chromium\";v=\"103\"");
myHeaders.append("sec-ch-ua-mobile", "?1");
myHeaders.append("sec-ch-ua-platform", "\"Android\"");
myHeaders.append("sec-fetch-dest", "empty");
myHeaders.append("sec-fetch-mode", "cors");
myHeaders.append("sec-fetch-site", "same-origin");
myHeaders.append("user-agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Mobile Safari/537.36");
const requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow'
};
let pageSize = 1000;
let pageNumber = 0;
let allOrders = [];
count = 1;
while (pageNumber * pageSize < count) {
console.log(`Getting ${pageNumber * pageSize} - ${(pageNumber + 1) * pageSize}...`);
let response = await fetch(`https://snappfood.ir/mobile/v1/order/reorder?optionalClient=WEBSITE&client=WEBSITE&deviceType=WEBSITE&appVersion=8.1.1&UDID=${UDID}&page=${pageNumber}&size=${pageSize}&locale=fa`, requestOptions);
const responseData = await response.json();
const orders = responseData.data.orders;
count = responseData.data.count ?? 0;
if (orders.length === 0) break;
allOrders = allOrders.concat(orders);
pageNumber++;
}
let price = 0;
let discount = 0;
for (let i = 0; i < allOrders.length; i++) {
let order = allOrders[i];
if (!order.orderCanceled) {
price += order['totalPrice'];
discount += order['sumAllDiscount'];
}
}
console.log(`---------------`);
console.log(`Total order: ${allOrders.length}`)
console.log(`Total spent: ${price.toLocaleString()}`);
console.log(`Total discount: ${discount.toLocaleString()}`);
console.log(`Oldest order date: ${allOrders.at(-1).startedAt}`);
@SamadiPour
Copy link
Author

With USD output:

cookies = Object.fromEntries(document.cookie.split('; ').map(c => c.split('=')));
let UDID = cookies.UDID;
let jwt = cookies[['jwt-access_token']] ?? JSON.parse(window.localStorage.JWT ?? '{}').access_token;

var myHeaders = new Headers();
myHeaders.append("authority", "snappfood.ir");
myHeaders.append("accept", "application/json, text/plain, */*");
myHeaders.append("accept-language", "en-US,en;q=0.9,fa;q=0.8");
myHeaders.append("authorization", "Bearer " + jwt);
myHeaders.append("content-type", "application/x-www-form-urlencoded");
myHeaders.append("referer", "https://snappfood.ir/profile/orders");
myHeaders.append("sec-ch-ua", "\".Not/A)Brand\";v=\"99\", \"Google Chrome\";v=\"103\", \"Chromium\";v=\"103\"");
myHeaders.append("sec-ch-ua-mobile", "?1");
myHeaders.append("sec-ch-ua-platform", "\"Android\"");
myHeaders.append("sec-fetch-dest", "empty");
myHeaders.append("sec-fetch-mode", "cors");
myHeaders.append("sec-fetch-site", "same-origin");
myHeaders.append("user-agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Mobile Safari/537.36");

const requestOptions = {
    method: 'GET',
    headers: myHeaders,
    redirect: 'follow'
};

let pageSize = 1000;
let pageNumber = 0;
let allOrders = [];
let count = 1;

while (pageNumber * pageSize < count) {
    console.log(`Getting ${pageNumber * pageSize} - ${(pageNumber + 1) * pageSize}...`);
    let response = await fetch(`https://snappfood.ir/mobile/v1/order/reorder?optionalClient=WEBSITE&client=WEBSITE&deviceType=WEBSITE&appVersion=8.1.1&UDID=${UDID}&page=${pageNumber}&size=${pageSize}&locale=fa`, requestOptions);
    const responseData = await response.json();
    const orders = responseData.data.orders;
    count = responseData.data.count ?? 0;

    if (orders.length === 0) break;

    allOrders = allOrders.concat(orders);
    pageNumber++;
}


let price = 0;
let discount = 0;
let years = new Set();
for (let i = 0; i < allOrders.length; i++) {
    let order = allOrders[i];
    years.add(new Date(order.startedAt).getFullYear());
    if (!order.orderCanceled) {
        price += order.totalPrice;
        discount += order.sumAllDiscount;
    }
}

let conversionRates = {};

async function fetchConversionRates(year) {
    const apiUrl = `https://raw.githubusercontent.com/SamadiPour/rial-exchange-rates-archive/main/gregorian/${year}/full`;

    try {
        const response = await fetch(apiUrl);
        const ratesData = await response.json();
        const prices = Object.fromEntries(
            Object.entries(ratesData).map(([key, value]) => [key, value.usd.buy])            
        );
        conversionRates = Object.assign(conversionRates, prices);
    } catch (error) {
        console.error(`Error fetching conversion rates for ${year}:`, error);
    }
}

console.log(`Fetching conversion rates...`);
const conversionPromises = [...years].map(fetchConversionRates);
await Promise.all(conversionPromises);

const today = new Date();
const yesterday = new Date(today);
yesterday.setDate(today.getDate() - 1);

let usd = 0;
for (let i = 0; i < allOrders.length; i++) {
    let order = allOrders[i];
    if (!order.orderCanceled) {
        const year = new Date(order.startedAt).getFullYear();
        usdRate = conversionRates[new Date(order.startedAt).toLocaleDateString('en-CA', { year: 'numeric', month: '2-digit', day: '2-digit' }).replace(/-/g, '/')] ?? conversionRates[yesterday.toLocaleDateString('en-CA', { year: 'numeric', month: '2-digit', day: '2-digit' }).replace(/-/g, '/')];
        usd += order.totalPrice / usdRate;
    }
}


console.log(`---------------`);
console.log(`Total order: ${allOrders.length}`)
console.log(`Total spent: ${price.toLocaleString()}`);
console.log(`Total spent in USD: ${usd.toLocaleString()}`)
console.log(`Total discount: ${discount.toLocaleString()}`);
console.log(`Oldest order date: ${allOrders.at(-1).startedAt}`);

@Heshmatkhah
Copy link

راهنمای استفاده از اسکریپت:

  • تو مرورگر دسکتاپ اسنپ فود رو باز کنید (نسخه موبایلشو باز نکنید)
  • اگر لاگین نیستید، لاگین کنید.
  • بعد راست کلیک کنید و گزینه Inspect رو انتخاب کنید
  • برید تب کنسول
  • اسکریپت رو paste کنید

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