From 0c190a9d6a4a6fa52f6bea3c6695f7848d4017d7 Mon Sep 17 00:00:00 2001 From: Deema Al Sheikhly Date: Thu, 15 Apr 2021 17:17:08 -0400 Subject: [PATCH] encapsulated_project --- .idea/.gitignore | 3 ++ .idea/COVID19Py.iml | 8 +++ .../inspectionProfiles/profiles_settings.xml | 6 +++ .idea/misc.xml | 4 ++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 +++ COVID19Py/covid19.py | 54 ++++++++++++------- COVID19Py/sources.py | 21 ++++++++ Pipfile | 4 +- Pipfile.lock | 31 ++++++----- examples/latest.py | 4 +- requirements.txt | 2 +- 12 files changed, 112 insertions(+), 39 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/COVID19Py.iml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 COVID19Py/sources.py diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/COVID19Py.iml b/.idea/COVID19Py.iml new file mode 100644 index 0000000..d0876a7 --- /dev/null +++ b/.idea/COVID19Py.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..ddd9c8b --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..9a08c67 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/COVID19Py/covid19.py b/COVID19Py/covid19.py index a68faa7..9076ecb 100644 --- a/COVID19Py/covid19.py +++ b/COVID19Py/covid19.py @@ -1,6 +1,8 @@ from typing import Dict, List import requests import json +from .sources import Reterivedata + class COVID19(object): default_url = "https://covid-tracker-us.herokuapp.com" @@ -27,7 +29,9 @@ def __init__(self, url="https://covid-tracker-us.herokuapp.com", data_source='jh self.url = mirror["url"] result = None try: - result = self._getSources() + retrieve = Reterivedata(url= url, data_source= data_source, + endpoint="/v2/sources", params=None) + result = retrieve.getSources() except Exception as e: # URL did not work, reset it and move on self.url = "" @@ -44,7 +48,9 @@ def __init__(self, url="https://covid-tracker-us.herokuapp.com", data_source='jh else: self.url = url - self._valid_data_sources = self._getSources() + retrieve = Reterivedata(url=url, data_source=data_source, + endpoint="/v2/sources", params=None) + self._valid_data_sources = retrieve.getSources() if data_source not in self._valid_data_sources: raise ValueError("Invalid data source. Expected one of: %s" % self._valid_data_sources) self.data_source = data_source @@ -64,14 +70,8 @@ def _getSources(self): response.raise_for_status() return response.json()["sources"] - 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.json() - def getAll(self, timelines=False): + self._update(timelines) return self.latestData @@ -95,7 +95,8 @@ def getLatest(self) -> List[Dict[str, int]]: """ :return: The latest amount of total confirmed cases, deaths, and recoveries. """ - data = self._request("/v2/latest") + retrieve = Reterivedata(self.url, self.data_source, "/v2/latest", None) + data = retrieve.retreive() return data["latest"] def getLocations(self, timelines=False, rank_by: str = None) -> List[Dict]: @@ -107,12 +108,15 @@ def getLocations(self, timelines=False, rank_by: str = None) -> List[Dict]: """ data = None if timelines: - data = self._request("/v2/locations", {"timelines": str(timelines).lower()}) + retrieve = Reterivedata(self.url, self.data_source, "/v2/locations", + {"timelines": str(timelines).lower()}) + data = retrieve.retreive() else: - data = self._request("/v2/locations") + retrieve = Reterivedata(self.url, self.data_source, "/v2/locations") + data = retrieve.retreive() data = data["locations"] - + ranking_criteria = ['confirmed', 'deaths', 'recovered'] if rank_by is not None: if rank_by not in ranking_criteria: @@ -131,11 +135,16 @@ def getLocationByCountryCode(self, country_code, timelines=False) -> List[Dict]: """ data = None if timelines: - data = self._request("/v2/locations", {"country_code": country_code, "timelines": str(timelines).lower()}) + retrieve = Reterivedata(self.url, self.data_source, "/v2/locations", + {"country_code": country_code, "timelines": str(timelines).lower()}) + data = retrieve.retreive() else: - data = self._request("/v2/locations", {"country_code": country_code}) + retrieve = Reterivedata(self.url, self.data_source, "/v2/locations", + {"country_code": country_code}) + data = retrieve.retreive() + return data["locations"] - + def getLocationByCountry(self, country, timelines=False) -> List[Dict]: """ :param country: String denoting name of the country @@ -144,9 +153,13 @@ def getLocationByCountry(self, country, timelines=False) -> List[Dict]: """ data = None if timelines: - data = self._request("/v2/locations", {"country": country, "timelines": str(timelines).lower()}) + retrieve = Reterivedata(self.url, self.data_source, "/v2/locations", + {"country": country, "timelines": str(timelines).lower()}) + data = retrieve.retreive() else: - data = self._request("/v2/locations", {"country": country}) + retrieve = Reterivedata(self.url, self.data_source, "/v2/locations", + {"country": country}) + data = retrieve.retreive() return data["locations"] def getLocationById(self, country_id: int): @@ -154,5 +167,6 @@ 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"] + retrieve = Reterivedata(self.url, self.data_source, "/v2/locations/" + str(country_id)) + data = retrieve.retreive() + return data["locatioGIT INITGIT INITgn"] \ No newline at end of file diff --git a/COVID19Py/sources.py b/COVID19Py/sources.py new file mode 100644 index 0000000..7996d5a --- /dev/null +++ b/COVID19Py/sources.py @@ -0,0 +1,21 @@ +import requests + + +class Reterivedata: + def __init__(self, url, data_source, endpoint, params=None): + self.url = url + self.data_source = data_source + self.endpoint = endpoint + self.params = params if params else None + + def retreive(self): + if self.params is None: + self.params = {} + response = requests.get(self.url + self.endpoint, {**self.params, "source": self.data_source}) + response.raise_for_status() + return response.json() + + def getSources(self): + response = requests.get(self.url + "/v2/sources") + response.raise_for_status() + return response.json()["sources"] \ No newline at end of file diff --git a/Pipfile b/Pipfile index b12d70f..37c73bb 100644 --- a/Pipfile +++ b/Pipfile @@ -4,9 +4,9 @@ verify_ssl = true name = "pypi" [packages] -requests = "~=2.24.0" +requests = "*" [dev-packages] [requires] -python_version = "3.6" +python_version = "3.9" diff --git a/Pipfile.lock b/Pipfile.lock index ef1a7b1..eb9d1e2 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,11 +1,11 @@ { "_meta": { "hash": { - "sha256": "7f2f5d1adbeccc71da52e4aa14bc80a823d3952b32091b58fbe67b6d8c826a01" + "sha256": "b8c2e1580c53e383cfe4254c1f16560b855d984fde8b2beb3bf6ee8fc2fe5a22" }, "pipfile-spec": 6, "requires": { - "python_version": "3.6" + "python_version": "3.9" }, "sources": [ { @@ -18,39 +18,42 @@ "default": { "certifi": { "hashes": [ - "sha256:5930595817496dd21bb8dc35dad090f1c2cd0adfaf21204bf6732ca5d8ee34d3", - "sha256:8fc0819f1f30ba15bdb34cceffb9ef04d99f420f68eb75d901e9560b8749fc41" + "sha256:1a4995114262bffbc2413b159f2a1a480c969de6e6eb13ee966d470af86af59c", + "sha256:719a74fb9e33b9bd44cc7f3a8d94bc35e4049deebe19ba7d8e108280cfd59830" ], - "version": "==2020.6.20" + "version": "==2020.12.5" }, "chardet": { "hashes": [ - "sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae", - "sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691" + "sha256:0d6f53a15db4120f2b08c94f11e7d93d2c911ee118b6b30a04ec3ee8310179fa", + "sha256:f864054d66fd9118f2e67044ac8981a54775ec5b67aed0441892edb553d21da5" ], - "version": "==3.0.4" + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'", + "version": "==4.0.0" }, "idna": { "hashes": [ "sha256:b307872f855b18632ce0c21c5e45be78c0ea7ae4c15c828c20788b26921eb3f6", "sha256:b97d804b1e9b523befed77c48dacec60e6dcb0b5391d57af6a65a312a90648c0" ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", "version": "==2.10" }, "requests": { "hashes": [ - "sha256:b3559a131db72c33ee969480840fff4bb6dd111de7dd27c8ee1f820f4f00231b", - "sha256:fe75cc94a9443b9246fc7049224f75604b113c36acb93f87b80ed42c44cbb898" + "sha256:27973dd4a904a4f13b263a19c866c13b92a39ed1c964655f025f3f8d3d75b804", + "sha256:c210084e36a42ae6b9219e00e48287def368a26d03a048ddad7bfee44f75871e" ], "index": "pypi", - "version": "==2.24.0" + "version": "==2.25.1" }, "urllib3": { "hashes": [ - "sha256:91056c15fa70756691db97756772bb1eb9678fa585d9184f24534b100dc60f4a", - "sha256:e7983572181f5e1522d9c98453462384ee92a0be7fac5f1413a1e35c56cc0461" + "sha256:2f4da4594db7e1e110a944bb1b551fdf4e6c136ad42e4234131391e21eb5b0df", + "sha256:e7b021f7241115872f92f43c6508082facffbd1c048e3c6e2bb9c2a157e28937" ], - "version": "==1.25.10" + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4' and python_version < '4'", + "version": "==1.26.4" } }, "develop": {} diff --git a/examples/latest.py b/examples/latest.py index 5d95cdb..842d14b 100644 --- a/examples/latest.py +++ b/examples/latest.py @@ -1,5 +1,5 @@ -import COVID19Py +from COVID19Py import COVID19 -covid19 = COVID19Py.COVID19() +covid19 = COVID19() print(covid19.getLatest()) diff --git a/requirements.txt b/requirements.txt index d6dd6ba..4970db3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -requests~=2.24.0 +requests~=2.25.1 \ No newline at end of file