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/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 a68faa7..3360418 100644 --- a/COVID19Py/covid19.py +++ b/COVID19Py/covid19.py @@ -67,92 +67,8 @@ 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() - 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 - - 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"] +