Skip to content

Instantly share code, notes, and snippets.

@brockoala
Forked from Vinh2uang/Shopee.js
Last active December 8, 2024 17:05
Show Gist options
  • Save brockoala/ae6b9f3e4042688576c65ce9a394f72a to your computer and use it in GitHub Desktop.
Save brockoala/ae6b9f3e4042688576c65ce9a394f72a to your computer and use it in GitHub Desktop.
Cách tính tổng số tiền đã mua hàng trên Shopee
/*
Sửa đổi so với bản gốc, bởi ChatGPT 4o:
- Thêm ngày giờ cho mỗi đơn hàng
- Nhóm đơn hàng theo năm
- Thêm chi tiết thống kê theo năm trong tổng kết và trình bày dễ đọc hơn
- Dùng phân cách cột bằng kí tự Tab để paste được thẳng vào Google Sheets
*/
/*CÁCH DÙNG:
B1: Mở Google Chrome, truy cập và đăng nhập vào Shopee.vn
B2: Vào mục "Đơn Mua": https://shope.ee/7pHF2r5lo0
B3: Nhấn tổ hợp phím Ctrl+Shift+J để mở tab "Console"
B4: Copy toàn bộ Code ở dưới. Paste vào tab "Console". Sau đó nhấn "Enter".
B5: Ra 1 box nhỏ cạnh trang Shopee, các bạn kéo ra, copy và paste vào Excel hoặc Google Sheet để xem
Nguồn: @nttkq */
async function getOrders(offset, limit) {
let url = "https://shopee.vn/api/v4/order/get_all_order_and_checkout_list?limit=" + limit + "&offset=" + offset;
var ordersData = (await (await fetch(url)).json()).data.order_data;
var detailList = ordersData.details_list;
if (detailList) {
return detailList;
} else {
return [];
}
}
function _VietNamCurrency(number) {
return new Intl.NumberFormat('vi-VN', { style: 'currency', currency: 'VND' }).format(number);
}
async function getAllOrders() {
const limit = 20;
let offset = 0;
let allOrders = [];
let yearlySpending = {}; // To store spending by year
let unknownSpending = { spent: 0, orders: 0, items: 0 }; // For orders with unknown time
allOrders.push(
[
'Ngày giờ\tTổng tiền', 'Tên chung', 'Số lượng', 'Trạng thái', 'Tên shop', 'Chi tiết'
].join('\t')
);
let totalSpent = 0;
let totalItems = 0;
let totalOrders = 0;
let currentYear = null;
while (true) {
let data = await getOrders(offset, limit);
if (data.length == 0) break;
for (const item of data) {
const infoCard = item.info_card;
const listType = item.list_type;
const ctime = item.shipping?.tracking_info?.ctime;
const orderDate = ctime ? new Date(ctime * 1000) : null;
const year = orderDate ? orderDate.getFullYear() : currentYear || "Unknown time";
const formattedDate = orderDate ? orderDate.toLocaleString('vi-VN') : "Unknown";
if (!yearlySpending[year]) {
yearlySpending[year] = { spent: 0, orders: 0, items: 0 };
}
if (currentYear !== year) {
allOrders.push("\n-------------------------" + `\nNăm ${year === "Unknown time" ? "Unknown time" : year}\n-------------------------`);
currentYear = year;
}
let strListType;
switch (listType) {
case 3: strListType = "Hoàn thành"; break;
case 4: strListType = "Đã hủy"; break;
case 7: strListType = "Vận chuyển"; break;
case 8: strListType = "Đang giao"; break;
case 9: strListType = "Chờ thanh toán"; break;
case 12: strListType = "Trả hàng"; break;
default: strListType = "Không rõ"; break;
}
const productCount = infoCard.product_count;
let subTotal = infoCard.subtotal / 1e5;
const orderCard = infoCard.order_list_cards[0];
const shopName = orderCard.shop_info.username + " - " + orderCard.shop_info.shop_name;
const products = orderCard.product_info.item_groups;
const productSummary = products.map(product => product.items.map(item => item.name.replace(/\n/g, " ") + "--amount: " + item.amount + "--price: " + _VietNamCurrency(item.item_price)).join(', ')).join('; ');
const name = products[0].items[0].name.replace(/\n/g, " ");
if (listType != 4 && listType != 12) {
totalSpent += subTotal;
yearlySpending[year].spent += subTotal;
yearlySpending[year].orders += 1;
yearlySpending[year].items += productCount;
} else {
subTotal = 0;
}
totalOrders += 1;
totalItems += productCount;
const subTotalNative = _VietNamCurrency(subTotal);
allOrders.push(
[
`${formattedDate}\t${subTotalNative}`, name, productCount, strListType, shopName, productSummary
].join('\t')
);
}
console.log('Collected: ' + offset);
offset += limit;
}
allOrders.push("\n\n-------------------------" +
"\nChi tiêu theo năm\n" +
"-------------------------");
for (const year in yearlySpending) {
if (year === "Unknown time" || (!isNaN(year) && year >= 2021 && year <= 2024)) { // Include valid years and unknown time
allOrders.push(
`${year === "Unknown time" ? "Unknown time" : `Năm ${year}`}:
- Tổng tiền chi tiêu: ${_VietNamCurrency(yearlySpending[year].spent)}
- Tổng đơn hàng: ${yearlySpending[year].orders} đơn hàng
- Tổng sản phẩm: ${yearlySpending[year].items} sản phẩm
`
);
}
}
allOrders.push("\n-------------------------" +
"\nTổng chi tiêu tất cả các năm\n-------------------------");
allOrders.push(
`Tổng tiền chi tiêu: ${_VietNamCurrency(totalSpent)}\n` +
`Tổng đơn hàng: ${totalOrders} đơn hàng\n` +
`Tổng sản phẩm: ${totalItems} sản phẩm\n`
);
var text = allOrders.join('\r\n');
document.write('<textarea>' + text + '</textarea>');
}
getAllOrders();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment