mirror of
https://github.com/autistic-symposium/web3-starter-py.git
synced 2025-05-17 22:20:22 -04:00
💾
This commit is contained in:
parent
0696b08d04
commit
314bdf533e
94 changed files with 11 additions and 7 deletions
0
web2-projects/fastapi-location-app/api/__init__.py
Normal file
0
web2-projects/fastapi-location-app/api/__init__.py
Normal file
14
web2-projects/fastapi-location-app/api/api.py
Normal file
14
web2-projects/fastapi-location-app/api/api.py
Normal file
|
@ -0,0 +1,14 @@
|
|||
|
||||
from .routes import router as LocationRouter
|
||||
from fastapi import FastAPI
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/", tags=["Root"])
|
||||
async def read_root() -> dict:
|
||||
return {
|
||||
"message": "Location API 🗺✨. Try /docs to learn about this API"
|
||||
}
|
||||
|
||||
app.include_router(LocationRouter, prefix="/tzone")
|
35
web2-projects/fastapi-location-app/api/methods.py
Normal file
35
web2-projects/fastapi-location-app/api/methods.py
Normal file
|
@ -0,0 +1,35 @@
|
|||
import pytz
|
||||
import datetime
|
||||
from geopy.geocoders import Nominatim
|
||||
from timezonefinder import TimezoneFinder
|
||||
|
||||
|
||||
def _find_timezone(lat, lon) -> int:
|
||||
obj = TimezoneFinder()
|
||||
tzone = obj.timezone_at(lng=lon, lat=lat)
|
||||
timezone = datetime.datetime.now(pytz.timezone(tzone)).strftime('%z')
|
||||
try:
|
||||
timezone = int(timezone[:-2])
|
||||
except:
|
||||
print('Error formatting timezone {}'.format(timezone))
|
||||
return timezone
|
||||
|
||||
|
||||
def get_location(data) -> dict:
|
||||
try:
|
||||
city = data['city']
|
||||
except:
|
||||
print('Data is ill-formatted: {}'.format(data))
|
||||
|
||||
geolocator = Nominatim(user_agent="$choices")
|
||||
location = geolocator.geocode(city)
|
||||
|
||||
data = {
|
||||
'lat': location.latitude,
|
||||
'lon': location.longitude,
|
||||
'tzone': _find_timezone(location.latitude, location.longitude)
|
||||
}
|
||||
|
||||
return data
|
||||
|
||||
|
17
web2-projects/fastapi-location-app/api/routes.py
Normal file
17
web2-projects/fastapi-location-app/api/routes.py
Normal file
|
@ -0,0 +1,17 @@
|
|||
from pydantic import BaseModel
|
||||
from fastapi import APIRouter
|
||||
from .methods import get_location
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
class City(BaseModel):
|
||||
city: str
|
||||
|
||||
|
||||
@router.post("/")
|
||||
def search_city(data: City) -> dict:
|
||||
return get_location(data.dict())
|
||||
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue