From 8e8ac7d995e9ade899e017f82b60f42e10ec833f Mon Sep 17 00:00:00 2001 From: examknow Date: Tue, 13 Oct 2020 20:29:25 -0400 Subject: [PATCH 1/3] add covid stats plugin --- plugins/covid.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 plugins/covid.py diff --git a/plugins/covid.py b/plugins/covid.py new file mode 100644 index 000000000..d153dbe0c --- /dev/null +++ b/plugins/covid.py @@ -0,0 +1,70 @@ +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""" - returns covid numbers""" + country = text.split(" ")[0] + if ( + not country in slugs + and not country.upper() in isos + and not 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"]) From 48ee4484921c9638305ef1232b23409bd4a50899 Mon Sep 17 00:00:00 2001 From: examknow Date: Tue, 13 Oct 2020 20:37:24 -0400 Subject: [PATCH 2/3] make .covid work after reload --- plugins/covid.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugins/covid.py b/plugins/covid.py index d153dbe0c..f407dc605 100644 --- a/plugins/covid.py +++ b/plugins/covid.py @@ -43,6 +43,8 @@ def getcountry(country): @hook.command("covid", autohelp=False) def covid(text, reply): r""" - returns covid numbers""" + if len(slugs) <= 0 and len(isos) <= 0: + getcountries() country = text.split(" ")[0] if ( not country in slugs From 471b141b93aa4f1198c9851825d45b133d83517a Mon Sep 17 00:00:00 2001 From: examknow Date: Tue, 13 Oct 2020 20:40:10 -0400 Subject: [PATCH 3/3] make country default to global --- plugins/covid.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/plugins/covid.py b/plugins/covid.py index f407dc605..31c1a94cc 100644 --- a/plugins/covid.py +++ b/plugins/covid.py @@ -46,10 +46,12 @@ def covid(text, reply): if len(slugs) <= 0 and len(isos) <= 0: getcountries() country = text.split(" ")[0] + if country == "": + country = "global" if ( - not country in slugs - and not country.upper() in isos - and not country == "global" + 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":