-
-
Notifications
You must be signed in to change notification settings - Fork 314
Adding county information to API #114
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
814d107
added county api
JKSenthil e0bbf1a
readme updated
JKSenthil 009cb80
quick readme fix
JKSenthil 6975cb7
added csbs as service via middleware
JKSenthil 8250bf7
readme fix
JKSenthil 9c20a9f
last_updated changed to iso format
JKSenthil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| from .location.jhu import JhuLocationService | ||
| from .location.csbs import CSBSLocationService | ||
|
|
||
| # Instances of the services. | ||
| jhu = JhuLocationService() | ||
| jhu = JhuLocationService() | ||
| csbs = CSBSLocationService() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| from . import LocationService | ||
| from ...location import CSBSLocation | ||
| from ...coordinates import Coordinates | ||
|
|
||
| class CSBSLocationService(LocationService): | ||
| """ | ||
| Servive for retrieving locations from csbs | ||
| """ | ||
|
|
||
| def get_all(self): | ||
| # Get the locations | ||
| return get_locations() | ||
|
|
||
| def get(self, id): | ||
| return self.get_all()[id] | ||
|
|
||
| import requests | ||
| import csv | ||
| from datetime import datetime | ||
| from cachetools import cached, TTLCache | ||
|
|
||
| # Base URL for fetching data | ||
| base_url = 'https://facts.csbs.org/covid-19/covid19_county.csv' | ||
|
|
||
| @cached(cache=TTLCache(maxsize=1, ttl=3600)) | ||
| def get_locations(): | ||
| """ | ||
| Retrieves county locations; locations are cached for 1 hour | ||
|
|
||
| :returns: The locations. | ||
| :rtype: dict | ||
| """ | ||
| request = requests.get(base_url) | ||
| text = request.text | ||
|
|
||
| data = list(csv.DictReader(text.splitlines())) | ||
|
|
||
| locations = [] | ||
|
|
||
| for i, item in enumerate(data): | ||
| state = item['State Name'] | ||
| county = item['County Name'] | ||
| if county == "Unassigned" or county == "Unknown": | ||
| continue | ||
|
|
||
| confirmed = int(item['Confirmed'] or 0) | ||
| death = int(item['Death'] or 0) | ||
| coordinates = Coordinates(float(item['Latitude']), float(item['Longitude'])) | ||
|
|
||
| # Parse time to ISO format | ||
| last_update = item['Last Update'] | ||
| date = last_update.split("-") | ||
| year = int(date[0]) | ||
| month = int(date[1]) | ||
| date = date[2].split(" ") | ||
| day = int(date[0]) | ||
| time = date[1].split(":") | ||
| hour = int(time[0]) | ||
| minute = int(time[1]) | ||
| d = datetime(year=year, month=month, day=day, hour=hour, minute=minute) | ||
| last_update = d.isoformat() + 'Z' | ||
|
|
||
| locations.append(CSBSLocation(i, state, county, coordinates, last_update, confirmed, death)) | ||
|
|
||
| return locations | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| County Name,State Name,Confirmed,New,Death,Fatality Rate,Latitude,Longitude,Last Update | ||
| New York,New York,4408,454,26,0.6%,40.71455,-74.00714,2020-03-20 13:58 EDT | ||
| Westchester,New York,1091,293,0,0%,41.16319759,-73.7560629,2020-03-20 13:58 EDT | ||
| Nassau,New York,754,382,4,0.5%,40.74165225,-73.58899619,2020-03-20 13:58 EDT | ||
| Yakima,Washington,7,0,0,0%,46.60448,-120.50721,2020-03-20 13:58 EDT | ||
| Thurston,Washington,6,0,0,0%,46.91980578,-122.8298691,2020-03-20 13:58 EDT | ||
| Jefferson,Washington,4,0,0,0%,47.74810608,-123.6000095,2020-03-20 13:58 EDT | ||
| Douglas,Kansas,1,0,0,0%,38.88462907,-95.29255463,2020-03-20 13:58 EDT | ||
| Cherokee,Kansas,1,0,0,0%,37.16926692,-94.8462675759999,2020-03-20 13:58 EDT | ||
| Jackson,Kansas,1,0,0,0%,39.4168027220001,-95.793674403,2020-03-20 13:58 EDT | ||
| Twin Falls,Idaho,1,0,0,0%,42.55619,-114.4696,2020-03-20 13:58 EDT | ||
| Kootenai,Idaho,1,0,0,0%,47.6775872760001,-116.697131928,2020-03-20 13:58 EDT | ||
| Chittenden,Vermont,4,0,1,25%,44.45799511,-73.05404973,2020-03-20 13:58 EDT | ||
| Bennington,Vermont,3,0,0,0%,42.87672,-73.19818,2020-03-20 13:58 EDT | ||
| Windsor,Vermont,3,0,1,33.3%,43.48115,-72.38581,2020-03-20 13:58 EDT | ||
| Washington,Vermont,1,0,0,0%,44.27344561,-72.61485925,2020-03-20 13:58 EDT | ||
| Orange,Vermont,1,0,0,0%,44.14854,-72.40233,2020-03-20 13:58 EDT | ||
| Addison,Vermont,1,0,0,0%,44.0280736,-73.13152876,2020-03-20 13:58 EDT | ||
| Burleigh,North Dakota,11,0,0,0%,46.97801044,-100.4669442,2020-03-20 13:58 EDT | ||
| Tucker,West Virginia,2,0,0,0%,39.1135508250001,-79.56492129,2020-03-20 13:58 EDT | ||
| Mercer,West Virginia,1,0,0,0%,37.40556515,-81.11143231,2020-03-20 13:58 EDT | ||
| Monongalia,West Virginia,1,0,0,0%,39.630233859,-80.0465546289999,2020-03-20 13:58 EDT | ||
| Unassigned,New York,166,149,4,2.4%,42.165726,-74.948051,2020-03-20 13:58 EDT | ||
| Unassigned,Washington,151,0,0,0%,47.400902,-121.490494,2020-03-20 13:58 EDT | ||
| Unassigned,Colorado,57,0,0,0%,39.059811,-105.311104,2020-03-20 13:58 EDT | ||
| Unknown,Pennsylvania,55,55,0,0%,40.590752,-77.209755,2020-03-20 13:58 EDT | ||
| Unassigned,Pennsylvania,0,0,0,NaN%,40.590752,-77.209755,2020-03-20 13:58 EDT | ||
| Franklin,Pennsylvania,1,1,0,0%,39.927495836,-77.721161869,2020-03-20 13:58 EDT | ||
| Franklin,North Carolina,4,4,0,0%,36.0827448150001,-78.285600305,2020-03-20 13:58 EDT | ||
| Lee,North Carolina,1,1,0,0%,35.475059921,-79.17154054,2020-03-20 13:58 EDT | ||
| Clay,Minnesota,1,1,0,0%,46.892347886,-96.490737839,2020-03-20 13:58 EDT | ||
| Yuma,Arizona,1,1,0,0%,32.768956524,-113.905830295,2020-03-20 13:58 EDT | ||
| Dunklin,Missouri,1,1,0,0%,36.105848973,-90.16563,2020-03-20 13:58 EDT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import app | ||
| import datetime | ||
| import pytest | ||
| from unittest import mock | ||
| from app.services.location import csbs | ||
|
|
||
| def mocked_csbs_requests_get(*args, **kwargs): | ||
| class FakeRequestsGetResponse: | ||
| """ | ||
| Returns instance of `FakeRequestsGetResponse` | ||
| when calling `app.services.location.csbs.requests.get() | ||
| """ | ||
| def __init__(self): | ||
| self.text = self.read_file() | ||
|
|
||
| def read_file(self): | ||
| """ | ||
| Mock HTTP GET-method and return text from file | ||
| """ | ||
| filepath = "tests/example_data/sample_covid19_county.csv" | ||
| print("Try to read {}".format(filepath)) | ||
| with open(filepath, "r") as file: | ||
| return file.read() | ||
|
|
||
| return FakeRequestsGetResponse() | ||
|
|
||
| @mock.patch('app.services.location.csbs.requests.get', side_effect=mocked_csbs_requests_get) | ||
| def test_get_locations(mock_request_get): | ||
| data = csbs.get_locations() | ||
| assert isinstance(data, list) | ||
|
|
||
| # check to see that Unknown/Unassigned has been filtered | ||
| for d in data: | ||
| assert d.county != "Unknown" | ||
| assert d.county != "Unassigned" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be parsed as ISO with an added 'Z' at the end. Use
import datetime from datetimeto achieve this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed!