Caught up again
This commit is contained in:
parent
7119cc0786
commit
c8b080c2be
90
catchup.py
Normal file
90
catchup.py
Normal file
@ -0,0 +1,90 @@
|
||||
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()
|
22
history.json
22
history.json
@ -3911,5 +3911,27 @@
|
||||
"Tests": 33261,
|
||||
"Positive Percentage": 6,
|
||||
"Vaccine Percentage": "N/A"
|
||||
},
|
||||
"2021/12/17": {
|
||||
"Confirmed": 721819,
|
||||
"New Cases": 1443,
|
||||
"Tests": 35829,
|
||||
"Positive Percentage": 5,
|
||||
"Vaccine Percentage": "N/A"
|
||||
},
|
||||
"2021/12/18": {
|
||||
"Confirmed": 723433,
|
||||
"New Cases": 1614,
|
||||
"Tests": 45925,
|
||||
"Positive Percentage": 4,
|
||||
"Vaccine Percentage": "N/A"
|
||||
},
|
||||
"2021/12/19": {
|
||||
"Confirmed": 724337,
|
||||
"New Cases": 904,
|
||||
"Tests": 26744,
|
||||
"Positive Percentage": 4,
|
||||
"Vaccine Percentage": 28
|
||||
}
|
||||
}}
|
||||
}
|
4
main.py
4
main.py
@ -7,9 +7,9 @@ import json
|
||||
from math import ceil
|
||||
|
||||
# while True:
|
||||
TODAY = str(date.today().strftime("%d/%m/%Y"))
|
||||
TODAY = str(date.today().strftime("%Y/%m/%d"))
|
||||
YESTERDAY = date.today() - timedelta(days = 1)
|
||||
YESTERDAY = str(YESTERDAY.strftime("%d/%m/%Y"))
|
||||
YESTERDAY = str(YESTERDAY.strftime("%Y/%m/%d"))
|
||||
|
||||
def write_json(new_data, filename='history.json'):
|
||||
with open(filename,'r+') as file:
|
||||
|
Reference in New Issue
Block a user