Last active
May 8, 2024 16:59
-
-
Save tcdtist/fb62e02badddea1442b4d7527d04ffff to your computer and use it in GitHub Desktop.
LordShopee
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
function getPageTotal(offset, callback) { | |
var req = new XMLHttpRequest(); | |
req.open( | |
'GET', | |
`https://shopee.vn/api/v4/order/get_all_order_and_checkout_list?limit=20&offset=${offset}`, | |
true | |
); | |
req.onload = function() { | |
if (req.status >= 200 && req.status < 300) { | |
var response = JSON.parse(req.responseText); | |
if (response.data.next_offset === -1) { | |
callback(0, 0, 0); | |
} else { | |
var completedOrders = response.data.order_data.details_list | |
.filter(function(item) { | |
return item.status.status_label.text === 'label_order_completed'; | |
}); | |
var completedOrdersTotal = completedOrders.reduce(function(total, order) { | |
return total + order.info_card.final_total; | |
}, 0); | |
var productsPurchased = completedOrders.reduce(function(count, order) { | |
return count + order.info_card.order_list_cards.reduce(function(sum, card) { | |
return sum + card.product_info.item_groups.reduce(function(groupSum, group) { | |
return groupSum + group.items.reduce(function(itemSum, item) { | |
return itemSum + item.amount; | |
}, 0); | |
}, 0); | |
}, 0); | |
}, 0); | |
callback(completedOrdersTotal, completedOrders.length, productsPurchased); | |
} | |
} else { | |
console.error('Request failed: ' + req.statusText); | |
callback(null, 0, 0); | |
} | |
}; | |
req.onerror = function() { | |
console.error('Network Error'); | |
callback(null, 0, 0); | |
}; | |
req.send(); | |
} | |
function formatMoney(value) { | |
return Intl.NumberFormat('vi-VN').format(value / 100000); | |
} | |
var totalSpent = 0; | |
var totalOrders = 0; | |
var totalProductsPurchased = 0; | |
var currentOffset = 0; | |
function fetchPage() { | |
getPageTotal(currentOffset, function(pageTotal, ordersCount, productsCount) { | |
if (pageTotal === 0) { | |
console.log("================================"); | |
console.log("Tổng tiền bạn đã chi tiêu cho Shopee là: " + formatMoney(totalSpent) + " vnđ"); | |
console.log("Số lượng sản phẩm đã đặt: " + totalProductsPurchased + " sản phẩm"); | |
console.log("Tổng đơn hàng đã giao: " + totalOrders + " đơn hàng"); | |
console.log("================================"); | |
} else if (pageTotal !== null) { | |
totalSpent += pageTotal; | |
totalOrders += ordersCount; | |
totalProductsPurchased += productsCount; | |
currentOffset += 20; | |
console.log("Đã thống kê được: " + currentOffset + " đơn hàng - chi tiêu: " + formatMoney(totalSpent) + " vnđ"); | |
fetchPage(); | |
} else { | |
console.log("An error occurred during fetching data."); | |
} | |
}); | |
} | |
fetchPage(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment