Created
March 10, 2020 08:20
-
-
Save yolossn/c29d855a755768b4cf3886f4afedb0be to your computer and use it in GitHub Desktop.
Python script to download swiggy bills.
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
import os | |
import datetime | |
import requests | |
def download_order(order_info,headers,download_path): | |
order_id = str(order_info['order_id']) | |
url = "https://www.swiggy.com/invoice/download/" | |
response = requests.get(url+order_id,headers = headers) | |
file_name = order_id+".pdf" | |
file_path = os.path.join(download_path,file_name) | |
with open(file_path, "wb+") as invoice_file: | |
invoice_file.write(response.content) | |
def get_orders(headers,order_id): | |
url = "https://www.swiggy.com/dapi/order/all?order_id=" | |
if order_id != "": | |
url += order_id | |
response = requests.get(url,headers = headers) | |
resp = response.json() | |
return resp["data"]["orders"] | |
from_date = datetime.date(2019,1,1) | |
path = os.getcwd() | |
download_path = os.path.join(path, "swiggy") | |
if not os.path.exists(download_path): | |
print("Creating the path as it doesn't exists") | |
os.makedirs(download_path) | |
print("Created path", download_path) | |
headers = { | |
'Accept-Language': 'en-US,en;q=0.5', | |
'Referer': 'https://www.swiggy.com/my-account/orders', | |
'Content-Type': 'application/json', | |
'__fetch_req__': 'true', | |
'Connection': 'keep-alive', | |
'Cookie': '', # Add your cookie here | |
'TE': 'Trailers' | |
} | |
last_order = "" | |
total_order_amount = 0 | |
count = 0 | |
Processed = True | |
while Processed: | |
orders = get_orders(headers,last_order) | |
for order in orders: | |
last_order = str(order['order_id']) | |
print("Processing order",last_order) | |
if order["order_type"] != "regular": | |
print("Skipping non regular order",last_order) | |
continue | |
if order["order_status"] == "cancelled": | |
print("Cancelled order skipping", last_order) | |
continue | |
count += 1 | |
order_date = datetime.datetime.fromtimestamp(int(order["delivered_time_in_seconds"])) | |
if from_date > order_date.date(): | |
print("Stopping as",from_date,"is greater than",order_date.date()) | |
Processed = False | |
break | |
download_order(order,headers,download_path) | |
total_order_amount += order['order_total'] | |
print("Processed order Id",last_order,"order date",order_date.date(),"bill amount",order['order_total']) | |
print("amount as of order:",total_order_amount) | |
print("total_orders:",count) | |
print("total_amount:",total_order_amount) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment