From e8cd21306343fa4d1d68d0255d47ea6653455b37 Mon Sep 17 00:00:00 2001 From: ibhuiyan Date: Fri, 3 Apr 2020 18:42:51 -0400 Subject: [PATCH 1/3] Started adding documentation for NYT source --- README.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8fb2e962..865633db 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,8 @@ Currently 2 different data-sources are available to retrieve the data: * **csbs** - https://www.csbs.org/information-covid-19-coronavirus - U.S. County data that comes from the Conference of State Bank Supervisors. +* **nyt** - https://github.com/nytimes/covid-19-data - The New York Times is releasing a series of data files with cumulative counts of coronavirus cases in the United States, at the state and county level, over time. + __jhu__ data-source will be used as a default source if you don't specify a *source parameter* in your request. ## API Reference @@ -71,7 +73,8 @@ __Sample response__ { "sources": [ "jhu", - "csbs" + "csbs", + "nyt" ] } ``` @@ -87,7 +90,7 @@ GET /v2/latest __Query String Parameters__ | __Query string parameter__ | __Description__ | __Type__ | | -------------------------- | -------------------------------------------------------------------------------- | -------- | -| source | The data-source where data will be retrieved from *(jhu/csbs)*. Default is *jhu* | String | +| source | The data-source where data will be retrieved from *(jhu/csbs/nyt)*. Default is *jhu* | String | __Sample response__ ```json @@ -117,7 +120,7 @@ __Path Parameters__ __Query String Parameters__ | __Query string parameter__ | __Description__ | __Type__ | | -------------------------- | -------------------------------------------------------------------------------- | -------- | -| source | The data-source where data will be retrieved from *(jhu/csbs)*. Default is *jhu* | String | +| source | The data-source where data will be retrieved from *(jhu/csbs/nyt)*. Default is *jhu* | String | #### Example Request ```http @@ -160,7 +163,7 @@ GET /v2/locations __Query String Parameters__ | __Query string parameter__ | __Description__ | __Type__ | | -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ | -------- | -| source | The data-source where data will be retrieved from.
__Value__ can be: *jhu/csbs*. __Default__ is *jhu* | String | +| source | The data-source where data will be retrieved from.
__Value__ can be: *jhu/csbs/nyt*. __Default__ is *jhu* | String | | country_code | The ISO ([alpha-2 country_code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)) to the Country/Province for which you're calling the Endpoint | String | | timelines | To set the visibility of timelines (*daily tracking*).
__Value__ can be: *0/1*. __Default__ is *0* (timelines are not visible) | Integer | From 9ffe9e6540eb8f1235b430401654a9e27a34b88c Mon Sep 17 00:00:00 2001 From: ibhuiyan Date: Tue, 7 Apr 2020 19:24:52 -0400 Subject: [PATCH 2/3] Started nyt.py file in services.location module --- app/data/__init__.py | 3 ++- app/enums/sources.py | 1 + app/services/location/nyt.py | 41 ++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 app/services/location/nyt.py diff --git a/app/data/__init__.py b/app/data/__init__.py index aef58e8c..265bf3d3 100644 --- a/app/data/__init__.py +++ b/app/data/__init__.py @@ -1,9 +1,10 @@ """app.data""" from ..services.location.csbs import CSBSLocationService from ..services.location.jhu import JhuLocationService +from ..services.location.nyt import NYTLocationService # Mapping of services to data-sources. -DATA_SOURCES = {"jhu": JhuLocationService(), "csbs": CSBSLocationService()} +DATA_SOURCES = {"jhu": JhuLocationService(), "csbs": CSBSLocationService(), "nyt": NYTLocationService()} def data_source(source): diff --git a/app/enums/sources.py b/app/enums/sources.py index b4538c45..9fc00744 100644 --- a/app/enums/sources.py +++ b/app/enums/sources.py @@ -8,3 +8,4 @@ class Sources(str, Enum): jhu = "jhu" csbs = "csbs" + nyt = "nyt" diff --git a/app/services/location/nyt.py b/app/services/location/nyt.py new file mode 100644 index 00000000..42c2ea83 --- /dev/null +++ b/app/services/location/nyt.py @@ -0,0 +1,41 @@ +"""app.services.location.nyt.py""" +import csv +from datetime import datetime + +from asyncache import cached +from cachetools import TTLCache + +from . import LocationService + + +class NYTLocationService(LocationService): + """ + Service for retrieving locations from New York Times (https://github.com/nytimes/covid-19-data). + """ + + async def get_all(self): + # Get the locations. + locations = await get_locations() + return locations + + async def get(self, loc_id): # pylint: disable=arguments-differ + # Get location at the index equal to provided id. + locations = await self.get_all() + return locations[loc_id] + + +# --------------------------------------------------------------- + + +# Base URL for fetching category. +BASE_URL = "https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv" + + +@cached(cache=TTLCache(maxsize=1024, ttl=3600)) # TODO +async def get_category(category): + pass + + +@cached(cache=TTLCache(maxsize=1024, ttl=3600)) +async def get_locations(): + pass From 4903abb5b7c87d8479ca78c97eaa0d3c6040208e Mon Sep 17 00:00:00 2001 From: nischalshankar Date: Wed, 8 Apr 2020 15:54:40 -0400 Subject: [PATCH 3/3] Testing nischal pushing to branch --- app/data/__init__.py | 1 + app/services/location/nyt.py | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/app/data/__init__.py b/app/data/__init__.py index 265bf3d3..9e0f34c1 100644 --- a/app/data/__init__.py +++ b/app/data/__init__.py @@ -5,6 +5,7 @@ # Mapping of services to data-sources. DATA_SOURCES = {"jhu": JhuLocationService(), "csbs": CSBSLocationService(), "nyt": NYTLocationService()} +# DATA_SOURCES = {"jhu": JhuLocationService(), "csbs": CSBSLocationService()} def data_source(source): diff --git a/app/services/location/nyt.py b/app/services/location/nyt.py index 42c2ea83..0f32f46e 100644 --- a/app/services/location/nyt.py +++ b/app/services/location/nyt.py @@ -5,6 +5,7 @@ from asyncache import cached from cachetools import TTLCache +from ...utils import httputils from . import LocationService @@ -38,4 +39,16 @@ async def get_category(category): @cached(cache=TTLCache(maxsize=1024, ttl=3600)) async def get_locations(): + """ + Retrieves the locations from the categories. The locations are cached for 1 hour. + + :returns: The locations. + :rtype: List[Location] + """ pass + ''' + async with httputils.CLIENT_SESSION.get(BASE_URL) as response: + text = await response.text() + + data = list(csv.DictReader(text.splitlines())) + '''