Created
September 9, 2018 17:16
-
-
Save tirkarthi/c36eebed4dc5c72d87ff6c176c857e3c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Go to order history page | |
# In console see requests to https://www.swiggy.com/dapi/order/all?order_id= and hit show more to download more JSON data. | |
# Collect all the JSON responses in an array to form swiggy.json | |
from collections import defaultdict | |
import json | |
FORMAT_STRING = "{name:>30} | {price:<8} | {delivery:<10} | {gst:<5} | {discount:<5}" | |
orders_list = [] | |
with open('swiggy.json') as f: | |
pages = json.loads(f.read()) | |
for page in pages: | |
for order in page['data']['orders']: | |
items = order['order_items'] | |
for item in items: | |
orders_list.append((item['name'], float(item['total']), | |
float(order['charges']['Delivery Charges']), | |
float(order['charges'].get('GST', 0)), | |
float(order['order_discount']))) | |
print(FORMAT_STRING.format(name="name", | |
price="price", | |
delivery="delivery", | |
gst="gst", | |
discount="discount")) | |
for item in sorted(orders_list): | |
print(FORMAT_STRING.format(name=item[0], | |
price=item[1], | |
delivery=item[2], | |
gst=item[3], | |
discount=item[4])) | |
order_total = sum(item[1] for item in orders_list) | |
delivery_total = sum(item[2] for item in orders_list) | |
gst_total = sum(item[3] for item in orders_list) | |
discount_total = sum(item[4] for item in orders_list) | |
print(FORMAT_STRING.format(name="", | |
price=round(order_total, 2), | |
delivery=round(delivery_total, 2), | |
gst=round(gst_total, 2), | |
discount=round(discount_total, 2))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment