Skip to content

Commit

Permalink
Merge pull request #69 from ExpDev07/v2
Browse files Browse the repository at this point in the history
Version 2 has landed!
  • Loading branch information
ExpDev07 authored Mar 18, 2020
2 parents 28dd4c8 + a0327d4 commit 75eaed7
Show file tree
Hide file tree
Showing 19 changed files with 417 additions and 110 deletions.
88 changes: 73 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,95 @@

## Endpoints

All requests must be made to the base url: ``https://coronavirus-tracker-api.herokuapp.com`` (e.g: https://coronavirus-tracker-api.herokuapp.com/all). You can try them out in your browser to further inspect responses.
All requests must be made to the base url: ``https://coronavirus-tracker-api.herokuapp.com/v2/`` (e.g: https://coronavirus-tracker-api.herokuapp.com/v2/locations). You can try them out in your browser to further inspect responses.

Getting confirmed cases, deaths, and recoveries:
### Getting latest amount of total confirmed cases, deaths, and recoveries.
```http
GET /all
GET /v2/latest
```
```json
{ "latest": { ... }, "confirmed": { ... }, "deaths": { ... }, "recovered": { ... } }
{
"latest": {
"confirmed": 197146,
"deaths": 7905,
"recovered": 80840
}
}
```

Getting just confirmed:
### Getting all locations.
```http
GET /confirmed
GET /v2/locations
```
```json
{
"latest": 42767,
"locations": [ ... ],
"last_updated": "2020-03-07T18:08:58.432242Z",
"source": "https://github.com/ExpDev07/coronavirus-tracker-api"
"locations": [
{
"id": 0,
"country": "Thailand",
"country_code": "TH",
"province": "",
"coordinates": {
"latitude": "15",
"longitude": "101"
},
"latest": {
"confirmed": 177,
"deaths": 1,
"recovered": 41
}
},
{
"id": 39,
"country": "Norway",
"country_code": "NO",
"province": "",
"coordinates": {
"latitude": "60.472",
"longitude": "8.4689"
},
"latest": {
"confirmed": 1463,
"deaths": 3,
"recovered": 1
}
}
]
}
```

Getting just deaths:
Additionally, you can also filter by country ([alpha-2 country_code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)).
```http
GET /deaths
GET /v2/locations?country_code=US
```

Getting just recoveries:
### Getting a specific location (includes timeline).
```http
GET /recovered
GET /v2/locations/:id
```
```json
{
"location": {
"id": 39,
"country": "Norway",
"country_code": "NO",
"province": "",
"coordinates": { },
"latest": { },
"timelines": {
"confirmed": {
"latest": 1463,
"timeline": {
"2020-03-16T00:00:00Z": 1333,
"2020-03-17T00:00:00Z": 1463
}
},
"deaths": { },
"recovered": { }
}
}
}
}
```

## Data
Expand Down Expand Up @@ -112,5 +170,5 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d

## License

The data is available to the public strictly for educational and academic research purposes. Please link to this repo somewhere in your project if you can (not required) :).
The data is available to the public strictly for educational and academic research purposes. Please link to this repo somewhere in your project :).

3 changes: 1 addition & 2 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from flask import Flask
from flask_cors import CORS
from . import settings

def create_app():
"""
Expand All @@ -12,7 +11,7 @@ def create_app():
CORS(app)

# Set app config from settings.
app.config.from_pyfile('settings.py');
app.config.from_pyfile('config/settings.py');

with app.app_context():
# Import routes.
Expand Down
File renamed without changes.
20 changes: 20 additions & 0 deletions app/coordinates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Coordinates:
"""
A position on earth using decimal coordinates (latitude and longitude).
"""

def __init__(self, latitude, longitude):
self.latitude = latitude
self.longitude = longitude

def serialize(self):
"""
Serializes the coordinates into a dict.
"""
return {
'latitude' : self.latitude,
'longitude': self.longitude
}

def __str__(self):
return 'lat: %s, long: %s' % (self.latitude, self.longitude)
73 changes: 0 additions & 73 deletions app/data/__init__.py

This file was deleted.

49 changes: 49 additions & 0 deletions app/location.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from .coordinates import Coordinates
from .utils import countrycodes

class Location:
"""
A location in the world affected by the coronavirus.
"""

def __init__(self, id, country, province, coordinates, confirmed, deaths, recovered):
# General info.
self.id = id
self.country = country.strip()
self.province = province.strip()
self.coordinates = coordinates

# Data.
self.confirmed = confirmed
self.deaths = deaths
self.recovered = recovered


@property
def country_code(self):
"""
Gets the alpha-2 code represention of the country. Returns 'XX' if none is found.
"""
return (countrycodes.country_code(self.country) or countrycodes.default_code).upper()

def serialize(self):
"""
Serializes the location into a dict.
"""
return {
# General info.
'id' : self.id,
'country' : self.country,
'province' : self.province,
'country_code': self.country_code,

# Coordinates.
'coordinates': self.coordinates.serialize(),

# Latest data.
'latest': {
'confirmed': self.confirmed.latest,
'deaths' : self.deaths.latest,
'recovered': self.recovered.latest
}
}
4 changes: 0 additions & 4 deletions app/models/location.py

This file was deleted.

9 changes: 5 additions & 4 deletions app/routes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from . import confirmed
from . import deaths
from . import recovered
from . import all
# API version 1.
from .v1 import confirmed, deaths, recovered, all

# API version 2.
from .v2 import locations, latest
8 changes: 4 additions & 4 deletions app/routes/all.py → app/routes/v1/all.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from flask import jsonify
from flask import current_app as app
from ..data import get_data
from ...services.location.jhu import get_category

@app.route('/all')
def all():
# Get all the categories.
confirmed = get_data('confirmed')
deaths = get_data('deaths')
recovered = get_data('recovered')
confirmed = get_category('confirmed')
deaths = get_category('deaths')
recovered = get_category('recovered')

return jsonify({
# Data.
Expand Down
4 changes: 2 additions & 2 deletions app/routes/confirmed.py → app/routes/v1/confirmed.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from flask import jsonify
from flask import current_app as app
from ..data import get_data
from ...services.location.jhu import get_category

@app.route('/confirmed')
def confirmed():
return jsonify(get_data('confirmed'))
return jsonify(get_category('confirmed'))
4 changes: 2 additions & 2 deletions app/routes/deaths.py → app/routes/v1/deaths.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from flask import jsonify
from flask import current_app as app
from ..data import get_data
from ...services.location.jhu import get_category

@app.route('/deaths')
def deaths():
return jsonify(get_data('deaths'))
return jsonify(get_category('deaths'))
4 changes: 2 additions & 2 deletions app/routes/recovered.py → app/routes/v1/recovered.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from flask import jsonify
from flask import current_app as app
from ..data import get_data
from ...services.location.jhu import get_category

@app.route('/recovered')
def recovered():
return jsonify(get_data('recovered'))
return jsonify(get_category('recovered'))
18 changes: 18 additions & 0 deletions app/routes/v2/latest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from flask import jsonify, current_app as app
from ...services import jhu

@app.route('/v2/latest')
def latest():
# Get the serialized version of all the locations.
locations = [ location.serialize() for location in jhu.get_all() ]

# All the latest information.
latest = list(map(lambda location: location['latest'], locations))

return jsonify({
'latest': {
'confirmed': sum(map(lambda latest: latest['confirmed'], latest)),
'deaths' : sum(map(lambda latest: latest['deaths'], latest)),
'recovered': sum(map(lambda latest: latest['recovered'], latest)),
}
})
Loading

0 comments on commit 75eaed7

Please sign in to comment.