Skip to content

Commit

Permalink
country list and better errors
Browse files Browse the repository at this point in the history
  • Loading branch information
apex2504 committed Apr 20, 2020
1 parent 1244075 commit 9057c2a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 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.7.4"
__version__ = "0.7.5"
__author__ = "Rob Wainwright // apex2504"
__license__ = "MIT"
44 changes: 40 additions & 4 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 BadSortParameter, BadYesterdayParameter
from .exceptions import NotFound, BadSortParameter, BadYesterdayParameter
from .endpoints import *


Expand Down Expand Up @@ -59,6 +59,7 @@ def _compile_country_data(self, country_stats):
cases_per_million = country_stats.get("casesPerOneMillion", 0)
deaths_per_million = country_stats.get("deathsPerOneMillion", 0)
tests_per_million = country_stats.get("testsPerOneMillion", 0)
continent = country_stats.get("continent", None)
updated_epoch = country_stats.get("updated", 0)
updated = datetime.utcfromtimestamp(updated_epoch/1000.0)

Expand All @@ -80,6 +81,7 @@ def _compile_country_data(self, country_stats):
cases_per_million,
deaths_per_million,
tests_per_million,
continent,
updated
)

Expand Down Expand Up @@ -295,6 +297,34 @@ async def get_country_data(self, country, **kwargs):

return self._compile_country_data(country_stats)


async def get_country_list(self, *countries, **kwargs):
"""
Get the data for more than one country, but not necessarily all of them.
"""
get_yesterday = kwargs.get('yesterday')
country_list = ','.join(map(str, countries))

if get_yesterday:
self._check_yesterday(get_yesterday)
endpoint = COUNTRY_DATA_YESTERDAY.format(self.api_url, country_list)

else:
endpoint = COUNTRY_DATA.format(self.api_url, country_list)

data = await self.request_client.make_request(endpoint)

if isinstance(data, dict):
return self._compile_country_data(data)

returned_countries = []

for country in data:
returned_countries.append(
self._compile_country_data(country)
)
return returned_countries


async def get_all_countries(self, **kwargs):
"""
Expand Down Expand Up @@ -412,9 +442,12 @@ async def get_state_county_history(self, state, county, last_days='all'):
"""
endpoint = STATE_COUNTY.format(self.api_url, state, last_days)
data = await self.request_client.make_request(endpoint)

matching_county = next(place for place in data if place["province"].lower() == state.lower() \

try:
matching_county = next(place for place in data if place["province"].lower() == state.lower() \
and place["county"].lower() == county.lower())
except StopIteration:
raise NotFound('Nothing found for specified county.')

return self._generate_history(matching_county, True)

Expand Down Expand Up @@ -464,8 +497,11 @@ async def get_jhu_county_data(self, state, county):
endpoint = JHU_CSSE_COUNTIES.format(self.api_url, county)
all_matching_counties = await self.request_client.make_request(endpoint)

matching_county = next(place for place in all_matching_counties if place["province"].lower() == state.lower() \
try:
matching_county = next(place for place in all_matching_counties if place["province"].lower() == state.lower() \
and place["county"].lower() == county.lower())
except StopIteration:
raise NotFound('Nothing found for specified county.')

country = matching_county.get("country") #will always be 'US'
province = matching_county.get("province")
Expand Down
3 changes: 2 additions & 1 deletion corona_api/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(self, _id, iso2, iso3, _lat, _long, flag):

class CountryStatistics:
def __init__(self, info, name, cases, deaths, recoveries, today_cases, today_deaths, critical, active,
tests, cases_per_million, deaths_per_million, tests_per_million, updated):
tests, cases_per_million, deaths_per_million, tests_per_million, continent, updated):
self.info = info
self.name = name
self.cases = cases
Expand All @@ -42,6 +42,7 @@ def __init__(self, info, name, cases, deaths, recoveries, today_cases, today_dea
self.cases_per_million = cases_per_million
self.deaths_per_million = deaths_per_million
self.tests_per_million = tests_per_million
self.continent = continent
self.updated = updated


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
setup(
name = 'corona_api',
packages = ['corona_api'],
version = '0.7.4',
version = '0.7.5',
license='MIT',
description = 'An asynchronous wrapper for the corona.lmao.ninja API written in Python.',
long_description= long_desc,
Expand Down

0 comments on commit 9057c2a

Please sign in to comment.