Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add COVID-19 stats plugin #242

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions plugins/covid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import json
from urllib.request import urlopen

from cloudbot import hook

slugs = []
isos = []


def getglobal():
api = urlopen("https://api.covid19api.com/world")
data = json.loads(api.read())
data = data[0]
cases = data["TotalConfirmed"]
deaths = data["TotalDeaths"]
recovered = data["TotalRecovered"]
return "Global: Cases: %s Deaths: %s Recovered: %s" % (
cases,
deaths,
recovered,
)


def getcountry(country):
api = urlopen("https://api.covid19api.com/live/country/" + country)
data = json.loads(api.read())
fullname = data[0]["Country"]
cases = 0
deaths = 0
recovered = 0
for row in data:
cases += row["Confirmed"]
deaths += row["Deaths"]
recovered += row["Recovered"]
return "%s: Cases: %s Deaths: %s Recovered: %s" % (
fullname,
cases,
deaths,
recovered,
)


@hook.command("covid", autohelp=False)
def covid(text, reply):
r"""<country> - returns covid numbers"""
if len(slugs) <= 0 and len(isos) <= 0:
getcountries()
country = text.split(" ")[0]
if country == "":
country = "global"
if (
country not in slugs
and country.upper() not in isos
and country != "global"
):
return "Error, %s is not a valid country" % country
if country == "global":
response = getglobal()
else:
response = getcountry(country)
return response


@hook.on_connect
def getcountries():
if len(slugs) > 0 and len(isos) > 0:
return
slugs.clear()
isos.clear()
api = urlopen("https://api.covid19api.com/countries")
data = json.loads(api.read())
for row in data:
slugs.append(row["Slug"])
isos.append(row["ISO2"])