Skip to content

Commit

Permalink
new two_days_ago and other shit idk
Browse files Browse the repository at this point in the history
  • Loading branch information
apex2504 committed Jun 2, 2020
1 parent 5212c9c commit d343f8f
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 29 deletions.
2 changes: 1 addition & 1 deletion corona_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
from .statistics import *
from .utils import *

__version__ = "0.8.8"
__version__ = "0.9.0"
__author__ = "Rob Wainwright // apex2504"
__license__ = "MIT"
91 changes: 80 additions & 11 deletions corona_api/client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime, timezone
from .request import RequestClient
from .statistics import *
from .exceptions import NotFound, BadSortParameter, BadYesterdayParameter, BadAllowNoneParameter
from .exceptions import NotFound, BadSortParameter, BadYesterdayParameter, BadTwoDaysAgoParameter, BadAllowNoneParameter
from .endpoints import *


Expand All @@ -23,12 +23,16 @@ def _check_sort(self, sort):

def _check_yesterday(self, value):
if not isinstance(value, bool):
raise BadYesterdayParameter('Value for yesterday should either be True or False')
raise BadYesterdayParameter('Value for yesterday should either be True or False.')

def _check_two_days_ago(self, value):
if not isinstance(value, bool):
raise BadTwoDaysAgoParameter('Value for two_days_ago should either be True or False.')


def _check_allow_none(self, value):
if not isinstance(value, bool):
raise BadAllowNoneParameter('Value for allow_none should either be True or False')
raise BadAllowNoneParameter('Value for allow_none should either be True or False.')


def _compile_countryInfo(self, countryInfo):
Expand Down Expand Up @@ -58,6 +62,7 @@ def _compile_country_data(self, country_stats):
total_country_recoveries = country_stats.get("recovered", 0)
today_cases = country_stats.get("todayCases", 0)
today_deaths = country_stats.get("todayDeaths", 0)
today_recoveries = country_stats.get("todayRecovered", 0)
total_critical = country_stats.get("critical", 0)
active = country_stats.get("active", 0)
tests = country_stats.get("tests", 0)
Expand All @@ -67,6 +72,9 @@ def _compile_country_data(self, country_stats):
recoveries_per_million = country_stats.get("recoveredPerOneMillion", 0)
critical_per_million = country_stats.get("criticalPerOneMillion", 0)
active_per_million = country_stats.get("activePerOneMillion", 0)
one_case_per_people = country_stats.get("oneCasePerPeople", 0)
one_death_per_people = country_stats.get("oneDeathPerPeople", 0)
one_test_per_people = country_stats.get("oneTestPerPeople", 0)
continent = country_stats.get("continent")
population = country_stats.get("population", 0)
updated_epoch = country_stats.get("updated", 0)
Expand All @@ -84,6 +92,7 @@ def _compile_country_data(self, country_stats):
total_country_recoveries,
today_cases,
today_deaths,
today_recoveries,
total_critical,
active,
tests,
Expand All @@ -93,6 +102,9 @@ def _compile_country_data(self, country_stats):
recoveries_per_million,
critical_per_million,
active_per_million,
one_case_per_people,
one_death_per_people,
one_test_per_people,
continent,
population,
updated
Expand Down Expand Up @@ -198,6 +210,7 @@ def _compile_continent(self, data):
recoveries = data.get("recovered", 0)
today_cases = data.get("todayCases", 0)
today_deaths = data.get("todayDeaths", 0)
today_recoveries = data.get("todayRecovered", 0)
critical = data.get("critical", 0)
updated_epoch = data.get("updated", 0)
active = data.get("active", cases-deaths-recoveries)
Expand All @@ -222,6 +235,7 @@ def _compile_continent(self, data):
tests,
today_cases,
today_deaths,
today_recoveries,
cases_per_million,
deaths_per_million,
tests_per_million,
Expand Down Expand Up @@ -319,19 +333,27 @@ async def all(self, **kwargs):
Get the global stats for Coronavirus COVID-19
"""
yesterday = kwargs.get('yesterday', False)
two_days_ago = kwargs.get('two_days_ago', False)
allow_none = kwargs.get('allow_none', False)

endpoint = GLOBAL_DATA.format(self.api_url)

if yesterday:
self._check_yesterday(yesterday)

if two_days_ago:
self._check_two_days_ago(two_days_ago)

if yesterday and two_days_ago:
raise ValueError('yesterday and two_days_ago cannot both be True.')

if allow_none:
self._check_allow_none(allow_none)

yesterday = str(yesterday).lower()
two_days_ago = str(two_days_ago).lower()
allow_none = str(allow_none).lower()
params = {"yesterday": yesterday, "allowNull": allow_none}
params = {"yesterday": yesterday, "twoDaysAgo": two_days_ago, "allowNull": allow_none}

global_data = await self.request_client.make_request(endpoint, params)

Expand All @@ -340,6 +362,7 @@ async def all(self, **kwargs):
recoveries = global_data.get("recovered", 0)
today_cases = global_data.get("todayCases", 0)
today_deaths = global_data.get("todayDeaths", 0)
today_recoveries = global_data.get("todayRecovered", 0)
total_critical = global_data.get("critical", 0)
updated_epoch = global_data.get("updated", 0)
active = global_data.get("active", 0)
Expand All @@ -350,6 +373,9 @@ async def all(self, **kwargs):
active_per_million = global_data.get("activePerOneMillion", 0)
recoveries_per_million = global_data.get("recoveredPerOneMillion", 0)
critical_per_million = global_data.get("criticalPerOneMillion", 0)
one_case_per_people = global_data.get("oneCasePerPeople", 0)
one_death_per_people = global_data.get("oneDeathPerPeople", 0)
one_test_per_people = global_data.get("oneTestPerPeople", 0)
population = global_data.get("population", 0)
infected_countries = global_data.get("affectedCountries")
updated = datetime.utcfromtimestamp(updated_epoch/1000.0)
Expand All @@ -360,6 +386,7 @@ async def all(self, **kwargs):
recoveries,
today_cases,
today_deaths,
today_recoveries,
total_critical,
active,
tests,
Expand All @@ -369,6 +396,9 @@ async def all(self, **kwargs):
active_per_million,
recoveries_per_million,
critical_per_million,
one_case_per_people,
one_death_per_people,
one_test_per_people,
population,
infected_countries,
updated,
Expand All @@ -380,19 +410,27 @@ async def get_country_data(self, country, **kwargs):
Get the data for a specific country.
"""
yesterday = kwargs.get('yesterday', False)
two_days_ago = kwargs.get('two_days_ago', False)
allow_none = kwargs.get('allow_none', False)

