Skip to content

Instantly share code, notes, and snippets.

@Yukaii
Last active August 22, 2022 13:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yukaii/200056f0c517fbd51eb20c7c43edbffe to your computer and use it in GitHub Desktop.
Save Yukaii/200056f0c517fbd51eb20c7c43edbffe to your computer and use it in GitHub Desktop.
Export shopee orders to JSON file

Export Shopee Order

Installation

  1. Install Tampermonkey / Greasemonkey(Firefox)
  2. Click this link and install

Usage

  1. Visit any shopee.tw domain
  2. In the right-click menu, select Export Shopee Orders to JSON スクリーンショット 2022-08-21 午後8 38 05
  3. Now you can do JSON manipulation with jq like: スクリーンショット 2022-08-21 午後8 46 22 Oh shit

Appendix

Some useful jq queries

  1. Show total money spent: jq '[.[] | .info_card.final_total] | add | . / 100000'
  2. Total order price(without the shipping fee): jq '[.[] | .info_card.order_list_cards | .[] | .items | .[] | .order_price * .amount] | add | . / 100000'
  3. Combine these two, then you got the shipping fee: jq '. as $parent | [.[] | .info_card.order_list_cards | .[] | .items | .[] | .order_price * .amount] | add | . / 100000 | . as $raw_total | $parent | [.[] | .info_card.final_total] | add | . / 100000 | . as $total | $total - $raw_total'
// ==UserScript==
// @name Export Shopee Orders to JSON
// @namespace https://yukai.dev
// @version 0.1.3
// @description Export Shopee Orders to JSON
// @author Yukai Huang
// @match https://shopee.tw/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=shopee.tw
// @downloadURL https://gist.githubusercontent.com/Yukaii/200056f0c517fbd51eb20c7c43edbffe/raw/export-shopee-order.user.js
// @updateURL https://gist.githubusercontent.com/Yukaii/200056f0c517fbd51eb20c7c43edbffe/raw/export-shopee-order.user.js
// @grant GM_openInTab
// @run-at context-menu
// ==/UserScript==
(async function() {
'use strict';
function getAllOrderAndCheckoutList(offset = 0, limit = 20) {
return fetch(
`https://shopee.tw/api/v4/order/get_all_order_and_checkout_list?limit=${limit}&offset=${offset}`,
).then((res) => res.json());
}
async function getOrders() {
let orders = []
for (let offset = 0; ;) {
const { error, data } = await getAllOrderAndCheckoutList(offset);
if (error || !data) {
break
}
const { next_offset, order_data } = data
if (next_offset === -1) {
break
}
orders.push(...order_data.details_list);
offset = next_offset
}
return orders;
}
function saveOrders(orders) {
const data = JSON.stringify(orders, null, 2);
// prompt browser to download file
const blob = new Blob([data], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
const timestamp = new Date().toISOString().replace(/:/g, '-');
a.download = `orders-${timestamp}.json`;
a.click();
}
const orders = await getOrders();
saveOrders(orders)
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment