diff --git a/README.md b/README.md index 7c1cb27f..804878d2 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,11 @@ GET /v2/locations ``` ```json { + "latest": { + "confirmed": 272166, + "deaths": 11299, + "recovered": 87256 + }, "locations": [ { "id": 0, @@ -82,7 +87,7 @@ GET /v2/locations } ``` -Additionally, you can also filter by country ([alpha-2 country_code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)). +Additionally, you can also filter by any attribute, including province and country ([alpha-2 country_code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)). ```http GET /v2/locations?country_code=US ``` diff --git a/app/routes/v2/locations.py b/app/routes/v2/locations.py index 8d443924..f9104015 100644 --- a/app/routes/v2/locations.py +++ b/app/routes/v2/locations.py @@ -5,18 +5,27 @@ @api.route('/locations') def locations(): # Query parameters. - timelines = strtobool(request.args.get('timelines', default='0')) - country_code = request.args.get('country_code', type=str) + args = request.args + timelines = strtobool(args.get('timelines', default='0')) # Retrieve all the locations. locations = request.source.get_all() - # Filtering my country code if provided. - if not country_code is None: - locations = list(filter(lambda location: location.country_code == country_code.upper(), locations)) + # Filtering by args if provided. + for i in args: + if i != 'timelines' and i[:2] != '__': + try: + locations = [j for j in locations if getattr(j, i) == args.get(i, type=str)] + except AttributeError: + print('TimelinedLocation object does not have attribute {}.'.format(i)) # Serialize each location and return. return jsonify({ + 'latest': { + 'confirmed': sum(map(lambda location: location.confirmed, locations)), + 'deaths' : sum(map(lambda location: location.deaths, locations)), + 'recovered': sum(map(lambda location: location.recovered, locations)), + }, 'locations': [ location.serialize(timelines) for location in locations ]