Skip to content
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
Binary file added .DS_Store
Binary file not shown.
34 changes: 34 additions & 0 deletions Abstract class/abstractCoronavirus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from abc import ABCMeta, abstractmethod

class AbstractCoronavirus(metaclass=ABCMeta):

@abstractmethod
def _update():
pass
@abstractmethod
def _getSources():
pass
@abstractmethod
def _request():
pass
@abstractmethod
def getAll():
pass
@abstractmethod
def getLatestChanges():
pass
@abstractmethod
def getLatest():
pass
@abstractmethod
def getLocations():
pass
@abstractmethod
def getLocationByCountryCode():
pass
@abstractmethod
def getLocationByCountry():
pass
@abstractmethod
def getLocationById():
pass
Binary file added COVID19Py/.DS_Store
Binary file not shown.
91 changes: 91 additions & 0 deletions COVID19Py/ObjectBuilder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from abc import ABCMeta,abstractmethod
from typing import Dict, List
import covid19
import requests
import json

"The Builder Interface"
class InterfaceBuilder(metaclass=ABCMeta):
@staticmethod
@abstractmethod
def setURL():
"build url"

@staticmethod
@abstractmethod
def setDataSource():
"build data source"

@staticmethod
@abstractmethod
def getProduct():
"return the final product"

"The concrete Builder"
class ObjectBuilder(InterfaceBuilder):

def __init__(self):
self.product = covid19.COVID19()

def setURL(self,url):
if url == self.product.default_url:

# Load mirrors
response = requests.get(self.product.mirrors_source)
response.raise_for_status()
self.product.mirrors = response.json()
# Try to get sources as a test
for mirror in self.product.mirrors:
# Set URL of mirror
self.product.url = mirror["url"]
result = None

try:
result = self.product._getSources()

except Exception as e:

# URL did not work, reset it and move on
self.product.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.")

else:
self.product.url = url

return self

def setDataSource(self,data_source):

self.product._valid_data_sources = self.product._getSources()

if data_source not in self.product._valid_data_sources:
raise ValueError("Invalid data source. Expected one of: %s" % self._valid_data_sources)
self.product.data_source = data_source

return self

def getProduct(self):
return self.product


class Director:

@staticmethod
def constructURL(obj=ObjectBuilder(),url="https://covid-tracker-us.herokuapp.com"):
return obj.setURL(url)
@staticmethod
def constructDataSource(obj=ObjectBuilder(),dataSource='jhu'):
return obj.setDataSource(dataSource)
@staticmethod
def contructWholeProduct(obj=ObjectBuilder(),url="https://covid-tracker-us.herokuapp.com", dataSource='jhu'):
return obj.setURL(url).setDataSource(dataSource).getProduct()


55 changes: 10 additions & 45 deletions COVID19Py/covid19.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,18 @@
from typing import Dict, List
import requests
import json
from abstractCoronavirus import AbstractCoronavirus

class COVID19(object):
default_url = "https://covid-tracker-us.herokuapp.com"
url = ""
data_source = ""
class COVID19(AbstractCoronavirus):
previousData = None
latestData = None
_valid_data_sources = []

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'):
# Skip mirror checking if custom url was passed
if url == self.default_url:
# Load mirrors
response = requests.get(self.mirrors_source)
response.raise_for_status()
self.mirrors = response.json()

# Try to get sources as a test
for mirror in self.mirrors:
# Set URL of mirror
self.url = mirror["url"]
result = None
try:
result = self._getSources()
except Exception as e:
# 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.")

else:
self.url = url

self._valid_data_sources = self._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
def __init__(self):
self.default_url = "https://covid-tracker-us.herokuapp.com"
self.mirrors_source = "https://raw.github.com/Kamaropoulos/COVID19Py/master/mirrors.json"
self._valid_data_sources = []
self.url = "https://covid-tracker-us.herokuapp.com"
self.data_source = "jhu"
self.mirrors = None

def _update(self, timelines):
latest = self.getLatest()
Expand All @@ -67,7 +32,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()

Expand Down