Skip to content

Instantly share code, notes, and snippets.

@lucas34
Last active April 21, 2022 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucas34/1858bc98d219bf20c02c2755c44cc9f8 to your computer and use it in GitHub Desktop.
Save lucas34/1858bc98d219bf20c02c2755c44cc9f8 to your computer and use it in GitHub Desktop.
Convert FairPrice invoice to CSV
import requests
import json
import pandas as pd
import sys
if len(sys.argv) != 3:
print("bearer token needed as argument")
exit(1)
headers = {"Authorization": sys.argv[1] + " " + sys.argv[2]}
def get_prucharse_history(page_token = 1):
url = 'https://pay.fairprice.com.sg/api/pay/invoices?pageToken={}&pageSize=100'.format(page_token)
r = requests.get(url, headers=headers)
if r.status_code != 200:
print("HTTP ERROR code " + str(r.status_code))
print(r.json())
exit()
else:
result = r.json()
data = result["data"]
total_items = data["totalSize"]
items = data["invoices"]
if 'nextPageToken' in data:
print(".")
return get_prucharse_history(data["nextPageToken"]) + items
else:
return items
# Call API to get list of purchased items link to this invoice Id
def get_item_purchased(id):
url = 'https://pay.fairprice.com.sg/api/pay/invoices/{}/items'.format(id)
r = requests.get(url, headers=headers)
if r.status_code != 200:
print("Fail to load invoice id=" + id)
print("HTTP ERROR code " + str(r.status_code))
print(r.json())
exit()
else:
return r.json()["data"]["items"]
# Remove uncessary field to keep only item "Name", "Quantity" and "Price" (Price refer to total price for all identicals items)
def shrink_json(items, date):
result = []
for item in items:
elements = {"date": date, "name": item["name"], "quantity": item["quantity"], "total_price": item["price"]}
result.insert(0, elements)
return result
def result_to_csv(result):
df = pd.read_json(json.dumps(result))
df.to_csv(r'faireprice_result.csv', index=None)
print("DONE")
def pull_all_data():
result = []
print("Fetching purchase history")
invoices = get_prucharse_history()
print("Number of invoices: " + str(len(invoices)))
for idx, invoice in enumerate(invoices):
print("Fetching " + str(idx + 1) + "/" + str(len(invoices)))
item = shrink_json(get_item_purchased(invoice["id"]), str(invoice["invoiceDate"]))
result = result + item
return result
# Setup
# pip install requests
# pip install pandas
# Get your Bearer token
# Login to https://www.fairprice.com.sg/ and with developer console, any request contains the token in the header
# python fairprice.py Bearer ABCDEF
# Result is in faireprice_result.csv
result_to_csv(pull_all_data())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment