From cadd2f984b8f0ee49e665e0d8bba9b63faf50588 Mon Sep 17 00:00:00 2001 From: Kevin Date: Wed, 17 Mar 2021 15:42:56 -0400 Subject: [PATCH 1/6] testing commit on readme --- README.md | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 27491cc..a68553f 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # COVID19Py + + [![Downloads](https://pepy.tech/badge/covid19py)](https://pepy.tech/project/covid19py) [![Downloads](https://pepy.tech/badge/covid19py/month)](https://pepy.tech/project/covid19py/month) [![Downloads](https://pepy.tech/badge/covid19py/week)](https://pepy.tech/project/covid19py/week) @@ -8,8 +10,11 @@ [![CodeFactor](https://www.codefactor.io/repository/github/kamaropoulos/covid19py/badge)](https://www.codefactor.io/repository/github/kamaropoulos/covid19py) [![HitCount](http://hits.dwyl.com/Kamaropoulos/COVID19Py.svg)](http://hits.dwyl.com/Kamaropoulos/COVID19Py) [![GitHub stars](https://img.shields.io/github/stars/Kamaropoulos/COVID19Py.svg?style=social&label=Star)](https://github.com/Kamaropoulos/COVID19Py) + + A tiny Python package for easy access to up-to-date Coronavirus (COVID-19, SARS-CoV-2) cases data. + ## About @@ -18,10 +23,12 @@ COVID19Py is a Python wrapper made to be compatible with [ExpDev07/coronavirus-t It retrieves data directly from an instance of [@ExpDev07](https://covid-tracker-us.herokuapp.com/)'s backend but it can also be set up to use a different backend. To achieve this, just pass the URL of the backend as a parameter to the library's constructor: + ```python import COVID19Py covid19 = COVID19Py.COVID19("https://my-awesome-covid19-backend") ``` + ## Installation In order install this package, simply run: @@ -43,9 +50,11 @@ covid19 = COVID19Py.COVID19() COVID19Py supports the retrieval of data from multiple data sources. To choose a specific data source, simply pass it as a parameter to the library's constructor: + ```python covid19 = COVID19Py.COVID19(data_source="csbs") ``` + For more details about the available data sources, please check the [API's documentation](https://github.com/ExpDev07/coronavirus-tracker-api/blob/master/README.md#picking-data-source). ### Getting latest amount of total confirmed cases, deaths, and recoveries: @@ -65,6 +74,7 @@ or: ```python locations = covid19.getLocations(timelines=True) ``` + to also get timelines. You can also rank the results by `confirmed`, `deaths` or `recovered`. @@ -78,10 +88,13 @@ locations = covid19.getLocations(rank_by='recovered') ```python location = covid19.getLocationByCountryCode("US") ``` + or: + ```python location = covid19.getLocationByCountryCode("US", timelines=True) ``` + to also get timelines. ### Getting a specific location (includes timelines by default): @@ -102,7 +115,6 @@ or: location = covid19.getLocationByCountry("Zimbabwe", timelines=True) ``` - ### Getting all data at once: You can also get all the available data with one command. @@ -110,10 +122,13 @@ You can also get all the available data with one command. ```python data = covid19.getAll() ``` + or: + ```python data = covid19.getAll(timelines=True) ``` + to also get timelines. `latest` will be available on `data["latest"]` and `locations` will be available on `data["locations"]`. @@ -125,18 +140,21 @@ When using `getAll()`, COVID19Py will also store the previous version of the ret ```python changes = covid19.getLatestChanges() ``` + ```json { - "confirmed": 512, - "deaths": 16, - "recovered": 1024 + "confirmed": 512, + "deaths": 16, + "recovered": 1024 } ``` + ## Contributing You will need to fork this repository onto your own GitHub account, then clone the repo to your local machine. In the project directory, you will need to make sure the required packages are installed. To install requirements from the Pipfile, run a command like the following, which may depend on how you have package management set up: + ```bash pipenv install ``` @@ -157,6 +175,9 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d + This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! + +# just testing! From c66be29abb2330a1ce7d9e58a32bcb4bbc557a1c Mon Sep 17 00:00:00 2001 From: Kevin Date: Wed, 17 Mar 2021 21:24:32 -0400 Subject: [PATCH 2/6] added CovidLoc class with country location functionalites --- .vscode/settings.json | 3 ++ COVID19Py/covid19.py | 98 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 85 insertions(+), 16 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..615aafb --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": "/usr/bin/python3" +} \ No newline at end of file diff --git a/COVID19Py/covid19.py b/COVID19Py/covid19.py index a68faa7..f84417a 100644 --- a/COVID19Py/covid19.py +++ b/COVID19Py/covid19.py @@ -2,6 +2,62 @@ import requests import json + + +class CovidLoc: + url = "" + data_source = "" + + + + def sources(self, url, data): + self.url = url + self.data_source = data + + def _request(self, endpoint, params=None): + if params is None: + params = {} + response = requests.get(self.url + endpoint, {**params, "source":self.data_source}) + response.raise_for_status() + return response + + def getLocationByIdTwoo(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 + + def getLocationByCountryTwo(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 + + def getLocationByCountryCodeTwo(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 + + +covidloc = CovidLoc() + class COVID19(object): default_url = "https://covid-tracker-us.herokuapp.com" url = "" @@ -9,12 +65,14 @@ class COVID19(object): previousData = None latestData = None _valid_data_sources = [] - + covidLoc = "hello" mirrors_source = "https://raw.github.com/Kamaropoulos/COVID19Py/master/mirrors.json" mirrors = None - def __init__(self, url="https://covid-tracker-us.herokuapp.com", data_source='jhu'): + def __init__(self, url="https://covid-tracker-us.herokuapp.com", data_source='jhu', covidLocation = covidloc): # Skip mirror checking if custom url was passed + self.covidLoc = covidLocation + if url == self.default_url: # Load mirrors response = requests.get(self.mirrors_source) @@ -49,6 +107,9 @@ def __init__(self, url="https://covid-tracker-us.herokuapp.com", data_source='jh raise ValueError("Invalid data source. Expected one of: %s" % self._valid_data_sources) self.data_source = data_source + self.covidLoc.sources(self.url,self.data_source) + + def _update(self, timelines): latest = self.getLatest() locations = self.getLocations(timelines) @@ -129,12 +190,8 @@ def getLocationByCountryCode(self, country_code, timelines=False) -> List[Dict]: :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"] + + return self.covidLoc.getLocationByCountryCodeTwo(country_code).json()["locations"] def getLocationByCountry(self, country, timelines=False) -> List[Dict]: """ @@ -142,17 +199,26 @@ def getLocationByCountry(self, country, timelines=False) -> List[Dict]: :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"] + + return self.covidLoc.getLocationByCountryTwo(country).json()["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"] + + return self.covidLoc.getLocationByIdTwoo(39).json()["location"] + + + + # def getLocationByIdtwo(self, country_id: int): + + + + # return self.covidLoc.getLocationByIdTwoo(39).json()["location"] + + + \ No newline at end of file From cca1cfd9fecb447020e4033ee5cb207782083130 Mon Sep 17 00:00:00 2001 From: Kevin Date: Wed, 17 Mar 2021 21:34:37 -0400 Subject: [PATCH 3/6] removed trailing new lines --- COVID19Py/covid19.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/COVID19Py/covid19.py b/COVID19Py/covid19.py index f84417a..7efa2fb 100644 --- a/COVID19Py/covid19.py +++ b/COVID19Py/covid19.py @@ -211,14 +211,5 @@ def getLocationById(self, country_id: int): """ return self.covidLoc.getLocationByIdTwoo(39).json()["location"] - - - - # def getLocationByIdtwo(self, country_id: int): - - - - # return self.covidLoc.getLocationByIdTwoo(39).json()["location"] - \ No newline at end of file From 7010f6acc9df37fe546d8c0d6b6aef6eb6a71eed Mon Sep 17 00:00:00 2001 From: Kevin Date: Wed, 17 Mar 2021 21:42:39 -0400 Subject: [PATCH 4/6] corrected enwline error --- .vscode/settings.json | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 615aafb..42aff5e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,9 @@ { - "python.pythonPath": "/usr/bin/python3" + "python.pythonPath": "/usr/bin/python3", + "python.testing.pytestArgs": [ + "COVID19Py" + ], + "python.testing.unittestEnabled": false, + "python.testing.nosetestsEnabled": false, + "python.testing.pytestEnabled": true } \ No newline at end of file From 76c008a2939016ab94bb4cc0de8228a971e4f5c3 Mon Sep 17 00:00:00 2001 From: Kevin Date: Wed, 17 Mar 2021 21:51:02 -0400 Subject: [PATCH 5/6] new line error fix --- COVID19Py/covid19.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/COVID19Py/covid19.py b/COVID19Py/covid19.py index 7efa2fb..f52671c 100644 --- a/COVID19Py/covid19.py +++ b/COVID19Py/covid19.py @@ -203,7 +203,7 @@ def getLocationByCountry(self, country, timelines=False) -> List[Dict]: return self.covidLoc.getLocationByCountryTwo(country).json()["locations"] - + def getLocationById(self, country_id: int): """ :param country_id: Country Id, an int From 2b71585d7926f74b266ecfb71140140ee4d99711 Mon Sep 17 00:00:00 2001 From: Kevin Date: Wed, 17 Mar 2021 21:55:43 -0400 Subject: [PATCH 6/6] trailing new line --- COVID19Py/covid19.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/COVID19Py/covid19.py b/COVID19Py/covid19.py index f52671c..e91a87f 100644 --- a/COVID19Py/covid19.py +++ b/COVID19Py/covid19.py @@ -201,8 +201,7 @@ def getLocationByCountry(self, country, timelines=False) -> List[Dict]: """ return self.covidLoc.getLocationByCountryTwo(country).json()["locations"] - - + def getLocationById(self, country_id: int): """ @@ -210,6 +209,4 @@ def getLocationById(self, country_id: int): :return: A dictionary with case information for the specified location. """ - return self.covidLoc.getLocationByIdTwoo(39).json()["location"] - - \ No newline at end of file + return self.covidLoc.getLocationByIdTwoo(39).json()["location"] \ No newline at end of file