This repository has been archived on 2023-06-21. You can view files and clone it, but cannot push or open issues or pull requests.

91 lines
3.0 KiB
Python
Raw Normal View History

2021-12-19 02:52:06 +02:00
import requests
from math import ceil
import json
from discord_webhook import DiscordEmbed, DiscordWebhook
import time
# request to government free information
new_records = requests.get("https://data.egov.bg/resource/download/e59f95dd-afde-43af-83c8-ea2916badd19/json").json()
del new_records[0]
# counter to fight limit of embeds
counter = 1
data = {}
try:
with open("history.json", "r") as f:
old_data = json.load(f)
print(type(old_data))
f.close()
except json.decoder.JSONDecodeError:
old_data = None
print("No data!")
for i in new_records:
if old_data:
if not i[0] in old_data.keys():
data[i[0].replace("\/","/")] = {
"Confirmed": int(i[3]),
"New Cases": int(i[5]),
"Tests": int(i[2]),
"Positive Percentage": ceil(int(i[5]) / int(i[2]) * 100),
"Vaccine Percentage": "N/A"
}
else:
data[i[0].replace("\/","/")] = {
"Confirmed": int(i[3]),
"New Cases": int(i[5]),
"Tests": int(i[2]),
"Positive Percentage": ceil(int(i[5]) / int(i[2]) * 100),
"Vaccine Percentage": "N/A"
}
if old_data:
with open("history.json", "w") as file:
json.dump(old_data.update(data), file, indent = 4)
else:
with open("history.json", "w") as file:
json.dump(data, file, indent = 4)
def discord_daily(datum=data):
webhook = DiscordWebhook(url="https://discord.com/api/webhooks/920787339146575943/ojsBB9nlWPnLp3ncW726_4UrckXOHBEHB5YJ7KDrwAnn_6tQTmDWitkY6yaxsxkeLnKC")
for i in data.keys():
if i == "2021/12/18":
print(i)
# Color coding levels of danger
if data[i]["Positive Percentage"] >= 10:
color = "fff00"
elif data[i]["Positive Percentage"] >= 30:
color = "ff000"
elif data[i]["Positive Percentage"] >= 50:
color = "ffa500"
elif data[i]["Positive Percentage"] >= 80:
color = "000000"
else:
color = "00ff00"
embed = DiscordEmbed(title=f'Covid Daily - {i}', description='Daily briefing!', color=color)
for y in data[i].keys():
print(y)
if "Percentage" in y:
embed.add_embed_field(name=str(y), value=str(data[i][y])+"%")
else:
embed.add_embed_field(name=str(y), value=str(data[i][y]))
embed.set_footer(text='Data taken from https://worldometer.com and https://coronavirus.bg')
webhook.add_embed(embed)
webhook.execute()
time.sleep(1)
global counter
if counter % 5 == 0:
webhook = DiscordWebhook(url="https://discord.com/api/webhooks/920787339146575943/ojsBB9nlWPnLp3ncW726_4UrckXOHBEHB5YJ7KDrwAnn_6tQTmDWitkY6yaxsxkeLnKC", content="-----------")
time.sleep(5)
counter += 1
discord_daily()