endpoint = COUNTRY_DATA.format(self.api_url, country)

if yesterday:
self._check_yesterday(yesterday)

if two_days_ago:
self._check_two_days_ago(two_days_ago)

if yesterday and two_days_ago:
raise ValueError('yesterday and two_days_ago cannot both be True.')

if allow_none:
self._check_allow_none(allow_none)

yesterday = str(yesterday).lower()
two_days_ago = str(two_days_ago).lower()
allow_none = str(allow_none).lower()
params = {"yesterday": yesterday, "allowNull": allow_none}
params = {"yesterday": yesterday, "twoDaysAgo": two_days_ago, "allowNull": allow_none}

country_stats = await self.request_client.make_request(endpoint, params)

Expand All @@ -404,6 +442,7 @@ async def get_country_list(self, *countries, **kwargs):
Get the data for more than one country, but not necessarily all of them.
"""
yesterday = kwargs.get('yesterday', False)
two_days_ago = kwargs.get('two_days_ago', False)
allow_none = kwargs.get('allow_none', False)
country_list = ','.join(map(str, countries))

Expand All @@ -412,12 +451,18 @@ async def get_country_list(self, *countries, **kwargs):
if yesterday:
self._check_yesterday(yesterday)

if two_days_ago:
self._check_two_days_ago(two_days_ago)

if yesterday and two_days_ago:
raise ValueError('yesterday and two_days_ago cannot both be True.')

if allow_none:
self._check_allow_none(allow_none)

yesterday = str(yesterday).lower()
allow_none = str(allow_none).lower()
params = {"yesterday": yesterday, "allowNull": allow_none}
params = {"yesterday": yesterday, "twoDaysAgo": two_days_ago, "allowNull": allow_none}

data = await self.request_client.make_request(endpoint, params)

Expand All @@ -438,6 +483,7 @@ async def get_all_countries(self, **kwargs):
Get the data for every infected country.
"""
yesterday = kwargs.get('yesterday', False)
two_days_ago = kwargs.get('two_days_ago', False)
allow_none = kwargs.get('allow_none', False)
sort = kwargs.get('sort', None)

