diff --git a/app/location/__init__.py b/app/location/__init__.py index 1da5e9e5..0676fd0d 100644 --- a/app/location/__init__.py +++ b/app/location/__init__.py @@ -3,6 +3,27 @@ from ..utils import countries from ..utils.populations import country_population +# put confirmed cases, deaths cases, recovered cases into one class +# Inseated of using confirmed cases, deaths cases, recovered cases as attributes, we can use CaseNumbers class instance as attribute +class CaseNumbers: + def __init__(self, id, confirmed = 0, deaths = 0, recovered = 0): + self.id = id + self.confirmed = confirmed + self.deaths = deaths + self.recovered = recovered + + +#put all location information into one class +#CaseNumbers, Locationinfo, coordinates and Location forms one aggregate +class Locationinfo: + def __init__(self, id, country, province, coordinates): + self.id = id + self.country = country.strip() + self.province = province.strip() + self.coordinates = coordinates + + + # pylint: disable=redefined-builtin,invalid-name class Location: # pylint: disable=too-many-instance-attributes @@ -10,22 +31,18 @@ class Location: # pylint: disable=too-many-instance-attributes A location in the world affected by the coronavirus. """ + # Use instance of class CaseNumbers as attribute def __init__( - self, id, country, province, coordinates, last_updated, confirmed, deaths, recovered, + self, id, locationinfo, last_updated, casenumbers ): # pylint: disable=too-many-arguments # General info. - self.id = id - self.country = country.strip() - self.province = province.strip() - self.coordinates = coordinates + self.locationinfo = locationinfo # Last update. self.last_updated = last_updated # Statistics. - self.confirmed = confirmed - self.deaths = deaths - self.recovered = recovered + self.casenumbers = casenumbers @property def country_code(self): @@ -35,7 +52,7 @@ def country_code(self): :returns: The country code. :rtype: str """ - return (countries.country_code(self.country) or countries.DEFAULT_COUNTRY_CODE).upper() + return (countries.country_code(self.locationinfo.country) or countries.DEFAULT_COUNTRY_CODE).upper() @property def country_population(self): @@ -56,20 +73,20 @@ def serialize(self): """ return { # General info. - "id": self.id, - "country": self.country, + "id": self.locationinfo.id, + "country": self.locationinfo.country, "country_code": self.country_code, "country_population": self.country_population, - "province": self.province, + "province": self.locationinfo.province, # Coordinates. - "coordinates": self.coordinates.serialize(), + "coordinates": self.locationinfo.coordinates.serialize(), # Last updated. "last_updated": self.last_updated, # Latest data (statistics). "latest": { - "confirmed": self.confirmed, - "deaths": self.deaths, - "recovered": self.recovered, + "confirmed": self.casenumbers.confirmed, + "deaths": self.casenumbers.deaths, + "recovered": self.casenumbers.recovered, }, } @@ -80,20 +97,21 @@ class TimelinedLocation(Location): """ # pylint: disable=too-many-arguments - def __init__(self, id, country, province, coordinates, last_updated, timelines): + def __init__(self, locationinfo, last_updated, timelines, casenumbers): super().__init__( # General info. - id, - country, - province, - coordinates, + locationinfo, last_updated, # Statistics (retrieve latest from timelines). - confirmed=timelines.get("confirmed").latest or 0, - deaths=timelines.get("deaths").latest or 0, - recovered=timelines.get("recovered").latest or 0, + casenumbers + #confirmed=timelines.get("confirmed").latest or 0, + #deaths=timelines.get("deaths").latest or 0, + #recovered=timelines.get("recovered").latest or 0, ) - + #set case numbers + self.casenumbers.confirmed=timelines.get("confirmed").latest or 0 + self.casenumbers.deaths=timelines.get("deaths").latest or 0 + self.casenumbers.recovered=timelines.get("recovered").latest or 0 # Set timelines. self.timelines = timelines diff --git a/app/services/location/csbs.py b/app/services/location/csbs.py index 444ebad6..b35a45b5 100644 --- a/app/services/location/csbs.py +++ b/app/services/location/csbs.py @@ -12,6 +12,8 @@ from ...utils import httputils from . import LocationService +from urls import URLs + LOGGER = logging.getLogger("services.location.csbs") @@ -32,7 +34,7 @@ async def get(self, loc_id): # pylint: disable=arguments-differ # Base URL for fetching data -BASE_URL = "https://facts.csbs.org/covid-19/covid19_county.csv" +#BASE_URL = "https://facts.csbs.org/covid-19/covid19_county.csv" @cached(cache=TTLCache(maxsize=1, ttl=1800)) @@ -52,7 +54,7 @@ async def get_locations(): locations = cache_results else: LOGGER.info(f"{data_id} shared cache empty") - async with httputils.CLIENT_SESSION.get(BASE_URL) as response: + async with httputils.CLIENT_SESSION.get(URLs.CSBS) as response: text = await response.text() LOGGER.debug(f"{data_id} Data received") diff --git a/app/services/location/jhu.py b/app/services/location/jhu.py index ebed3960..b0c0c480 100644 --- a/app/services/location/jhu.py +++ b/app/services/location/jhu.py @@ -17,6 +17,8 @@ from ...utils import httputils from . import LocationService +from urls import URLs + LOGGER = logging.getLogger("services.location.jhu") PID = os.getpid() @@ -41,7 +43,7 @@ async def get(self, loc_id): # pylint: disable=arguments-differ # Base URL for fetching category. -BASE_URL = "https://raw.githubusercontent.com/CSSEGISandData/2019-nCoV/master/csse_covid_19_data/csse_covid_19_time_series/" +# BASE_URL = "https://raw.githubusercontent.com/CSSEGISandData/2019-nCoV/master/csse_covid_19_data/csse_covid_19_time_series/" @cached(cache=TTLCache(maxsize=4, ttl=1800)) @@ -64,7 +66,7 @@ async def get_category(category): else: LOGGER.info(f"{data_id} shared cache empty") # URL to request data from. - url = BASE_URL + "time_series_covid19_%s_global.csv" % category + url = URLs.JHU + "time_series_covid19_%s_global.csv" % category # Request the data LOGGER.info(f"{data_id} Requesting data...") diff --git a/app/services/location/nyt.py b/app/services/location/nyt.py index 1f25ec34..fc3d78e2 100644 --- a/app/services/location/nyt.py +++ b/app/services/location/nyt.py @@ -13,6 +13,8 @@ from ...utils import httputils from . import LocationService +from urls import URLs + LOGGER = logging.getLogger("services.location.nyt") @@ -36,7 +38,7 @@ async def get(self, loc_id): # pylint: disable=arguments-differ # Base URL for fetching category. -BASE_URL = "https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv" +# BASE_URL = "https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv" def get_grouped_locations_dict(data): @@ -85,7 +87,7 @@ async def get_locations(): locations = cache_results else: LOGGER.info(f"{data_id} shared cache empty") - async with httputils.CLIENT_SESSION.get(BASE_URL) as response: + async with httputils.CLIENT_SESSION.get(URLs.NYT) as response: text = await response.text() LOGGER.debug(f"{data_id} Data received") diff --git a/app/services/location/urls.py b/app/services/location/urls.py new file mode 100644 index 00000000..5a803f44 --- /dev/null +++ b/app/services/location/urls.py @@ -0,0 +1,8 @@ +import enum + +# Encapsulate urls into one class, clas URLs serve as aggregate root. +class URLs(str, enum.Enum): + + JHU = "https://raw.githubusercontent.com/CSSEGISandData/2019-nCoV/master/csse_covid_19_data/csse_covid_19_time_series/" + CSBS = "https://facts.csbs.org/covid-19/covid19_county.csv" + NYT = "https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv" \ No newline at end of file