forked from friendly-telegram/modules-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
corona.py
executable file
·84 lines (66 loc) · 3.41 KB
/
corona.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Friendly Telegram (telegram userbot)
# Copyright (C) 2018-2020 The Authors
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import logging
import requests
import json
from datetime import datetime
import dateutil.parser
from .. import loader, utils
logger = logging.getLogger(__name__)
class CoronaReportsMod(loader.Module):
"""Gets the latest COVID-19 data found in JHU database for a country"""
strings = {"name": "Corona"}
def __init__(self):
self.config = loader.ModuleConfig("DEFAULT_COUNTRY", ("spain"),
"Enter your default country here")
async def coronacmd(self, message):
""".corona <country (Optional)>"""
args = utils.get_args_raw(message)
if not args:
country = self.config["DEFAULT_COUNTRY"]
else:
country = args
message = await utils.answer(message, "<code>Visiting Wuhan...</code>")
url = "https://covid19.mathdro.id/api/countries/" + country
tries = 0
response = requests.get(url)
while response.status_code == 400 and tries < 10:
response = requests.get(url)
tries += 1
await utils.answer(message, "<code>Try #" + str(tries) + "...</code>")
jsonDumps = json.dumps(response.json(), sort_keys=True)
jsonResponse = json.loads(jsonDumps)
if(response.status_code == 200):
confirmed = jsonResponse['confirmed']['value']
recovered = jsonResponse['recovered']['value']
deaths = jsonResponse['deaths']['value']
active = confirmed - recovered - deaths
try:
lastUpdate = dateutil.parser.parse(jsonResponse['lastUpdate']).strftime("%d/%m/%Y - %X")
except (ValueError, TypeError) as e:
logger.error(e)
lastUpdate = jsonResponse['lastUpdate']
msg = "<s>-------------------------------------</s>\n";
msg += "👑🦠 in "+ country.capitalize() + "<i> "+lastUpdate+"</i>\n"
msg += "<s>-------------------------------------</s>\n";
msg+= "<b>😷 Confirmed:</b> " + str(confirmed)
msg+= "\n<b>🤧 Active:</b> " + str(active) + " (" + str(round(active/confirmed * 100, 2)) + "%)"
msg+= "\n<b>🏥 Recovered:</b> " + str(recovered) + " (" + str(round(recovered/confirmed * 100, 2)) + "%)"
msg+= "\n<b>💀 Deaths:</b> " + str(deaths) + " (" + str(round(deaths/confirmed * 100, 2)) + "%)"
elif response.status_code == 404:
msg = "<code>"+jsonResponse['error']['message']+"</code>"
elif response.status_code == 400:
msg = "<code>Bad request</code>"
else:
msg = "<code>Unknown error</code>"
await utils.answer(message, msg)