From 5bfc936738c6a125a696180c4d86a915167ac919 Mon Sep 17 00:00:00 2001 From: HuangHuangHuangShiqing Date: Tue, 16 Mar 2021 04:50:33 +0800 Subject: [PATCH 1/2] aggregate --- COVID19Py/CaseByCountry.py | 37 +++++++++++++++++++++++++++++++++++++ COVID19Py/covid19.py | 38 +------------------------------------- 2 files changed, 38 insertions(+), 37 deletions(-) create mode 100644 COVID19Py/CaseByCountry.py diff --git a/COVID19Py/CaseByCountry.py b/COVID19Py/CaseByCountry.py new file mode 100644 index 0000000..1d14636 --- /dev/null +++ b/COVID19Py/CaseByCountry.py @@ -0,0 +1,37 @@ +from typing import Dict, List +#import COVID19Py +class CaseByCountry: + def __init__(self,Covid19Object=COVID19Py.COVID19()): + self.obj = Covid19Object + def getLocationByCountryCode(self, countryCode, timelines=False) -> List[Dict]: + """ + :param country_code: String denoting the ISO 3166-1 alpha-2 code (https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) of the country + :param timelines: Whether timeline information should be returned as well. + :return: A list of areas that correspond to the country_code. If the country_code is invalid, it returns an empty list. + """ + data = None + if timelines: + data = self.obj._request("/v2/locations", {"country_code": country_code, "timelines": str(timelines).lower()}) + else: + data = self.obj._request("/v2/locations", {"country_code": country_code}) + return data["locations"] + def getLocationByCountryName(self, countryName, timelines=False) -> List[Dict]: + """ + :param country: String denoting name of the country + :param timelines: Whether timeline information should be returned as well. + :return: A list of areas that correspond to the country name. If the country is invalid, it returns an empty list. + """ + data = None + if timelines: + data = self.obj._request("/v2/locations", {"country": country, "timelines": str(timelines).lower()}) + else: + data = self.obj._request("/v2/locations", {"country": country}) + return data["locations"] + + def getLocationById(self, countryId: int): + """ + :param country_id: Country Id, an int + :return: A dictionary with case information for the specified location. + """ + data = self.obj._request("/v2/locations/" + str(country_id)) + return data["location"] diff --git a/COVID19Py/covid19.py b/COVID19Py/covid19.py index a68faa7..cb1ecae 100644 --- a/COVID19Py/covid19.py +++ b/COVID19Py/covid19.py @@ -32,12 +32,10 @@ def __init__(self, url="https://covid-tracker-us.herokuapp.com", data_source='jh # URL did not work, reset it and move on self.url = "" continue - # TODO: Should have a better health-check, this is way too hacky... if "jhu" in result: # We found a mirror that worked just fine, let's stick with it break - # None of the mirrors worked. Raise an error to inform the user. raise RuntimeError("No available API mirror was found.") @@ -67,7 +65,7 @@ def _getSources(self): def _request(self, endpoint, params=None): if params is None: params = {} - response = requests.get(self.url + endpoint, {**params, "source":self.data_source}) + response = requests.get(self.url + endpoint, {*params, "source":self.data_source}) #syntax error fixed response.raise_for_status() return response.json() @@ -122,37 +120,3 @@ def getLocations(self, timelines=False, rank_by: str = None) -> List[Dict]: data = ranked return data - - def getLocationByCountryCode(self, country_code, timelines=False) -> List[Dict]: - """ - :param country_code: String denoting the ISO 3166-1 alpha-2 code (https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) of the country - :param timelines: Whether timeline information should be returned as well. - :return: A list of areas that correspond to the country_code. If the country_code is invalid, it returns an empty list. - """ - data = None - if timelines: - data = self._request("/v2/locations", {"country_code": country_code, "timelines": str(timelines).lower()}) - else: - data = self._request("/v2/locations", {"country_code": country_code}) - return data["locations"] - - def getLocationByCountry(self, country, timelines=False) -> List[Dict]: - """ - :param country: String denoting name of the country - :param timelines: Whether timeline information should be returned as well. - :return: A list of areas that correspond to the country name. If the country is invalid, it returns an empty list. - """ - data = None - if timelines: - data = self._request("/v2/locations", {"country": country, "timelines": str(timelines).lower()}) - else: - data = self._request("/v2/locations", {"country": country}) - return data["locations"] - - def getLocationById(self, country_id: int): - """ - :param country_id: Country Id, an int - :return: A dictionary with case information for the specified location. - """ - data = self._request("/v2/locations/" + str(country_id)) - return data["location"] From 0fa4508b1b2f41a58938f27646b43b489baf6274 Mon Sep 17 00:00:00 2001 From: Shiqing Huang Date: Tue, 16 Mar 2021 05:38:02 +0800 Subject: [PATCH 2/2] aggregate --- COVID19Py/OverViewOfCovid19.py | 60 ++++++++++++++++++++++++++++++++++ COVID19Py/covid19.py | 54 ++---------------------------- 2 files changed, 63 insertions(+), 51 deletions(-) create mode 100644 COVID19Py/OverViewOfCovid19.py diff --git a/COVID19Py/OverViewOfCovid19.py b/COVID19Py/OverViewOfCovid19.py new file mode 100644 index 0000000..9075b65 --- /dev/null +++ b/COVID19Py/OverViewOfCovid19.py @@ -0,0 +1,60 @@ +from typing import Dict, List +import COVID19Py +#aggregate OverViewOfCovid19 +class OverViewOfCovid19: + def __init__(self,Covid19Object): + self.obj = Covid19Object + + def getAll(self, timelines=False): + self.obj._update(timelines) + return self.obj.latestData + + def getLatestChanges(self): + changes = None + if self.obj.previousData: + changes = { + "confirmed": self.obj.latestData["latest"]["confirmed"] - self.obj.latestData["latest"]["confirmed"], + "deaths": self.obj.latestData["latest"]["deaths"] - self.obj.latestData["latest"]["deaths"], + "recovered": self.obj.latestData["latest"]["recovered"] - self.obj.latestData["latest"]["recovered"], + } + else: + changes = { + "confirmed": 0, + "deaths": 0, + "recovered": 0, + } + return changes + + def getLatest(self) -> List[Dict[str, int]]: + """ + :return: The latest amount of total confirmed cases, deaths, and recoveries. + """ + data = self.obj._request("/v2/latest") + return data["latest"] + + def getLocations(self, timelines=False, rank_by: str = None) -> List[Dict]: + """ + Gets all locations affected by COVID-19, as well as latest case data. + :param timelines: Whether timeline information should be returned as well. + :param rank_by: Category to rank results by. ex: confirmed + :return: List of dictionaries representing all affected locations. + """ + data = None + if timelines: + data = self.obj._request("/v2/locations", {"timelines": str(timelines).lower()}) + else: + data = self.obj._request("/v2/locations") + + data = data["locations"] + + ranking_criteria = ['confirmed', 'deaths', 'recovered'] + if rank_by is not None: + if rank_by not in ranking_criteria: + raise ValueError("Invalid ranking criteria. Expected one of: %s" % ranking_criteria) + + ranked = sorted(data, key=lambda i: i['latest'][rank_by], reverse=True) + data = ranked + + return data + + diff --git a/COVID19Py/covid19.py b/COVID19Py/covid19.py index cb1ecae..3360418 100644 --- a/COVID19Py/covid19.py +++ b/COVID19Py/covid19.py @@ -32,10 +32,12 @@ def __init__(self, url="https://covid-tracker-us.herokuapp.com", data_source='jh # URL did not work, reset it and move on self.url = "" continue + # TODO: Should have a better health-check, this is way too hacky... if "jhu" in result: # We found a mirror that worked just fine, let's stick with it break + # None of the mirrors worked. Raise an error to inform the user. raise RuntimeError("No available API mirror was found.") @@ -69,54 +71,4 @@ def _request(self, endpoint, params=None): response.raise_for_status() return response.json() - def getAll(self, timelines=False): - self._update(timelines) - return self.latestData - - def getLatestChanges(self): - changes = None - if self.previousData: - changes = { - "confirmed": self.latestData["latest"]["confirmed"] - self.latestData["latest"]["confirmed"], - "deaths": self.latestData["latest"]["deaths"] - self.latestData["latest"]["deaths"], - "recovered": self.latestData["latest"]["recovered"] - self.latestData["latest"]["recovered"], - } - else: - changes = { - "confirmed": 0, - "deaths": 0, - "recovered": 0, - } - return changes - - def getLatest(self) -> List[Dict[str, int]]: - """ - :return: The latest amount of total confirmed cases, deaths, and recoveries. - """ - data = self._request("/v2/latest") - return data["latest"] - - def getLocations(self, timelines=False, rank_by: str = None) -> List[Dict]: - """ - Gets all locations affected by COVID-19, as well as latest case data. - :param timelines: Whether timeline information should be returned as well. - :param rank_by: Category to rank results by. ex: confirmed - :return: List of dictionaries representing all affected locations. - """ - data = None - if timelines: - data = self._request("/v2/locations", {"timelines": str(timelines).lower()}) - else: - data = self._request("/v2/locations") - - data = data["locations"] - - ranking_criteria = ['confirmed', 'deaths', 'recovered'] - if rank_by is not None: - if rank_by not in ranking_criteria: - raise ValueError("Invalid ranking criteria. Expected one of: %s" % ranking_criteria) - - ranked = sorted(data, key=lambda i: i['latest'][rank_by], reverse=True) - data = ranked - - return data +