I have been working on a function which I call report. What it does is that whenever I call the report function. I can give it a payload of my dict with some data and as well as reason where I can have custom message or the exception errors of whatever can happened.
For now I have created something simple where I create names for file_path and filename. I check if there is a path for that name already and if not then we create. We create a JSON file with the report data to my local PC and then I send it to discord. Both the JSON file and a small text that describes that there has been and error and to check it out ASAP.
My question here is pretty simple to see if there is anything I can actually improve my code somehow. Make it less code or a better report, I take whatever possible!
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import json
import os
import time
from datetime import datetime
from typing import List
from discord_webhook import DiscordEmbed, DiscordWebhook
# payload example payload = {"store": "burger-king"}
# reason = Custom message or exception err
def report(payload=None, reason=None):
webhook = DiscordWebhook(
url="https://discord....."
)
embed = DiscordEmbed(title="An error has occurred", color=16711680)
# -------------------------------------------------------------------------
# Create file path and filename
# -------------------------------------------------------------------------
file_path = f'./error/{payload["store"] if payload else "Other"}'
filename = f'{datetime.strftime(datetime.now(), "%Y-%m-%d_%H-%M-%S-%f")}.json'
# Check if the path already exists
if not os.path.exists(file_path):
os.makedirs(file_path)
# Write to a JSON file
with open(f"{file_path}/{filename}", "w", encoding="utf-8") as f:
f.write(json.dumps(
{
"payload": payload if payload else None,
"reason": str(reason)
},
indent=4,
ensure_ascii=False
))
# -------------------------------------------------------------------------
# Filename embed
# -------------------------------------------------------------------------
embed.add_embed_field(name="Filename", value=filename, inline=False)
# -------------------------------------------------------------------------
# Send files to discord
# -------------------------------------------------------------------------
with open(f"{file_path}/{filename}", "rb") as f:
webhook.add_file(file=f.read(), filename=f"{filename}")
# -------------------------------------------------------------------------
# Footer timestamp
# -------------------------------------------------------------------------
embed.set_footer(text=f'AutoSnkr | {datetime.now().strftime("%Y-%m-%d [%H:%M:%S.%f")[:-3]}]')
webhook.add_embed(embed)
# -------------------------------------------------------------------------
# Send to discord with exceptions
# -------------------------------------------------------------------------
while True:
response = webhook.execute()
# Workaround bug from discord_webhook pypi
if isinstance(response, List):
assert (len(response) == 1)
response = response[0]
# Successful requests
if response.ok:
return
# Rate limit. Wait until the limitation is gone
elif response.status_code == 429:
sleep_time = int(response.headers["retry-after"]) / 1000
time.sleep(sleep_time)
else:
print("Big error here! This text needs to be replaced later on")
return