-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
118 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from .client import Client | ||
from .statistics import GlobalStatistics, CountryStatistics | ||
from .client import Client | ||
from .statistics import GlobalStatistics, CountryStatistics | ||
from .utils import format_date, format_number |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,87 @@ | ||
from datetime import datetime | ||
import aiohttp | ||
from .utils import format_number | ||
from .statistics import GlobalStatistics, CountryStatistics | ||
|
||
class APIerror(Exception): | ||
pass | ||
|
||
class Client: | ||
""" | ||
Handles interactions with the corona.lmao.ninja API | ||
""" | ||
def __init__(self): | ||
self.session = aiohttp.ClientSession() | ||
self.global_data = 'https://corona.lmao.ninja/all' | ||
self.all_countries = 'https://corona.lmao.ninja/countries' | ||
self.country_data = 'https://corona.lmao.ninja/countries/{}' | ||
|
||
async def all(self): | ||
""" | ||
Get the global stats for Coronavirus COVID-19 | ||
""" | ||
async with self.session.get(self.global_data) as resp: | ||
if not resp.status == 200: | ||
raise APIerror('Failed to get global data.') | ||
|
||
global_data = await resp.json() | ||
|
||
async with self.session.get(self.all_countries) as resp: | ||
if not resp.status == 200: | ||
raise APIerror('Failed to get global data.') | ||
|
||
country_data = await resp.json() | ||
|
||
cases = global_data.get("cases", 0) | ||
deaths = global_data.get("deaths", 0) | ||
recoveries = global_data.get("recovered", 0) | ||
updated_epoch = global_data.get("updated", None) | ||
updated = datetime.fromtimestamp(updated_epoch/1000.0) | ||
|
||
today_cases = 0 | ||
today_deaths = 0 | ||
total_critical = 0 | ||
|
||
for c in country_data: | ||
today_cases += c["todayCases"] if c["todayCases"] else 0 | ||
today_deaths += c["todayDeaths"] if c["todayDeaths"] else 0 | ||
total_critical += c["critical"] if c["critical"] else 0 | ||
|
||
return GlobalStatistics(cases, | ||
deaths, | ||
recoveries, | ||
today_cases, | ||
today_deaths, | ||
total_critical, | ||
updated | ||
) | ||
|
||
|
||
async def get_country_data(self, country): | ||
""" | ||
Get the data for a specific country. | ||
""" | ||
async with self.session.get(self.country_data.format(country)) as resp: | ||
if not resp.status == 200: | ||
raise APIerror('Failed to get country data data.') | ||
|
||
country_stats = await resp.json() | ||
|
||
country_name = country_stats.get("country", "Null") | ||
total_country_cases = country_stats.get("cases", 0) | ||
total_country_deaths = country_stats.get("deaths", 0) | ||
total_country_recoveries = country_stats.get("recovered", 0) | ||
today_cases = country_stats.get("todayCases", 0) | ||
today_deaths = country_stats.get("todayDeaths", 0) | ||
total_critical = country_stats.get("critical", 0) | ||
cases_per_million = country_stats.get("casesPerOneMillion", 0) | ||
|
||
return CountryStatistics(country_name, | ||
total_country_cases, | ||
total_country_deaths, | ||
total_country_recoveries, | ||
today_cases, today_deaths, | ||
total_critical, | ||
cases_per_million | ||
) | ||
from datetime import datetime | ||
import aiohttp | ||
from .utils import format_number | ||
from .statistics import GlobalStatistics, CountryStatistics | ||
|
||
class APIerror(Exception): | ||
pass | ||
|
||
class Client: | ||
""" | ||
Handles interactions with the corona.lmao.ninja API | ||
""" | ||
def __init__(self): | ||
self.session = aiohttp.ClientSession() | ||
self.global_data = 'https://corona.lmao.ninja/all' | ||
self.all_countries = 'https://corona.lmao.ninja/countries' | ||
self.country_data = 'https://corona.lmao.ninja/countries/{}' | ||
|
||
async def all(self): | ||
""" | ||
Get the global stats for Coronavirus COVID-19 | ||
""" | ||
async with self.session.get(self.global_data) as resp: | ||
if not resp.status == 200: | ||
raise APIerror('Failed to get global data.') | ||
|
||
global_data = await resp.json() | ||
|
||
async with self.session.get(self.all_countries) as resp: | ||
if not resp.status == 200: | ||
raise APIerror('Failed to get global data.') | ||
|
||
country_data = await resp.json() | ||
|
||
cases = global_data.get("cases", 0) | ||
deaths = global_data.get("deaths", 0) | ||
recoveries = global_data.get("recovered", 0) | ||
updated_epoch = global_data.get("updated", None) | ||
updated = datetime.fromtimestamp(updated_epoch/1000.0) | ||
|
||
today_cases = 0 | ||
today_deaths = 0 | ||
total_critical = 0 | ||
|
||
for c in country_data: | ||
today_cases += c["todayCases"] if c["todayCases"] else 0 | ||
today_deaths += c["todayDeaths"] if c["todayDeaths"] else 0 | ||
total_critical += c["critical"] if c["critical"] else 0 | ||
|
||
return GlobalStatistics(cases, | ||
deaths, | ||
recoveries, | ||
today_cases, | ||
today_deaths, | ||
total_critical, | ||
updated | ||
) | ||
|
||
|
||
async def get_country_data(self, country): | ||
""" | ||
Get the data for a specific country. | ||
""" | ||
async with self.session.get(self.country_data.format(country)) as resp: | ||
if not resp.status == 200: | ||
raise APIerror('Failed to get country data data.') | ||
|
||
country_stats = await resp.json() | ||
|
||
country_name = country_stats.get("country", "Null") | ||
total_country_cases = country_stats.get("cases", 0) | ||
total_country_deaths = country_stats.get("deaths", 0) | ||
total_country_recoveries = country_stats.get("recovered", 0) | ||
today_cases = country_stats.get("todayCases", 0) | ||
today_deaths = country_stats.get("todayDeaths", 0) | ||
total_critical = country_stats.get("critical", 0) | ||
cases_per_million = country_stats.get("casesPerOneMillion", 0) | ||
|
||
return CountryStatistics(country_name, | ||
total_country_cases, | ||
total_country_deaths, | ||
total_country_recoveries, | ||
today_cases, today_deaths, | ||
total_critical, | ||
cases_per_million | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
class GlobalStatistics: | ||
def __init__(self, cases, deaths, recoveries, today_cases, today_deaths, total_critical, updated): | ||
self.cases = cases | ||
self.deaths = deaths | ||
self.recoveries = recoveries | ||
self.today_cases = today_cases | ||
self.today_deaths = today_deaths | ||
self.critical = total_critical | ||
self.updated = updated | ||
|
||
|
||
class CountryStatistics: | ||
def __init__(self, name, cases, deaths, recoveries, today_cases, today_deaths, critical, cases_per_million): | ||
self.name = name | ||
self.cases = cases | ||
self.deaths = deaths | ||
self.recoveries = recoveries | ||
self.today_cases = today_cases | ||
self.today_deaths = today_deaths | ||
self.critical = critical | ||
class GlobalStatistics: | ||
def __init__(self, cases, deaths, recoveries, today_cases, today_deaths, total_critical, updated): | ||
self.cases = cases | ||
self.deaths = deaths | ||
self.recoveries = recoveries | ||
self.today_cases = today_cases | ||
self.today_deaths = today_deaths | ||
self.critical = total_critical | ||
self.updated = updated | ||
|
||
|
||
class CountryStatistics: | ||
def __init__(self, name, cases, deaths, recoveries, today_cases, today_deaths, critical, cases_per_million): | ||
self.name = name | ||
self.cases = cases | ||
self.deaths = deaths | ||
self.recoveries = recoveries | ||
self.today_cases = today_cases | ||
self.today_deaths = today_deaths | ||
self.critical = critical | ||
self.cases_per_million = cases_per_million |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
from datetime import datetime | ||
|
||
def format_date(dt): | ||
return dt.strftime('%d %b %Y %H:%M') | ||
|
||
def format_number(number): | ||
from datetime import datetime | ||
|
||
def format_date(dt): | ||
return dt.strftime('%d %b %Y %H:%M') | ||
|
||
def format_number(number): | ||
return '{:,d}'.format(number) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters