Browse Source

removed the work on filte for now, cleaned up files and repo structure. This branch is complete and ready to be merged into main. Also releasing this version to the site.

kc-covid19-zipcode
prasket 2 months ago
parent
commit
00c5d345b1
11 changed files with 43 additions and 84 deletions
  1. +25
    -2
      README.md
  2. +18
    -82
      api.py
  3. +0
    -0
      database/create_covid-19_zip.py
  4. +0
    -0
      database/load_db.py
  5. +0
    -0
      database/source_data/covid-19.xlsx
  6. +0
    -0
      database/source_data/covid-19_20200810_zip.csv
  7. +0
    -0
      database/source_data/covid-19_20200824_zip.csv
  8. +0
    -0
      database/source_data/covid-19_20200831_zip.csv
  9. +0
    -0
      database/source_data/covid-data-extract-geography-august-10.xlsx
  10. +0
    -0
      database/source_data/covid-data-extract-geography-august-24.xlsx
  11. +0
    -0
      database/source_data/covid-data-extract-geography-august-31.xlsx

+ 25
- 2
README.md View File

@@ -2,7 +2,6 @@

API to help make public data easier to access. Idea started with need for King County, WA COVID-19 data in API format while only having an excel file for source data.


## Development Setup

### Datastore
@@ -20,6 +19,22 @@ docker ps # to find the CONTAINER ID
docker exec -it 05b3a3471f6f bash
```

From the shell login to the db and setup initial user and database

```bash
psql -U postgres
```

Create your creds.py from the creds_sample.py

```bash
cp creds_sample.py creds.py
```

Edit creds.py with correct info for your db.

More info coming soon on using the scripts in database/ to create tables and load them with data.

### Development Env.

Project is using Python 3.7
@@ -29,8 +44,16 @@ In the project we use [pipenv](https://pipenv.pypa.io/en/latest/) when developin
```bash
pipenv install && pipenv shell
```
### Production

When running on Debian server libpq-dev is required, this is postgresl library.

```bash
sudo apt-get install libpq-dev
```

### Data Available

- King County, Washington COVID-19 Data
- King County, Washington COVID-19 Data
- [King County, WS Covid-19 by ZipCode](https://api.seattlematrix.org/v1/kingcounty/covid-19/zipcode/all)
- sample source url https://www.kingcounty.gov/depts/health/covid-19/data/~/media/depts/health/communicable-diseases/documents/C19/data/covid-data-extract-geography-august-24.ashx

+ 18
- 82
api.py View File

@@ -7,104 +7,40 @@ app = flask.Flask(__name__)
app.config["DEBUG"] = True
import creds

# assign secrets to variables
# Assign secrets to variables, should look into making this
# a function. Plan to move this to Vault from creds file.
dbhost = creds.dblogin['host']
database = creds.dblogin['db']
username = creds.dblogin['username']
password = creds.dblogin['password']
port = creds.dblogin['port']


# Create some test data this will eventually be in a Postgresql db.
zipcodes = [
{'id': 0,
'date_added': '20200810',
'date_data': '20200810',
'zipcode': '98121',
'population': '22274',
'tests': '3182',
'test_rate': '142.86',
'positives': '64',
'positive_rate': '2.87',
'hospitalizations': '4',
'hospitalization_rate': '0.18',
'deaths INTEGER': '2',
'death_rate':'0.09'
},
{'id': 1,
'date_added': '20200810',
'date_data': '20200810',
'zipcode': '98109',
'population': '34939',
'tests': '6142',
'test_rate': '175.79',
'positives': '206',
'positive_rate': '5.9',
'hospitalizations': '38',
'hospitalization_rate': '1.09',
'deaths INTEGER': '19',
'death_rate':'0.54'
},
{'id': 2,
'date_added': '20200810',
'date_data': '20200810',
'zipcode': '98116',
'population': '26979',
'tests': '5259',
'test_rate': '194.93',
'positives': '121',
'positive_rate': '4.48',
'hospitalizations': '5',
'hospitalization_rate': '0.19',
'deaths INTEGER': '2',
'death_rate':'0.07'
}
]


@app.route('/', methods=['GET'])
def home():
return "<h1>Public API offered by the team at Seattle Matrix.</h1> <p>This \
# TODO: Look into putting all this into a template or something.
return """<h1>Public API offered by the team at Seattle Matrix</h1> <p>This \
site is a collection of api's from various public sources collected \
by the team at Seattle Matrix</p>"
by the team at <a href="https://seattlematrix.org" target="_blank">SeattleMatrix.org</a>.</p> Current Data: \
</br><a href="https://api.seattlematrix.org/v1/kingcounty/covid-19/zipcode/all" target="_blank">King County, WA Covid-19 By ZipCode</a> - Current Dates: 20200810, 20200824, 20200831
"""

# A route to return all of the available records for King County
# A route to return all of the available records for King County COVID-19
@app.route('/v1/kingcounty/covid-19/zipcode/all', methods=['GET'])
def api_zipcode_all():
conn = psycopg2.connect(f"host={dbhost} dbname={database} user={username} password={password} port={port}")
""" This route returns all the data in the king_county_covid19_zip
table. This data is currently manually updated.
"""
conn = psycopg2.connect(f"host={dbhost} dbname={database} user={username} password={password} port={port} sslmode='require'")
cur = conn.cursor()
cur.execute("select row_to_json(king_county_covid19_zip) from king_county_covid19_zip;")
rows = cur.fetchall()
# returning rv throws an error and jsonify returns double nested
# FIXME: returning row throws an error and jsonify returns double nested
# json result need to figure this out.
return jsonify(rows)

@app.route('/v1/kingcounty/covid-19/zipcode', methods=['GET'])
def api_zipcode():
# Check to see if 'zip' was provided in URL
# If no zip display message stating this
if 'zip' in request.args:
provided_zipcode = request.args['zip']
else:
return "Error: No zipcode [zip] field provided. Please specify an zipcode."

# Create an empty list for our results
results = []

# Loop through the data and match results that fit the requested Zipcode.
# zipcodes are unique, but other fields might return many results
for zipcode in zipcodes:
if zipcode['zipcode'] == provided_zipcode:
results.append(zipcode)

# If no zipcodes matched and results are empty show message rather than
# empty page, maybe this isnt good to do in an API that is going to be used
# for human and computer consumption, need to check into that.
if not results:
results = "The Zipcode you entered was not in the API, please try again"

# Use the jsonify function from Flask to convert our list of
# Python dictionaries to the JSON format.
return jsonify(results)
if __name__ == "__main__":
app.run(host='0.0.0.0')

app.run()
# TODO: Create route for filtering on zipcode and date
# TODO: Put all above into function for covid-19 data
# TODO: Look into making a Class for db connection

create_covid-19_zip.py → database/create_covid-19_zip.py View File


load_db.py → database/load_db.py View File


source_data/covid-19.xlsx → database/source_data/covid-19.xlsx View File


source_data/covid-19_20200810_zip.csv → database/source_data/covid-19_20200810_zip.csv View File


source_data/covid-19_20200824_zip.csv → database/source_data/covid-19_20200824_zip.csv View File


source_data/covid-19_20200831_zip.csv → database/source_data/covid-19_20200831_zip.csv View File


source_data/covid-data-extract-geography-august-10.xlsx → database/source_data/covid-data-extract-geography-august-10.xlsx View File


source_data/covid-data-extract-geography-august-24.xlsx → database/source_data/covid-data-extract-geography-august-24.xlsx View File


source_data/covid-data-extract-geography-august-31.xlsx → database/source_data/covid-data-extract-geography-august-31.xlsx View File


Loading…
Cancel
Save