-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
40 lines (34 loc) · 1.34 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import geocoder
from fastapi import FastAPI
from fastapi.exceptions import HTTPException
from fastapi.middleware.cors import CORSMiddleware
from utils.dictFunctions import structuredGeolocations
# Initiating the router
app = FastAPI()
# App configs
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"], # Allow all methods or specify with ["POST", "GET"]
allow_headers=["*"], # Allow all headers or specify with ["X-Custom-Header"]
)
GEONAMES_USERNAME = 'siri_yu'
# TODO: Organize the requests into different route-files
@app.get('/api/geolocations')
async def get_geolocations(placename: str):
# Static parameters (FIXME: outsource if necessary)
params = {
'q': placename,
'maxRows': 5,
'username': GEONAMES_USERNAME
}
# Geocoded location
geoObjects = geocoder.geonames(location=params['q'], maxRows=params['maxRows'], key=params['username'])
# Extracting further details from detected toponyms
g_details = [geocoder.geonames(location=row.geonames_id, method='details', key=params['username']) for row in geoObjects]
# Array, which includes geolocations and their respective coordinates
gls = structuredGeolocations(g_details)
if len(gls) == 0:
raise HTTPException(status_code=404, detail="No location available")
return gls