Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encapsulated_project #135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/COVID19Py.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 34 additions & 20 deletions COVID19Py/covid19.py
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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 = ""
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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]:
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -144,15 +153,20 @@ 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):
"""
: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"]
21 changes: 21 additions & 0 deletions COVID19Py/sources.py
Original file line number Diff line number Diff line change
@@ -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"]
4 changes: 2 additions & 2 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
31 changes: 17 additions & 14 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/latest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import COVID19Py
from COVID19Py import COVID19

covid19 = COVID19Py.COVID19()
covid19 = COVID19()

print(covid19.getLatest())
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
requests~=2.24.0
requests~=2.25.1