Analytics-Bot / src /json_creation /archive_orders.py
Viraj2307
Initial Commit
360b354
raw
history blame
2.97 kB
import requests
import base64
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad, pad
from datetime import datetime, timedelta
import json
from ..utils import dev_url
JWT_SECRET_KEY = "super-secret-key"
def encrypt(raw):
try:
raw = str(raw)
raw = pad(raw.encode(), 32)
cipher = AES.new(JWT_SECRET_KEY.encode("utf-8"), AES.MODE_ECB)
return base64.b64encode(cipher.encrypt(raw))
except Exception as e:
print(e)
return None
def get_date_range(period):
today = datetime.now()
if period == "week":
start_date = today - timedelta(days=7)
elif period == "month":
start_date = today - timedelta(days=30)
elif period == "six months":
start_date = today - timedelta(days=180)
elif period == "whole":
start_date = datetime(1970, 1, 1)
elif period == "yesterday":
start_date = today - timedelta(days=1)
end_date = start_date
elif period == "today":
start_date = today
end_date = start_date
else:
raise ValueError("Invalid period type. Choose from 'week', 'month', 'six months', 'whole', 'yesterday', or 'today'.")
end_date = today if period not in ["yesterday", "today"] else end_date
return start_date, end_date
def fetch_response(store_id, brand_id, period=None, start_date=None, end_date=None, hours=None, minutes=None, seconds=None):
if period:
start_date, end_date = get_date_range(period)
elif not start_date or not end_date:
raise ValueError("Start date and end date must be provided if period is not specified.")
if start_date == end_date:
if hours is not None and minutes is not None and seconds is not None:
start_date = start_date.replace(hour=hours, minute=minutes, second=seconds)
end_date = end_date.replace(hour=hours, minute=minutes, second=seconds)
else:
start_date = start_date.replace(hour=0, minute=0, second=0)
end_date = end_date.replace(hour=23, minute=59, second=59)
start_date_str = start_date.strftime("%Y-%m-%d %H:%M:%S")
end_date_str = end_date.strftime("%Y-%m-%d %H:%M:%S")
encrypted = encrypt(brand_id)
if encrypted is None:
print("Error in encryption")
return
url = f"https://dev.sahlhub.com/api/v1/order/archiveOrder?storeId={store_id}&startDate={start_date_str}&enddate={end_date_str}"
auth_token = "SAHL_COOKIE_KEY"
headers = {
"Content-Type": "application/json",
"userToken": auth_token,
"brandid": encrypted.decode("utf-8", "ignore"),
"origin": "https://pos-dev.sahlhub.com",
}
response = requests.request("GET", url, headers=headers)
if response.status_code == 200:
data = response.json()
return data
else:
print(f"Failed to fetch data. Status code: {response.status_code}")
print(response.text)
return None