Skip to content

Commit

Permalink
add vaccine tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
apex2504 committed Jan 19, 2021
1 parent 39d9d39 commit ad0f1c7
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 11 deletions.
56 changes: 55 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -471,5 +471,59 @@ async def get_country_gov():
asyncio.get_event_loop().run_until_complete(get_country_gov())
```

### Get the vaccine data globally
```python
import diseaseapi
import asyncio

client = diseaseapi.Client().covid19

async def vax_cov():
data = await client.vaccine_coverage(30)

print(data[0].date.day, data[0].value) #global vaccine data 30 days ago

await client.request_client.close() #close the ClientSession

asyncio.get_event_loop().run_until_complete(vax_cov())
```

### Get the vaccine data for all countries
```python
import diseaseapi
import asyncio

client = diseaseapi.Client().covid19

async def vax_all():
data = await client.vaccine_countries(30)

d = data[0]

print(d.country, d.timeline[0].date, d.timeline[0].value)

await client.request_client.close() #close the ClientSession

asyncio.get_event_loop().run_until_complete(vax_all())
```

### Get the vaccine data for a specific country
```python
import diseaseapi
import asyncio

client = diseaseapi.Client().covid19

async def vax_ctry():
data = await client.vaccine_country('UK', 30)

print(d.country, d.timeline[0].date, d.timeline[0].value)

await client.request_client.close() #close the ClientSession

asyncio.get_event_loop().run_until_complete(vac_ctry())
```


# Note
Due to the fact that each country's governmental/official statistics website is different (layouts, tables etc.), it is not feasible to create a standardised class for the data. However, the data resurned will be in standard JSON format so it should be relatively simple to work with.
Due to the fact that each country's governmental/official statistics website is different (layouts, tables etc.), it is not feasible to create a standardised class for the data. However, the data returned will be in standard JSON format so it should be relatively simple to work with.
2 changes: 1 addition & 1 deletion diseaseapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
from .utils import *
from .exceptions import *

__version__ = "1.1.0"
__version__ = "1.2.0"
__author__ = "Rob Wainwright // apex2504"
__license__ = "MIT"
52 changes: 50 additions & 2 deletions diseaseapi/covid.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime, timezone
from typing import Union, List, Dict
from typing import Union, List, Dict, Tuple
from .covidstatistics import *
from .exceptions import NotFound, BadSortParameter, BadYesterdayParameter, BadTwoDaysAgoParameter, BadAllowNoneParameter
from .covidendpoints import *
Expand Down Expand Up @@ -346,6 +346,17 @@ def _compile_vaccines(self, data):
)


def _compile_vax_tl(self, data):
return [VaccineTimeline(datetime.strptime(date, '%m/%d/%y'), data[date]) for date in data]


def _compile_vax_country(self, data):
return VaccineCountry(data['country'], self._compile_vax_tl(data['timeline']))


######################################################################################


async def all(self, **kwargs) -> Global:
"""
Get the global stats for Coronavirus COVID-19
Expand Down Expand Up @@ -849,4 +860,41 @@ async def vaccine(self) -> Vaccines:
endpoint = VACCINE.format(self.api_url)
data = await self.request_client.make_request(endpoint)

return self._compile_vaccines(data)
return self._compile_vaccines(data)


async def vaccine_coverage(self, last_days='all') -> List[VaccineTimeline]:
"""
Get global vaccine coverage data.
"""
endpoint = COVERAGE_ALL.format(self.api_url)
params = {'lastdays': last_days}
data = await self.request_client.make_request(endpoint, params=params)

return self._compile_vax_tl(data)


async def vaccine_countries(self, last_days='all') -> List[VaccineCountry]:
"""
Get vaccination data for all countries.
"""
endpoint = COVERAGE_COUNTRIES.format(self.api_url)
params = {'lastdays': last_days}
data = await self.request_client.client.make_request(endpoint, params=params)

return [self._compile_vax_country(country) for country in data]


async def vaccine_country(self, country, last_days='all') -> VaccineCountry:
"""
Get vaccination data for a specific country.
"""
endpoint = COVERAGE_COUNTRY.format(self.api_url, country)
params = {'lastdays': last_days}
data = self.request_client.make_request(endpoint, params=params)

return self._compile_vax_country(data)


async def therapeutics(self):
raise NotImplementedError
5 changes: 4 additions & 1 deletion diseaseapi/covidendpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,7 @@
GOV_ALL = '{}' + _COVID_BASE + '/gov'
GOV_COUNTRY = GOV_ALL + '/{}'

VACCINE = '{}' + _COVID_BASE + '/vaccine'
VACCINE = '{}' + _COVID_BASE + '/vaccine'
COVERAGE_ALL = VACCINE + '/coverage'
COVERAGE_COUNTRIES = COVERAGE_ALL + '/countries'
COVERAGE_COUNTRY = COVERAGE_COUNTRIES + '/{}'
14 changes: 13 additions & 1 deletion diseaseapi/covidstatistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,16 @@ def __init__(self, candidate, sponsors, details, phase, institutions, funding):
class Vaccines:
def __init__(self, source, vaccines):
self.source = source
self.vaccines = vaccines
self.vaccines = vaccines


class VaccineTimeline:
def __init__(self, date, value):
self.date = date
self.value = value


class VaccineCountry:
def __init__(self, country, timeline):
self.country = country
self.timeline = timeline
6 changes: 3 additions & 3 deletions diseaseapi/request.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import aiohttp
from .exceptions import NotFound, APIError

ver = '1.0.2'
ver = '1.2.0'

class RequestClient:
def __init__(self):
Expand All @@ -15,9 +15,9 @@ async def make_request(self, endpoint, params=None):
raise NotFound('No data available for specified country, state or province.')
elif resp.status != 200:
raise APIError('An unexpected error occurred.')

return await resp.json()


async def close(self):
await self.session.close()
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup

ver = '1.1.0'
ver = '1.2.0'

with open('README.md', 'r') as f:
long_desc = f.read()
Expand All @@ -14,7 +14,7 @@
long_description= long_desc,
long_description_content_type = 'text/markdown',
author = 'Rob Wainwright',
author_email = 'wainwrightbobby@gmail.com',
author_email = 'apex@taka.moe',
url = 'https://github.com/apex2504/disease.py',
keywords = ['coronavirus', 'covid-19'],
install_requires=[
Expand Down

0 comments on commit ad0f1c7

Please sign in to comment.