├── .dockerignore ├── .github ├── ISSUE_TEMPLATE.md ├── ISSUE_TEMPLATE │ ├── 01_bug-report.md │ ├── 02_feature-request.md │ ├── 03_documentation.md │ ├── 04_deployment-problem.md │ └── 05_anything-else.md ├── PULL_REQUEST_TEMPLATE.md └── SUPPORT.md ├── .gitignore ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── Dockerfile ├── LICENSE ├── README.md ├── app ├── README.md ├── covid_local_api │ ├── .flake8 │ ├── __init__.py │ ├── __version__.py │ ├── data │ │ ├── DE_place-hierarchy.csv │ │ └── DE_placeid-to-wikidata.json │ ├── db_handler.py │ ├── endpoints.py │ ├── local_test.py │ ├── place_handler.py │ ├── schema.py │ ├── search-dashboard.py │ └── utils │ │ ├── __init__.py │ │ ├── endpoint_utils.py │ │ └── place_request_utils.py ├── prestart.sh ├── requirements.txt ├── setup.cfg └── setup.py ├── build.py ├── docs └── images │ ├── dashboard-browser.png │ ├── github-banner.png │ └── mobile-mockup.png ├── resources └── prestart.sh └── scripts └── rki-plz-tool-to-csv.py /.dockerignore: -------------------------------------------------------------------------------- 1 | * 2 | !app/ 3 | !resources/ 4 | **/.DS_Store 5 | **/Thumbs.db 6 | **/*.pyc 7 | .DS_Store -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 6 | 7 | **Describe the issue:** 8 | 9 | 10 | 11 | **Technical details:** 12 | 13 | - Project version: 14 | - Command used to start the container : -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/01_bug-report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: "\U0001F6A8 Bug report" 3 | about: Did you come across a bug or unexpected behaviour differing from the docs? 4 | title: '' 5 | labels: bug 6 | assignees: '' 7 | 8 | --- 9 | 10 | 17 | 18 | **Describe the bug:** 19 | 20 | 21 | 22 | **Expected behaviour:** 23 | 24 | 25 | 26 | **Steps to reproduce the issue:** 27 | 28 | 29 | 30 | 36 | 37 | **Technical details:** 38 | 39 | - Project version: 40 | - Command used to start the container : 41 | 42 | **Possible Fix:** 43 | 44 | 45 | 46 | **Additional context:** 47 | 48 | 49 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/02_feature-request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: "\U0001F381 Feature request" 3 | about: Do you have an idea for an improvement or a new feature? 4 | title: '' 5 | labels: feature-request 6 | assignees: '' 7 | 8 | --- 9 | 10 | 15 | 16 | **Feature description:** 17 | 18 | 23 | 24 | **Problem and motivation:** 25 | 26 | 29 | 30 | **Is this something you're interested in working on?** 31 | 32 | 33 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/03_documentation.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: "\U0001F4DA Documentation" 3 | about: Is there a mistake in the docs, is anything unclear or do you have a suggestion? 4 | title: '' 5 | labels: enhancement, docs 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe your request:** 11 | 12 | 13 | 14 | **Which page or section is this issue related to?** 15 | 16 | 17 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/04_deployment-problem.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: "\U0001F433 Deployment Problem" 3 | about: Do you have problems with deployment, and none of the suggestions in the docs 4 | and other issues helped? 5 | title: '' 6 | labels: '' 7 | assignees: '' 8 | 9 | --- 10 | 11 | 16 | 17 | **Describe the issue:** 18 | 19 | 20 | 21 | **Technical details:** 22 | 23 | - Project version: 24 | - Command used to start the container : -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/05_anything-else.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: "\U0001F4AC Anything else?" 3 | about: For general usage questions, please consider using other channels listed on our documentation. 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | 15 | 16 | **Describe the issue:** 17 | 18 | 19 | 20 | **Technical details:** 21 | 22 | - Project version: 23 | - Command used to start the container : 24 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 4 | 5 | **What kind of change does this PR introduce?** 6 | 7 | 8 | - [ ] Bugfix 9 | - [ ] New Feature 10 | - [ ] Feature Improvment 11 | - [ ] Refactoring 12 | - [ ] Documentation 13 | - [ ] Other, please describe: 14 | 15 | **Description:** 16 | 17 | 18 | **Checklist:** 19 | 21 | 22 | - [ ] I have read the [CONTRIBUTING](https://github.com/cotect/cotect/blob/master/CONTRIBUTING.md) document. 23 | - [ ] My changes don't require a change to the documentation, or if they do, I've added all required information. 24 | -------------------------------------------------------------------------------- /.github/SUPPORT.md: -------------------------------------------------------------------------------- 1 | ## Support 2 | 3 | Please refer to our [support](https://github.com/cotect/cotect#support) and [contribution](https://github.com/cotect/cotect#contribution) sections on our main README for more information. 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | app/covid_local_api/data/spreedsheet.xlsx 2 | 3 | # IntelliJ 4 | target/ 5 | .idea/ 6 | *.iml 7 | 8 | # Sublime 9 | *.sublime-workspace 10 | 11 | # Eclipse 12 | .settings 13 | 14 | # VS Code 15 | .vscode 16 | .project 17 | .classpath 18 | 19 | # Java 20 | *.class 21 | target/ 22 | 23 | # C 24 | *.so 25 | 26 | # Python 27 | *.pyc 28 | *.egg-info 29 | __pycache__ 30 | .ipynb_checkpoints 31 | .Python 32 | dist/ 33 | build/ 34 | .python-version 35 | .installed.cfg 36 | *.egg 37 | 38 | # Byte-compiled / optimized / DLL files 39 | *.pyc 40 | __pycache__/ 41 | *.py[cod] 42 | *$py.class 43 | 44 | # Other Artifacts 45 | hs_err_pid* 46 | *.log 47 | *.swp 48 | temp/* 49 | .DS_Store -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at mltooling.team@gmail.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | TBD -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7 2 | 3 | COPY ./app/ /app 4 | 5 | RUN pip install -e /app 6 | 7 | # Default Configuration 8 | ENV MODULE_NAME="covid_local_api.endpoints" -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 cotect 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |  2 | 3 |
API for local information on COVID-19
(hotlines, websites, test sites, health departments)
6 | 7 | Search dashboard • 8 | Swagger docs • 9 | Contact 10 | 11 |
12 | 13 | 18 | 19 | 20 | ## What is this good for? 21 | 22 | This API provides local information and addresses on COVID-19 for a given location (e.g. local hotlines & websites, nearby test sites, relevant health departments). It can be easily integrated into existing websites and apps, giving the user relevant information for their location. E.g., a tracing app could use this API to refer the user to their nearest test site in case of an infection risk. Features: 23 | 24 | - **Local information** (hotlines, websites, test sites, health departments) for major German cities (more coming soon) 25 | - **Integration in websites & apps** via REST API and Python/JavaScript clients 26 | - **Built-in location search** for cities, neighborhoods, states, ... 27 | 28 | Check out our [search dashboard](http://ec2-3-90-67-33.compute-1.amazonaws.com:8600) to get an idea of which data the API offers! 29 | 30 |
31 |
32 |
33 |
Our API in practice – web and mobile
36 | 37 | 38 | ## Client libraries 39 | 40 | We now have client libraries for Python ([covid-local-py](https://github.com/cotect/covid-local-py)) and JavaScript ([covid-local-js](https://github.com/cotect/covid-local-js)), so you can access the API directly from code. Both repos show some examples in their READMEs. Please make sure to still read the usage guide below to get an idea of how the API works in general. (Need a client for another language? [Generate it yourself](#generating-client-libraries) or [reach out](mailto:johannes.rieke@gmail.com)) 41 | 42 | 43 | ## Usage 44 | 45 | You can try out the API using our live deployment at 46 | http://ec2-3-90-67-33.compute-1.amazonaws.com (note that the main page does not return information!). 47 | 48 | For example, to get all local information for Berlin Mitte, go to: 49 | 50 | http://ec2-3-90-67-33.compute-1.amazonaws.com/all?place_name=Berlin&20Mitte 51 | 52 | The data is returned as JSON. Note that information for hierachically higher areas 53 | (e.g. country-wide hotlines) are automatically returned as well. 54 | 55 | ### Endpoints 56 | 57 | Above, we used the `/all` endpoint to request all information from the database. You can 58 | also use the more specific endpoints `/hotlines`, `/websites`, `/test_sites` and 59 | `/health_departments`, which will only a return a subset of the data, e.g.: 60 | 61 | http://ec2-3-90-67-33.compute-1.amazonaws.com/hotlines?place_name=Berlin&20Mitte 62 | 63 | ### Place search 64 | 65 | To specify the location of the query, we support two options: You can either use the 66 | `place_name` parameter like above (with a city, neighborhood, state, ...). Under the 67 | hood, this searches on geonames.org and simply uses the first result to search our 68 | database. To get more control over the place selection (e.g. if the place name is 69 | ambiguous), you can use the `/places` endpoint: 70 | 71 | http://ec2-3-90-67-33.compute-1.amazonaws.com/places?q=Berlin&20Mitte 72 | 73 | This returns a list of places for your query. It uses the 74 | [geonames.org location search](http://www.geonames.org/export/geonames-search.html) 75 | with sensible defaults (e.g. search only for cities and districts) and clean 76 | formatting. If you found the correct place among these results, you can extract its 77 | `geonames_id` and pass it to the other endpoints like this: 78 | 79 | http://ec2-3-90-67-33.compute-1.amazonaws.com/all?geonames_id=2950159 80 | 81 | ### Docs 82 | 83 | For more details on endpoints, query parameters, and output formats, please have a 84 | look at the [Swagger docs](http://ec2-3-90-67-33.compute-1.amazonaws.com/docs). 85 | 86 | 87 | ## Running the API locally 88 | 89 | To run the API locally, clone this repo and run the following command: 90 | 91 | cd ./covid-local-api/app/covid_local_api 92 | uvicorn local_test:app --reload 93 | 94 | The API should now be accessible at 127.0.0.1:8000. You can also deploy the API with 95 | docker, using the dockerfile in the repo (note that this will serve the API at port 80 instead of 8000). 96 | 97 | To start the [search dashboard](http://ec2-3-90-67-33.compute-1.amazonaws.com:8600), 98 | run: 99 | 100 | streamlit run search-dashboard.py 101 | 102 | This will start the dashboard on port 8501. Note that the dockerfile automatically 103 | starts the dashboard along with the API (using the `prestart.sh` file; docker deployment uses port 8600 instead of 8501). 104 | 105 | 106 | ## Data 107 | 108 | Help us collect new data with our Google Form: 109 | [https://bit.ly/covid-local-form](https://bit.ly/covid-local-form) 110 | 111 | The data for this project is stored in a 112 | [Google Sheet](https://docs.google.com/spreadsheets/d/1AXadba5Si7WbJkfqQ4bN67cbP93oniR-J6uN0_Av958/edit?usp=sharing) 113 | (note that there is one worksheet for each data type). If you think that any of the 114 | data is wrong, please add a comment directly to the document or write to 115 | johannes.rieke@gmail.com. You can also use our 116 | [dashboard](http://ec2-3-90-67-33.compute-1.amazonaws.com:8600) to search through the 117 | data. 118 | 119 | 120 | ## Requirements 121 | 122 | Python 3.7 and all packages in requirements.txt 123 | 124 | 125 | ## Generating client libraries 126 | 127 | Client libraries can be generated automatically with the [OpenAPI Generator](https://openapi-generator.tech). If you update an existing client, please make sure to copy its `README.md` file before, as it was probably adapted manually. 128 | 129 | For Python, run: 130 | 131 | ```shell 132 | openapi-generator generate -i http://ec2-3-90-67-33.compute-1.amazonaws.com/openapi.json -g python -o covid-local-py --additional-properties packageName=covid_local,projectName=covid-local-py,packageUrl=https://github.com/cotect/covid-local-py,packageVersion=0.1.0 --git-host github.com --git-user-id cotect --git-repo-id covid-local-py 133 | ``` 134 | 135 | For JavaScript, run: 136 | 137 | ```shell 138 | openapi-generator generate -i http://ec2-3-90-67-33.compute-1.amazonaws.com/openapi.json -g javascript -o covid-local-js --additional-properties moduleName=CovidLocal,projectName=covid-local-js,projectVersion=0.1.0 --git-host github.com --git-user-id cotect --git-repo-id covid-local-js 139 | ``` 140 | 141 | Both commands will use the API definition from the live deployment and write the client to `covid-local-py` or `covid-local-js`. After creating the client, you need to manually replace all occurences (in all files) of `http://localhost` with `http://ec2-3-90-67-33.compute-1.amazonaws.com`. This way, the client will pull data from the live deployment of the API (instead of a local deployment). 142 | -------------------------------------------------------------------------------- /app/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Convert to swagger 2.0 specs via [api-spec-converter](https://github.com/LucyBot-Inc/api-spec-converter): 5 | 6 | ``` 7 | api-spec-converter --from openapi_3 --to swagger_2 --syntax yaml --check ./openapi.json > cotect_swagger2.yaml 8 | ``` 9 | 10 | 11 | Test API locally: 12 | ``` 13 | pip install -e . 14 | cd ./covid_local_api && uvicorn local_test:app --reload 15 | ``` 16 | -------------------------------------------------------------------------------- /app/covid_local_api/.flake8: -------------------------------------------------------------------------------- 1 | [flake8] 2 | max-line-length = 88 3 | extend-ignore = E203,W291,W293 -------------------------------------------------------------------------------- /app/covid_local_api/__init__.py: -------------------------------------------------------------------------------- 1 | from covid_local_api.__version__ import __version__ 2 | -------------------------------------------------------------------------------- /app/covid_local_api/__version__.py: -------------------------------------------------------------------------------- 1 | VERSION = (0, 1, 0) 2 | 3 | __version__ = '.'.join(map(str, VERSION)) 4 | -------------------------------------------------------------------------------- /app/covid_local_api/db_handler.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import sqlite3 3 | import math 4 | import logging 5 | 6 | 7 | class DatabaseHandler: 8 | def __init__(self): 9 | """Initializes the database with the data from the Google Sheet""" 10 | self.con = None 11 | self.update_database() 12 | 13 | def delete_database(self): 14 | """Closes the database connection, which deletes the database""" 15 | # See https://stackoverflow.com/questions/48732439/deleting-a-database-file-in-memory 16 | if self.con is not None: 17 | logging.info("Deleting old database...") 18 | self.con.close() 19 | 20 | def update_database(self): 21 | """Updates the database with the current data from the Google Sheet. 22 | 23 | Downloads the data as an excel file and writes it to an in-memory sqlite 24 | database. 25 | """ 26 | self.delete_database() 27 | 28 | # Create in-memory sqlite3 database. 29 | # We can use check_same_thread because we only read from the database, so 30 | # there's no concurrency 31 | logging.info("Creating new database...") 32 | self.con = sqlite3.connect(":memory:", check_same_thread=False) 33 | 34 | # Download excel file from Google Sheets, read it with pandas and write to 35 | # database. 36 | url = "https://docs.google.com/spreadsheets/d/1AXadba5Si7WbJkfqQ4bN67cbP93oniR-J6uN0_Av958/export?format=xlsx" 37 | dfs = pd.read_excel(url, sheet_name=None) 38 | for table, df in dfs.items(): 39 | df.to_sql(table, self.con) 40 | 41 | # Make database return dicts instead of tuples. 42 | # From: https://stackoverflow.com/questions/3300464/how-can-i-get-dict-from-sqlite-query 43 | def dict_factory(cursor, row): 44 | d = {} 45 | for idx, col in enumerate(cursor.description): 46 | d[col[0]] = row[idx] 47 | return d 48 | 49 | self.con.row_factory = dict_factory 50 | logging.info("Database successfully updated") 51 | 52 | def get(self, sheet, geonames_ids): 53 | """Returns all entries from `sheet`, which match one of the ids in 54 | `geonames_ids`. 55 | 56 | Args: 57 | sheet (str): The worksheet in the Google Sheet 58 | geonames_ids (int): The geonames_ids of the places to search for 59 | 60 | Returns: 61 | (list of dict): Filtered database entries as key-value dicts 62 | """ 63 | cur = self.con.execute( 64 | f"SELECT * FROM {sheet} WHERE geonames_id " 65 | f"IN ({', '.join(map(str, geonames_ids))})" 66 | ) 67 | dicts = cur.fetchall() 68 | return dicts 69 | 70 | def get_nearby(self, sheet, lat, lon, max_distance=0.5, limit=5): 71 | """Returns nearby entries from `sheet` for a latitude/longitude pair, sorted by 72 | distance. 73 | 74 | Note: Distance is right now not the true distance in kilometers, but the 75 | "distance" in degrees lat/lon (i.e. sqrt((lat1-lat2)**2 + (lon1-lon2)**2)). 76 | This value is proportional to the kilometers at the equator but deviates the 77 | further you move to the poles. We cannot calculate the true distance 78 | because the sqlite database can't do complex math operations. 79 | 80 | Args: 81 | sheet (str): The worksheet in the Google Sheet 82 | lat (float): The latitude of the place 83 | lon (float): The longitude of the place 84 | max_distance (float, optional): Maximum distance to search for objects (in 85 | degrees lat/lon; default: 0.5) 86 | limit (float, optional): Maximum number of elements to return (default: 5). 87 | If more elements were found within `max_distance`, return the closest 88 | ones. 89 | 90 | Returns: 91 | list of dict: Filtered database entries as key-value dicts 92 | """ 93 | # Query elements from the sheet that are closer to lat/lon than max_distance, 94 | # order them by the distance, and limit number of rows to limit. 95 | # Distance is in degrees lat/lon, see comment in docstring. 96 | # TODO: Find a better solution to calculate distances, based on true distance 97 | # in kilometers. 98 | squared_distance = f"(lat-{lat})*(lat-{lat})+(lon-{lon})*(lon-{lon})" 99 | query = ( 100 | f"SELECT *, {squared_distance} AS distance FROM test_sites " 101 | f"WHERE {squared_distance} <= {max_distance}*{max_distance} " 102 | f"ORDER BY {squared_distance} LIMIT {limit}" 103 | ) 104 | cur = self.con.execute(query) 105 | 106 | dicts = cur.fetchall() 107 | for d in dicts: 108 | # Distance in SQL query is squared, so take the sqrt here. 109 | d["distance"] = math.sqrt(d["distance"]) 110 | return dicts 111 | -------------------------------------------------------------------------------- /app/covid_local_api/endpoints.py: -------------------------------------------------------------------------------- 1 | import geocoder 2 | import uvicorn 3 | from starlette.responses import RedirectResponse 4 | from fastapi import FastAPI, Query, HTTPException 5 | from typing import List 6 | from enum import Enum 7 | from timeloop import Timeloop 8 | from datetime import timedelta 9 | 10 | from covid_local_api.__version__ import __version__ 11 | from covid_local_api.db_handler import DatabaseHandler 12 | from covid_local_api.schema import ( 13 | ResultsList, 14 | Place, 15 | ) 16 | from covid_local_api.utils import endpoint_utils, place_request_utils 17 | 18 | 19 | # TODO: Implement place handler code at some point in the future like below. 20 | # from covid_local_api.place_handler import ( 21 | # PlaceHandler, 22 | # load_place_hierarchy, 23 | # load_place_mapping, 24 | # ) 25 | # data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "data") 26 | # place_handler = PlaceHandler( 27 | # load_place_mapping(os.path.join(data_path, "DE_placeid-to-wikidata.json")), 28 | # load_place_hierarchy(os.path.join(data_path, "DE_place-hierarchy.csv")), 29 | # country_codes=["DE"], 30 | # ) 31 | # @app.get("/test_place_handler") 32 | # def test_place_handler(place_id: str = Query(..., description="Place ID to filter.")): 33 | # return place_handler.resolve_hierarchies(place_id) 34 | 35 | 36 | # Initialize database and schedule daily update 37 | db = DatabaseHandler() 38 | tl = Timeloop() 39 | 40 | 41 | @tl.job(interval=timedelta(seconds=86400)) # once per day 42 | def update_database(): 43 | db.update_database() 44 | 45 | 46 | tl.start() 47 | 48 | 49 | # Initialize API 50 | app = FastAPI( 51 | title="COVID-19 Local API", 52 | description="API to get local help information about COVID-19 (hotlines, websites, " 53 | "test sites, health departments)", 54 | version=__version__, 55 | ) 56 | 57 | 58 | # ---------------------------------- Helper functions ---------------------------------- 59 | place_name_query = Query( 60 | None, 61 | description="The name of the place, e.g. a city, neighborhood, state (either " 62 | "place_name or geonames_id must be provided)", 63 | ) 64 | 65 | 66 | geonames_id_query = Query( 67 | None, 68 | description="The geonames.org id of the place (either place_name or " 69 | "geonames_id must be provided)", 70 | ) 71 | 72 | 73 | class SearchProvider(str, Enum): 74 | """Enum of the available search providers for the places endpoint""" 75 | 76 | geonames = "geonames" 77 | 78 | 79 | def geocoder_to_place(result): 80 | """Convert a result object from geocoder to a Place object""" 81 | return Place( 82 | name=result.address, 83 | country=result.country, 84 | country_code=result.country_code, 85 | state=result.state, 86 | description=result.description + " - " + result.class_description, 87 | geonames_id=result.geonames_id, 88 | lat=result.lat, 89 | lon=result.lng, 90 | search_provider=SearchProvider.geonames, 91 | ) 92 | 93 | 94 | def find_place(place_name=None, geonames_id=None): 95 | """Finds and returns the place for the given query parameters. 96 | 97 | If geonames_id is given, simply get some more information about it. If 98 | place_name is given, search the /places endpoint and return the first result. If 99 | neither is given, raise an error. 100 | 101 | Args: 102 | place_name (str, optional): The name of the place to search for (used as query 103 | parameter for the places endpoint) 104 | geonames_id (int, optional): The geonames.org id of the place 105 | 106 | Returns: 107 | Place: The found place 108 | """ 109 | if geonames_id is None and place_name is None: 110 | raise HTTPException(400, "Either place_name or geonames_id must be provided") 111 | elif geonames_id is None: 112 | # Search by place_name and use first search result. 113 | places = search_places(q=place_name, limit=1, search_provider="geonames") 114 | if len(places) == 0: 115 | raise HTTPException( 116 | 400, f"Could not find any match for place_name: {place_name}" 117 | ) 118 | else: 119 | return places[0] 120 | else: 121 | # Get details for this geonames_id and return as Place object. 122 | search_result = geocoder.geonames( 123 | geonames_id, key=place_request_utils.get_geonames_user(), method="details" 124 | )[0] 125 | place = geocoder_to_place(search_result) 126 | return place 127 | 128 | 129 | def get_hierarchy(geonames_id): 130 | """Returns geonames ids of hierarchical parents (e.g. country for a city)""" 131 | hierarchy = geocoder.geonames( 132 | geonames_id, key=place_request_utils.get_geonames_user(), method="hierarchy", 133 | ) 134 | hierarchy = hierarchy[::-1] # reverse, so that more local areas come first 135 | geonames_ids_hierarchy = [item.geonames_id for item in hierarchy] 136 | return geonames_ids_hierarchy 137 | 138 | 139 | # ---------------------------------- Endpoints ----------------------------------------- 140 | @app.get( 141 | "/places", 142 | summary="Search for places via free-form query", 143 | response_model=List[Place], 144 | ) 145 | def search_places( 146 | q: str = Query( 147 | ..., 148 | description="Free-form query string (e.g. a city, neighborhood, state, ...)", 149 | ), 150 | limit: int = Query(5, description="Maximum number of entries to return"), 151 | search_provider: SearchProvider = Query( 152 | SearchProvider.geonames, 153 | description="The search provider (only geonames supported so far)", 154 | ), 155 | ): 156 | if search_provider == SearchProvider.geonames: 157 | # Search geonames API. 158 | search_results = geocoder.geonames( 159 | q, 160 | key=place_request_utils.get_geonames_user(), 161 | maxRows=limit, 162 | featureClass=["A", "P"], 163 | ) 164 | 165 | # Format the search results to Place objects and return them. 166 | places = [geocoder_to_place(result) for result in search_results] 167 | return places 168 | else: 169 | raise HTTPException(400, f"Search provider not supported: {search_provider}") 170 | 171 | 172 | @app.get( 173 | "/all", summary="Get all items for a place", response_model=ResultsList, 174 | ) 175 | def get_all( 176 | place_name: str = place_name_query, 177 | geonames_id: int = geonames_id_query, 178 | max_distance: float = Query( 179 | 0.5, description="Maximum distance in degrees lon/lat for test sites" 180 | ), 181 | limit: int = Query(5, description="Maximum number of test sites to return"), 182 | ): 183 | place = find_place(place_name, geonames_id) 184 | geonames_ids_hierarchy = get_hierarchy(place.geonames_id) 185 | return { 186 | "place": place, 187 | "hotlines": db.get("hotlines", geonames_ids_hierarchy), 188 | "websites": db.get("websites", geonames_ids_hierarchy), 189 | "test_sites": db.get_nearby( 190 | "test_sites", place.lat, place.lon, max_distance=max_distance, limit=limit 191 | ), 192 | "health_departments": db.get("health_departments", geonames_ids_hierarchy), 193 | } 194 | 195 | 196 | @app.get( 197 | "/hotlines", summary=f"Get hotlines for a place", response_model=ResultsList, 198 | ) 199 | def get_hotlines( 200 | place_name: str = place_name_query, geonames_id: int = geonames_id_query, 201 | ): 202 | place = find_place(place_name, geonames_id) 203 | geonames_ids_hierarchy = get_hierarchy(place.geonames_id) 204 | return { 205 | "place": place, 206 | "hotlines": db.get("hotlines", geonames_ids_hierarchy), 207 | } 208 | 209 | 210 | @app.get( 211 | "/websites", summary=f"Get websites for a place", response_model=ResultsList, 212 | ) 213 | def get_websites( 214 | place_name: str = place_name_query, geonames_id: int = geonames_id_query, 215 | ): 216 | place = find_place(place_name, geonames_id) 217 | geonames_ids_hierarchy = get_hierarchy(place.geonames_id) 218 | return { 219 | "place": place, 220 | "websites": db.get("websites", geonames_ids_hierarchy), 221 | } 222 | 223 | 224 | @app.get( 225 | "/test_sites", 226 | summary=f"Get nearby test sites for a place (sorted by distance to place)", 227 | response_model=ResultsList, 228 | ) 229 | def get_test_sites( 230 | place_name: str = place_name_query, 231 | geonames_id: int = geonames_id_query, 232 | max_distance: float = Query( 233 | 0.5, description="Maximum distance in degrees lon/lat for test sites" 234 | ), 235 | limit: int = Query(5, description="Maximum number of test sites to return"), 236 | ): 237 | place = find_place(place_name, geonames_id) 238 | # lat, lon = get_lat_lon(geonames_id) 239 | return { 240 | "geonames_id": geonames_id, 241 | "test_sites": db.get_nearby( 242 | "test_sites", place.lat, place.lon, max_distance=max_distance, limit=limit 243 | ), 244 | } 245 | 246 | 247 | @app.get( 248 | "/health_departments", 249 | summary=f"Get responsible health departments for a place", 250 | response_model=ResultsList, 251 | ) 252 | # TODO: This doesn't return results if e.g. Berlin is selected but the health department 253 | # is in Berlin Mitte. Maybe also search for the direct children of the geonames id 254 | # (but is direct children enough)? 255 | def get_health_departments( 256 | place_name: str = place_name_query, geonames_id: int = geonames_id_query, 257 | ): 258 | place = find_place(place_name, geonames_id) 259 | geonames_ids_hierarchy = get_hierarchy(place.geonames_id) 260 | return { 261 | "place": place, 262 | "health_departments": db.get("health_departments", geonames_ids_hierarchy), 263 | } 264 | 265 | 266 | @app.get( 267 | "/test", summary="Shows all entries for Berlin Mitte (redirects to /all endpoint)", 268 | ) 269 | def test(): 270 | response = RedirectResponse(url="/all?geonames_id=6545310") 271 | return response 272 | 273 | 274 | # Use function names as operation IDs 275 | endpoint_utils.use_route_names_as_operation_ids(app) 276 | 277 | 278 | # Run uvicorn server directly in here for debugging 279 | if __name__ == "__main__": 280 | uvicorn.run(app, debug=True) 281 | -------------------------------------------------------------------------------- /app/covid_local_api/local_test.py: -------------------------------------------------------------------------------- 1 | from covid_local_api.endpoints import app 2 | -------------------------------------------------------------------------------- /app/covid_local_api/place_handler.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import csv 3 | import json 4 | from typing import List, Optional 5 | 6 | from covid_local_api.utils.place_request_utils import ( 7 | GEONAMES_ID_PREFIX, 8 | OSM_ID_PREFIX, 9 | map_geonames_to_wikidata, 10 | map_osm_to_wikidata, 11 | map_wikidata_to_geonames, 12 | map_wikidata_to_osm, 13 | request_geonames_hierarchy, 14 | request_osm_hierarchy, 15 | search_geonames, 16 | search_osm, 17 | ) 18 | 19 | 20 | def load_place_hierarchy(hierarchy_csv_path: str): 21 | place_hierarchy = {} 22 | 23 | with open(hierarchy_csv_path, "r") as f: 24 | csv_reader = csv.reader(f, delimiter=",") 25 | for row in csv_reader: 26 | if row and len(row) == 2: 27 | place_hierarchy[row[0]] = row[1] 28 | return place_hierarchy 29 | 30 | 31 | def load_place_mapping(mapping_json_path: str): 32 | place_mapping = {} 33 | 34 | with open(mapping_json_path, "r") as f: 35 | place_mapping = json.load(f) 36 | 37 | return place_mapping 38 | 39 | 40 | def create_inverse_mapping( 41 | input_mapping: dict, filter_prefix: Optional[str] = None 42 | ) -> dict: 43 | new_mapping = {} 44 | for key, value_set in input_mapping.items(): 45 | if filter_prefix and not key.startswith(filter_prefix): 46 | continue 47 | 48 | for item in value_set: 49 | new_mapping.setdefault(item, set()).add(key) 50 | return new_mapping 51 | 52 | 53 | class PlaceHandler: 54 | def __init__( 55 | self, 56 | place_wikidata_mapping: dict, 57 | place_hierarchy: dict, 58 | country_codes: List[str] = None, 59 | resolve_unknown: bool = False, 60 | ): 61 | 62 | self._log = logging.getLogger(__name__) 63 | 64 | self._place_wikidata_mapping = place_wikidata_mapping 65 | self._place_hierarchy = place_hierarchy 66 | self._country_codes = country_codes 67 | 68 | self._place_inverse_mapping = create_inverse_mapping( 69 | self._place_wikidata_mapping 70 | ) 71 | 72 | def __getitem__(self, key): 73 | key = key.strip().upper() 74 | 75 | if key in self._place_wikidata_mapping: 76 | return list(self._place_wikidata_mapping[key]) 77 | 78 | if key in self._place_inverse_mapping: 79 | return list(self._place_inverse_mapping[key]) 80 | 81 | return [] 82 | 83 | def __contains__(self, key): 84 | key = key.strip().upper() 85 | 86 | if key in self._place_wikidata_mapping or key in self._place_inverse_mapping: 87 | return True 88 | 89 | def search_places(self, query: str, limit: int = 5): 90 | search_result = [] 91 | added_wikidata_ids = set() 92 | geonames_results = search_geonames(query, limit, self._country_codes) 93 | for result in geonames_results: 94 | wikidata_id = self.map_geonames_to_wikidata(result[0]) 95 | if wikidata_id and wikidata_id not in added_wikidata_ids: 96 | search_result.append((wikidata_id, result[1])) 97 | added_wikidata_ids.add(wikidata_id) 98 | 99 | if len(search_result) < limit: 100 | osm_results = search_osm(query, limit, self._country_codes) 101 | for result in osm_results: 102 | wikidata_id = self.map_osm_to_wikidata(result[0]) 103 | if wikidata_id and wikidata_id not in added_wikidata_ids: 104 | search_result.append((wikidata_id, result[1])) 105 | added_wikidata_ids.add(wikidata_id) 106 | if len(search_result) == limit: 107 | break 108 | 109 | return search_result 110 | 111 | def resolve_hierarchies(self, key: str) -> list: 112 | key = key.strip().upper() 113 | 114 | place_hierarchies = [] 115 | 116 | if key in self._place_wikidata_mapping: 117 | for wikidata_id in self._place_wikidata_mapping[key]: 118 | place_hierarchies.append(self.resolve_wikidata_hierarchy(wikidata_id)) 119 | return place_hierarchies 120 | 121 | key_to_add = None 122 | if key in self._place_inverse_mapping: 123 | key_to_add = key 124 | elif key.startswith(GEONAMES_ID_PREFIX): 125 | key_to_add = self.map_geonames_to_wikidata(key) 126 | print(key_to_add) 127 | elif key.startswith(OSM_ID_PREFIX): 128 | key_to_add = self.map_osm_to_wikidata()(key) 129 | 130 | if key_to_add: 131 | # Key is wikidata id 132 | place_hierarchies.append(self.resolve_wikidata_hierarchy(key_to_add)) 133 | return place_hierarchies 134 | 135 | def map_geonames_to_wikidata(self, geonames_id: str) -> str: 136 | geonames_id = str(geonames_id).strip().upper() 137 | if not geonames_id.startswith(GEONAMES_ID_PREFIX): 138 | geonames_id = GEONAMES_ID_PREFIX + geonames_id 139 | 140 | # TODO only return one result? 141 | if geonames_id in self and self[geonames_id]: 142 | return self[geonames_id][0] 143 | else: 144 | return map_geonames_to_wikidata(geonames_id) 145 | 146 | def map_wikidata_to_geonames(self, wikidata_id: str) -> str: 147 | if wikidata_id in self and self[wikidata_id]: 148 | for result in self[wikidata_id]: 149 | if result.startswith(GEONAMES_ID_PREFIX): 150 | return result 151 | return map_wikidata_to_geonames(wikidata_id) 152 | 153 | def map_osm_to_wikidata(self, osm_id: str) -> list: 154 | osm_id = str(osm_id).strip().upper() 155 | if not osm_id.startswith(OSM_ID_PREFIX): 156 | osm_id = OSM_ID_PREFIX + osm_id 157 | 158 | # TODO only return one result? 159 | if osm_id in self and self[osm_id]: 160 | return self[osm_id][0] 161 | else: 162 | return map_osm_to_wikidata(osm_id) 163 | 164 | def map_wikidata_to_osm(self, wikidata_id: str): 165 | if wikidata_id in self and self[wikidata_id]: 166 | for result in self[wikidata_id]: 167 | if result.startswith(OSM_ID_PREFIX): 168 | return result 169 | 170 | return map_wikidata_to_osm(wikidata_id) 171 | 172 | def request_wikidata_hierarchy_with_geonames(self, wikidata_id: str): 173 | wikidata_id = wikidata_id.strip().upper() 174 | wikidata_hierarchy = [] 175 | try: 176 | geonames_id = self.map_wikidata_to_geonames(wikidata_id) 177 | if not geonames_id: 178 | return [] 179 | geonames_hierarchy = request_geonames_hierarchy(geonames_id) 180 | for geonames_id in geonames_hierarchy: 181 | wikidata_id = self.map_geonames_to_wikidata(geonames_id) 182 | if wikidata_id and wikidata_id not in wikidata_hierarchy: 183 | wikidata_hierarchy.append(wikidata_id) 184 | return wikidata_hierarchy 185 | except Exception: 186 | self._log.info("Failed to request geonames hierarchy.", exc_info=True) 187 | return [] 188 | 189 | def request_wikidata_hierarchy_with_osm(self, wikidata_id: str): 190 | wikidata_id = wikidata_id.strip().upper() 191 | wikidata_hierarchy = [] 192 | try: 193 | osm_id = self.map_wikidata_to_osm(wikidata_id) 194 | if not osm_id: 195 | return [] 196 | osm_hierarchy = request_osm_hierarchy(osm_id) 197 | for osm_id in osm_hierarchy: 198 | wikidata_id = self.map_osm_to_wikidata(osm_id) 199 | if wikidata_id and wikidata_id not in wikidata_hierarchy: 200 | wikidata_hierarchy.append(wikidata_id) 201 | return wikidata_hierarchy 202 | except Exception: 203 | self._log.info("Failed to request osm hierarchy.", exc_info=True) 204 | return [] 205 | 206 | def resolve_wikidata_hierarchy( 207 | self, wikidata_id: str, prefer_geonames: bool = True, prefer_osm: bool = False 208 | ) -> List[str]: 209 | 210 | if wikidata_id in self._place_hierarchy: 211 | wikidata_hierarchy = [] 212 | current_item = wikidata_id 213 | wikidata_hierarchy.append(current_item) 214 | while current_item in self._place_hierarchy: 215 | current_item = self._place_hierarchy[current_item] 216 | wikidata_hierarchy.append(current_item) 217 | return list(reversed(wikidata_hierarchy)) 218 | 219 | geonames_wkdt_hierarchy = [] 220 | osm_wkdt_hierarchy = [] 221 | 222 | if prefer_geonames or not prefer_osm: 223 | geonames_wkdt_hierarchy = self.request_wikidata_hierarchy_with_geonames( 224 | wikidata_id 225 | ) 226 | if not geonames_wkdt_hierarchy: 227 | # Use osm as fallback 228 | self._log.info( 229 | "Fallback to using osm to resolve wikidata hierachy: " + wikidata_id 230 | ) 231 | return self.request_wikidata_hierarchy_with_osm(wikidata_id) 232 | 233 | if prefer_osm or not prefer_geonames: 234 | osm_wkdt_hierarchy = self.request_wikidata_hierarchy_with_osm(wikidata_id) 235 | if not osm_wkdt_hierarchy and not geonames_wkdt_hierarchy: 236 | # Use geonames as fallback 237 | self._log.info( 238 | "Fallback to using geonames to resolve wikidata hierachy: " 239 | + wikidata_id 240 | ) 241 | return self.request_wikidata_hierarchy_with_geonames(wikidata_id) 242 | 243 | if not geonames_wkdt_hierarchy and not osm_wkdt_hierarchy: 244 | self._log.info("Failed to get wikidata hiearchy for " + wikidata_id) 245 | 246 | if len(geonames_wkdt_hierarchy) > len(osm_wkdt_hierarchy): 247 | return geonames_wkdt_hierarchy 248 | else: 249 | return osm_wkdt_hierarchy 250 | -------------------------------------------------------------------------------- /app/covid_local_api/schema.py: -------------------------------------------------------------------------------- 1 | from typing import List, Optional 2 | from pydantic import BaseModel 3 | 4 | # Field validation 5 | # https://pydantic-docs.helpmanual.io/usage/schema/#field-customisation 6 | 7 | 8 | class Place(BaseModel): 9 | name: str 10 | geonames_id: int 11 | search_provider: str 12 | country: Optional[str] = None 13 | country_code: Optional[str] = None 14 | state: Optional[str] = None 15 | description: Optional[str] = None 16 | lat: Optional[float] = None 17 | lon: Optional[float] = None 18 | 19 | 20 | class Hotline(BaseModel): 21 | country_code: Optional[str] = None 22 | place: Optional[str] = None 23 | geonames_id: Optional[int] = None 24 | 25 | name: Optional[str] = None 26 | operator: Optional[str] = None 27 | phone: Optional[str] = None 28 | email: Optional[str] = None 29 | website: Optional[str] = None 30 | operating_hours: Optional[str] = None 31 | category: Optional[str] = None 32 | description: Optional[str] = None 33 | sources: Optional[str] = None 34 | 35 | 36 | class Website(BaseModel): 37 | country_code: Optional[str] = None 38 | place: Optional[str] = None 39 | geonames_id: Optional[int] = None 40 | 41 | name: Optional[str] = None 42 | operator: Optional[str] = None 43 | website: Optional[str] = None 44 | category: Optional[str] = None 45 | description: Optional[str] = None 46 | sources: Optional[str] = None 47 | 48 | 49 | class TestSite(BaseModel): 50 | country_code: Optional[str] = None 51 | lat: Optional[float] = None 52 | lon: Optional[float] = None 53 | 54 | name: Optional[str] = None 55 | street: Optional[str] = None 56 | zip_code: Optional[int] = None 57 | city: Optional[str] = None 58 | address_supplement: Optional[str] = None 59 | phone: Optional[str] = None 60 | website: Optional[str] = None 61 | operating_hours: Optional[str] = None 62 | appointment_required: Optional[bool] = None 63 | description: Optional[str] = None 64 | sources: Optional[str] = None 65 | 66 | distance: Optional[float] = None # added dynamically 67 | 68 | 69 | class HealthDepartment(BaseModel): 70 | country_code: Optional[str] = None 71 | place: Optional[str] = None 72 | geonames_id: Optional[int] = None 73 | 74 | name: Optional[str] = None 75 | department: Optional[str] = None 76 | street: Optional[str] = None 77 | zip_code: Optional[int] = None 78 | city: Optional[str] = None 79 | address_supplement: Optional[str] = None 80 | phone: Optional[str] = None 81 | fax: Optional[str] = None 82 | email: Optional[str] = None 83 | website: Optional[str] = None 84 | sources: Optional[str] = None 85 | 86 | 87 | class ResultsList(BaseModel): 88 | place: Place 89 | hotlines: List[Hotline] = [] 90 | websites: List[Website] = [] 91 | test_sites: List[TestSite] = [] 92 | health_departments: List[HealthDepartment] = [] 93 | -------------------------------------------------------------------------------- /app/covid_local_api/search-dashboard.py: -------------------------------------------------------------------------------- 1 | import streamlit as st 2 | import requests 3 | 4 | # import pandas as pd 5 | 6 | API_URL = "http://127.0.0.1:80" 7 | 8 | st.title("Search for local Corona information") 9 | "Enter your location and we will show you local hotlines, websites, test sites, health departments, and restrictions." 10 | 11 | place_query = st.text_input("City, Neighborhood, State, ...", value="Berlin Mitte") 12 | 13 | # Search for place with /places endpoint. 14 | places = requests.get(f"{API_URL}/places?q={place_query}").json() 15 | 16 | if len(places) == 0: 17 | "We could not find this place!" 18 | else: 19 | place = places[0] 20 | f"Found this place: {place['name']} ({place['country']}) – Geonames ID: {place['geonames_id']}" 21 | st.markdown("