Expand All @@ -447,18 +493,25 @@ async def get_all_countries(self, **kwargs):
if yesterday:
self._check_yesterday(yesterday)

if two_days_ago:
self._check_two_days_ago(two_days_ago)

if yesterday and two_days_ago:
raise ValueError('yesterday and two_days_ago cannot both be True.')

if allow_none:
self._check_allow_none(allow_none)

yesterday = str(yesterday).lower()
two_days_ago = str(two_days_ago).lower()
allow_none = str(allow_none).lower()

if sort:
self._check_sort(sort)
params = {"yesterday": yesterday, "allowNull": allow_none, "sort": sort}
params = {"yesterday": yesterday, "twoDaysAgo": two_days_ago, "allowNull": allow_none, "sort": sort}

else:
params = {"yesterday": yesterday, "allowNull": allow_none}
params = {"yesterday": yesterday, "twoDaysAgo": two_days_ago, "allowNull": allow_none}

all_countries = await self.request_client.make_request(endpoint, params)

Expand Down Expand Up @@ -665,27 +718,35 @@ async def get_all_continents(self, **kwargs):
Get the statistics for world continents.
"""
yesterday = kwargs.get('yesterday', False)
sort = kwargs.get('sort', None)
two_days_ago = kwargs.get('two_days_ago', False)
allow_none = kwargs.get('allow_none', False)
sort = kwargs.get('sort', None)

endpoint = ALL_CONTINENTS.format(self.api_url)
params = None

if yesterday:
self._check_yesterday(yesterday)

if two_days_ago:
self._check_two_days_ago(two_days_ago)

if yesterday and two_days_ago:
raise ValueError('yesterday and two_days_ago cannot both be True.')

if allow_none:
self._check_allow_none(allow_none)

yesterday = str(yesterday).lower()
two_days_ago = str(two_days_ago).lower()
allow_none = str(allow_none).lower()

if sort:
self._check_sort(sort)
params = {"yesterday": yesterday, "allowNull": allow_none, "sort": sort}
params = {"yesterday": yesterday, "twoDaysAgo": two_days_ago, "allowNull": allow_none, "sort": sort}

else:
params = {"yesterday": yesterday, "allowNull": allow_none}
params = {"yesterday": yesterday,"twoDaysAgo": two_days_ago, "allowNull": allow_none}

data = await self.request_client.make_request(endpoint, params)

Expand All @@ -702,6 +763,7 @@ async def get_single_continent(self, continent, **kwargs):
Get the statistics for a single continent.
"""
yesterday = kwargs.get('yesterday', False)
two_days_ago = kwargs.get('two_days_ago', False)
allow_none = kwargs.get('allow_none', False)

endpoint = CONTINENT_DATA.format(self.api_url)
Expand All @@ -710,7 +772,14 @@ async def get_single_continent(self, continent, **kwargs):
if yesterday:
self._check_yesterday(yesterday)

if two_days_ago:
self._check_two_days_ago(two_days_ago)

if yesterday and two_days_ago:
raise ValueError('yesterday and two_days_ago cannot both be True.')

yesterday = str(yesterday).lower()
two_days_ago = str(two_days_ago).lower()
allow_none = str(allow_none).lower()
params = {"yesterday": yesterday, "allowNull": allow_none}

Expand Down
3 changes: 3 additions & 0 deletions corona_api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ class BadSortParameter(Exception):
class BadYesterdayParameter(Exception):
pass

class BadTwoDaysAgoParameter(Exception):
pass

class BadAllowNoneParameter(Exception):
pass
2 changes: 1 addition & 1 deletion corona_api/request.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import aiohttp
from .exceptions import NotFound, APIError

ver = "0.8.8"
ver = '0.9.0'

class RequestClient:
def __init__(self):
Expand Down

0 comments on commit d343f8f

Please sign in to comment.